我正在从相机获取直接流,我需要将 Bitmap
保存到 ByteBuffer
中并恢复它。这是我的代码:
YuvImage yuv = new YuvImage(data.getExtractImageData(), previewFormat, width, height, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, width, height), 50, out);
byte[] bytes = out.toByteArray();
Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Bitmap imageResult = RotateImage(image, 4 - rotation);
imageResult = cropBitmap(imageResult, data.getRect());
int nBytes = imageResult.getByteCount();
ByteBuffer buffer = ByteBuffer.allocateDirect(nBytes);
imageResult.copyPixelsToBuffer(buffer);
return buffer.array();
将 byte[]
转换回 Bitmap
的代码:
Bitmap bitmap = BitmapFactory.decodeByteArray(images.getImage(), 0, images.getImage().length);
但是,bitmap
转换后为 Null...
知道哪里出了问题吗?
澄清一下:我需要将 byte[] 图像
保存在 native 内存中,这就是我执行 ByteBuffer.allocateDirect
的原因。我需要在特定点裁剪图像,这就是为什么我需要 bitmap
。
最佳答案
decodeByteArray()解码存储在字节数组中的压缩图像(例如 JPEG 或 PNG)。然而copyPixelsToBuffer()将 Bitmap 的内容“按原样”(即未压缩)复制到字节缓冲区中,因此它不能被 decodeByteArray() 解码。
如果您不想重新编码您的位图,您可以像现在一样使用 copyPixelsToBuffer(),并将您的第二个代码块更改为使用 copyPixelsFromBuffer()而不是 decodeByteArray()。
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(images.getImage()));
您需要保存宽度和高度。还要确保 Bitmap.Config
相同。
基本上,如果您将其保存为压缩文件,则必须将其加载为压缩文件,如果您将其未压缩文件保存,则必须将其未压缩文件加载。
关于android - 如何使用 ByteBuffer 在 native 内存中保存位图图像并恢复它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28959280/