Bitmap与byte[]转换

一、Bitmap转换成byte[]

1.文件流的方式转换

ByteArrayOutputStream baos = new ByteArrayOutputStream();
mRBmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();

2. Bitmap中的方法copyPixelsToBuffer

Bitmap b = ...
int bytes = b.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer

byte[] data = buffer.array(); //Get the bytes array of the bitmap

二、byte[]转Bitmap

public Bitmap Bytes2Bimap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }

三、Bitmap转换成YuvImage

Bitmap b = ...
int bytes = b.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer

byte[] data = buffer.array(); //Get the bytes array of the bitmap
YuvImage yuv = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null);

参考:https://www.codota.com/code/java/methods/android.graphics.Bitmap/copyPixelsToBuffer

            https://blog.csdn.net/xidiancoder/article/details/51649465

 

你可能感兴趣的:(android)