Android:将图像对象转换为位图不起作用
我试图将图像对象转换为位图,但它返回nullAndroid:将图像对象转换为位图不起作用,android,android-bitmap,bitmapfactory,Android,Android Bitmap,Bitmapfactory,我试图将图像对象转换为位图,但它返回null image = reader.acquireLatestImage(); ByteBuffer buffer = image.getPlanes()[0].getBuffer(); byte[] bytes = new byte[buffer.capacity()]; Bitmap myBitmap = B
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);
这假设返回位图对象,但它返回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);
任何把戏。。还是想法
谢谢我认为您正在尝试解码一个空数组,您只需创建它,但决不将图像数据复制到它 您可以尝试:
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = buffer.array();
Bitmap myBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,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);
如果此缓冲区未由可访问的阵列支持,则ByteBuffer.Java@会引发UnsupportedOperationException。这不适用于我的nv21映像。这是否仅限于一种可能的图像格式?