如何将android.media.Image转换为位图对象? [英] How to convert android.media.Image to bitmap object?
问题描述
在android中,我从此处 https://inducesmile .com/android/android-camera2-api-example-tutorial/此相机教程.但是我现在想遍历像素值,有人知道我该怎么做吗?我需要将其转换为其他东西吗?
In android, I get an Image object from here https://inducesmile.com/android/android-camera2-api-example-tutorial/ this camera tutorial. But I want to now loop through the pixel values, does anyone know how I can do that? Do I need to convert it to something else and how can I do that?
谢谢
推荐答案
如果要循环遍历整个像素,则需要先将其转换为Bitmap
对象.现在,由于我在源代码中看到它返回了Image
,因此您可以将字节直接转换为位图.
If you want to loop all throughout the pixel then you need to convert it first to Bitmap
object. Now since what I see in the source code that it returns an Image
, you can directly convert the bytes to bitmap.
Image image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
Bitmap bitmapImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);
然后,一旦获得位图对象,就可以遍历所有像素.
Then once you get the bitmap object, you can now iterate through all of the pixels.
这篇关于如何将android.media.Image转换为位图对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!