How to convert android.media.Image to bitmap object?

前端 未结 5 793
余生分开走
余生分开走 2020-12-02 23:34

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 pi

5条回答
  •  爱一瞬间的悲伤
    2020-12-03 00:11

    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.

提交回复
热议问题