Android:将图像对象转换为位图不起作用 [英] Android: Convert image object to bitmap does not work

查看:59
本文介绍了Android:将图像对象转换为位图不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像对象转换为位图,但它返回null.

I am trying to convert image object to bitmap, but it return 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中进行挖掘,我看到了这一行.

The image itself is jpeg image, I can save it to the disk fine, the reason I want to convert to bitmap is because I want to do final rotation before saving it to the disk. Digging in the Class BitmapFactory I see this line.

bm = nativeDecodeByteArray(data, offset, length, opts);

此行返回null. 用调试器进一步挖掘

This line return null. Further Digging with the debugger

private static native Bitmap nativeDecodeByteArray(byte[] data, int offset,
            int length, Options opts);

这应该返回Bitmap对象,但它返回null.

This suppose to return Bitmap object but it return null.

有什么花招..或想法吗?

Any tricks.. or ideas?

谢谢

推荐答案

您尚未复制字节.您已检查容量,但未复制字节.

You haven't copied bytes.You checked capacity but not copied bytes.

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

这篇关于Android:将图像对象转换为位图不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆