Android byte 和 Bitmap 互转工具代码

Bitmap 转 RGBA
// bitmap 转 RGBA
public static byte[] bitmapToRgba(Bitmap bitmap) {
    if (bitmap.getConfig() != Bitmap.Config.ARGB_8888)
        throw new IllegalArgumentException("Bitmap must be in ARGB_8888 format");
    int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
    byte[] bytes = new byte[pixels.length * 4];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    int i = 0;
    for (int pixel : pixels) {
        // Get components assuming is ARGB
        int A = (pixel >> 24) & 0xff;
        int R = (pixel >> 16) & 0xff;
        int G = (pixel >> 8) & 0xff;
        int B = pixel & 0xff;
        bytes[i++] = (byte) R;
        bytes[i++] = (byte) G;
        bytes[i++] = (byte) B;
        bytes[i++] = (byte) A;
    }
    return bytes;
}
RGBA 转 Bitmap
// RGBA 转 bitmap
public static Bitmap bitmapFromRgba(int width, int height, byte[] bytes) {
    int[] pixels = new int[bytes.length / 4];
    int j = 0;

    for (int i = 0; i < pixels.length; i++) {
        int R = bytes[j++] & 0xff;
        int G = bytes[j++] & 0xff;
        int B = bytes[j++] & 0xff;
        int A = bytes[j++] & 0xff;

        int pixel = (A << 24) | (R << 16) | (G << 8) | B;
        pixels[i] = pixel;
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}
保存 Bitmap 到 SD 卡
// 保存 bitmap 到 sd 卡sdcard/Android/data/应用包名/cache/ 目录下
public void saveBitmap(int width, int height, ByteBuffer buffer) {
    Bitmap bitmap = bitmapFromRgba(width, height, buffer.array());
    try {
        File dir = mContext.getExternalCacheDir();
        boolean mkdir = dir.mkdir();
        File file = new File(dir, "/snapshotVideo_" + System.currentTimeMillis() + ".jpg");
        boolean newFile = file.createNewFile();
        Log.e("xbo", "onSnapshotComplete: newFile = " + newFile);
        saveBitmap(file.getAbsolutePath(), bitmap);
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("xbo", "onSnapshotComplete: e = " + e.toString());
    }
}

// 将 Bitmap 保存到相册
public static File saveBitmap(String fileName, Bitmap bitmap) {
    try {
        File file = new File(fileName);
        OutputStream outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.close();
        return file;
    } catch (Exception e) {
        Log.e("xbo", "saveBitmap: e = " + e.toString());
        return null;
    }
}
NV21 转 Bitmap
/**
 * nv21 byte[] 转 Bitmap
 */
public static Bitmap convertData2(int width, int height, int rotation, int bufferLength, byte[] buffer, int yStride, int uStride, int vStride) {
    byte[] NV21 = new byte[bufferLength];
    swapYU12toYUV420SemiPlanar(buffer, NV21, width, height, yStride, uStride, vStride);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    int[] strides = {yStride, yStride};
    YuvImage image = new YuvImage(NV21, ImageFormat.NV21, width, height, strides);

    image.compressToJpeg(
            new Rect(0, 0, image.getWidth(), image.getHeight()),
            100, baos);

    // rotate picture when saving to file
    Matrix matrix = new Matrix();
    matrix.postRotate(rotation);
    byte[] bytes = baos.toByteArray();
    try {
        baos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    Bitmap target = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

    return target;
}

private static void swapYU12toYUV420SemiPlanar(byte[] yu12bytes, byte[] i420bytes, int width, int height, int yStride, int uStride, int vStride) {
    System.arraycopy(yu12bytes, 0, i420bytes, 0, yStride * height);
    int startPos = yStride * height;
    int yv_start_pos_u = startPos;
    int yv_start_pos_v = startPos + startPos / 4;
    for (int i = 0; i < startPos / 4; i++) {
        i420bytes[startPos + 2 * i + 0] = yu12bytes[yv_start_pos_v + i];
        i420bytes[startPos + 2 * i + 1] = yu12bytes[yv_start_pos_u + i];
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值