繁体   English   中英

Android:将图像对象转换为位图不起作用

[英]Android: Convert image object to bitmap does not work

我试图将图像对象转换为位图,但它返回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);

我认为你正在尝试解码一个空数组,你只是创建它但从不将图像数据复制到它。

你可以试试:

ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = buffer.array();
Bitmap myBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,null);

如你所说它不起作用,那么我们需要手动复制缓冲区...试试这个:)

    byte[] bytes = new byte[buffer.remaining()];
    buffer.get(bytes);
    Bitmap myBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,null);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM