4 android bitmapfactory android-bitmap
我正在尝试将图像对象转换为位图,但它返回 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);
Run Code Online (Sandbox Code Playgroud)
图像本身是jpeg图像,我可以将其保存到磁盘中,我想转换为位图的原因是因为我想在将其保存到磁盘之前进行最终旋转。挖掘类 BitmapFactory 我看到这一行。
bm = nativeDecodeByteArray(data, offset, length, opts);
Run Code Online (Sandbox Code Playgroud)
此行返回空值。使用调试器进一步挖掘
private static native Bitmap nativeDecodeByteArray(byte[] data, int offset,
int length, Options opts);
Run Code Online (Sandbox Code Playgroud)
这假设返回 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);
Run Code Online (Sandbox Code Playgroud)