'How to create Bitmap from Android MediaImage in OUTPUT_IMAGE_FORMAT_RGBA_8888 format?

I am trying to use this new feature of CameraX Image Analysis (version 1.1.0-alpha08): using setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_RGBA_8888), images sent to the analyzer will have RGBA format.

See this for reference: https://developer.android.com/reference/androidx/camera/core/ImageAnalysis#OUTPUT_IMAGE_FORMAT_RGBA_8888

I need to turn the image sent to the analyzer into a Bitmap so that I can input it to a TensorFlow classifier. Without this new feature I would receive the image in the standard YUV_420_888 format then I would have to use one of the several solutions that can be googled in order to turn YUV_420_888 to RGBA then to Bitmap. Like this: https://blog.minhazav.dev/how-to-convert-yuv-420-sp-android.media.Image-to-Bitmap-or-jpeg/. I assume getting the Media Image directly in RGBA format should help me avoid implementing those painfull solutions (that I have actually tried and do not seem to work very well for me so far).

Problem is I don't know how to turn this RGBA Media Image into a Bitmap. I have noticed that calling mediaImage.getFormat() returns 1 which is not an ImageFormat value but a PixelFormat one, the one logically corresponding to RGBA_8888 format, which is in line with the documentation: "All ImageProxy sent to ImageAnalysis.Analyzer.analyze(ImageProxy) will have format PixelFormat.RGBA_8888".

I have tried this:

private Bitmap toBitmapRGBA(Image image) {
    Image.Plane[] planes = image.getPlanes();
    ByteBuffer buffer = planes[0].getBuffer();
    buffer.rewind();
    int size = buffer.remaining();
    byte[] bytes = new byte[size];
    buffer.get(bytes);
    Bitmap bitmapImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);
    return bitmapImage;
}

This returns null indicating the decodeByteArray does not work. (I notice the image has got only one plane).

private Bitmap toBitmapRGBA2(Image image) {
    Image.Plane[] planes = image.getPlanes();
    ByteBuffer buffer = planes[0].getBuffer();
    Bitmap bitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888);
    buffer.rewind();
    bitmap.copyPixelsFromBuffer(buffer);
    return bitmap;
}

This returns a Bitmap that looks noting but noise.

Please help! Kind regards Mickael



Solution 1:[1]

I actually found a solution myself, so I post it here if anyone is interested:

private Bitmap toBitmap(Image image) {
    Image.Plane[] planes = image.getPlanes();
    ByteBuffer buffer = planes[0].getBuffer();
    int pixelStride = planes[0].getPixelStride();
    int rowStride = planes[0].getRowStride();
    int rowPadding = rowStride - pixelStride * image.getWidth();
    Bitmap bitmap = Bitmap.createBitmap(image.getWidth()+rowPadding/pixelStride, 
    image.getHeight(), Bitmap.Config.ARGB_8888);
    bitmap.copyPixelsFromBuffer(buffer);
    return bitmap;
    }

Solution 2:[2]

if you want to process the pixel array further on without creating a bitmap object you can do something like this:

            val data = imageProxy.planes[0].buffer.toByteArray()
            val pixels = IntArray(data.size / imageProxy.planes[0].pixelStride) {
                var index = it * imageProxy.planes[0].pixelStride
                (data[index++].toInt() and 0xff.shl(16)) or
                    (data[index++].toInt() and 0xff).shl(8) or
                    (data[index++].toInt() and 0xff).shl(0) or
                    (data[index].toInt() and 0xff).shl(24)
            }

And then you can create bitmap this way:

        Bitmap.createBitmap(
            pixels,
            0,
            imageProxy.planes[0].rowStride / imageProxy.planes[0].pixelStride,
            imageProxy.width,
            imageProxy.height,
            Bitmap.Config.ARGB_8888
        )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MickaelD
Solution 2 Arkharov Andrei