我正在尝试将图像对象转换为位图,但它返回null.
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
Bitmap myBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,null);
图像本身是jpeg图像,我可以将其保存到磁盘上,我想要转换为位图的原因是因为我想在将其保存到磁盘之前进行最后的旋转.
在类BitmapFactory中进行挖掘,我看到了这一行.
bm = nativeDecodeByteArray(data, offset, length, opts);
该行返回null.
进一步挖掘调试器
private static native Bitmap nativeDecodeByteArray(byte[] data, int offset,
int length, Options opts);
假定返回Bitmap对象,但返回null.
有技巧或想法吗?
谢谢
解决方法:
您尚未复制字节.您已检查容量,但未复制字节.
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
Bitmap myBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,null);