camera-samples icon indicating copy to clipboard operation
camera-samples copied to clipboard

CameraX providing Distorted bitmap when saving ImageProxy to bitmap.

Open HemanParbhakar opened this issue 2 years ago • 3 comments

When trying to save the bitmap from ImageProxy it save the bitmap as distorted but only in case of FrontCamera. we have tested on multiple devices the issues only appears in OnePlus6T on camera2 it was working properly. If we remove the imageCapture from bind to lifecycle then it start returning proper bitmap in OnepPlus6T devices as well. Changes done instead of setAspectRatio setTargetResolution is used where size is calculated using val metrics = windowManager.getCurrentWindowMetrics().bounds ImageAnalzyer now looks like this. imageAnalyzer = ImageAnalysis.Builder() .setTargetResolution(maxSize) .setTargetRotation(rotation) .setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_RGBA_8888) .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) .build() .also { it.setAnalyzer(cameraExecutor, this) } Bitmap is intialized bitmap = Bitmap.createBitmap(imageSize.width, imageSize.height, Bitmap.Config.ARGB_8888); Bitmap save is done in override analyze function bitmap!!.copyPixelsFromBuffer(image.image!!.planes[0].buffer); val bos = ByteArrayOutputStream() cameraBitmap!!.compress(Bitmap.CompressFormat.JPEG, 100, bos) val bitmapdata = bos.toByteArray()

Its happening on OnePlus 6T(A6010) Android Version : 11 Oxygen OS Version : 11.1.2.2 Build Number : ONEPLUS A6010_41_21125

HemanParbhakar avatar Jul 19 '22 03:07 HemanParbhakar

if the bitmap show like this photo

lf3Tp

just throwaway the old way converter and use this way ,it's work with custom size seletced

val myByteArray: ByteArray? = ImageUtil.imageToJpegByteArray(imageProxy)
val inputStream = ByteArrayInputStream(myByteArray)
val bitmap = BitmapFactory.decodeStream(inputStream) 

kareemalkoul avatar Jul 21 '22 14:07 kareemalkoul

It could be device-specific reason that rowStride doesn't equal to imageWidth on OnepPlus6T. So instead of using copyPixelsFromBuffer (doesn't consider rowStride), you could try createBitmap/setPixel or other Bitmap APIs.

e.g. Try this code snippt to see if it works

Bitmap targetBitmap = Bitmap.createBitmap(
        image.getWidth(),
        image.getHeight(),
        Bitmap.Config.ARGB_8888);

ByteBuffer byteBuffer =
        image.getImage().getPlanes()[0].getBuffer();
for (int i = 0; i < image.getHeight(); i++) {
    for (int j = 0; j < image.getWidth(); j++) {
        int index = i * image.getImage().getPlanes()[0].getRowStride() + j * 4;
        int R = byteBuffer.get(index);
        int G = byteBuffer.get(index + 1);
        int B = byteBuffer.get(index + 2);
        int A = byteBuffer.get(index + 3);
        int color =
                (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);
        targetBitmap.setPixel(j,  i, color);
    }
}

bbfeechen avatar Jul 21 '22 16:07 bbfeechen

You can use copyPixelsFromBuffer using plane properties instead of width:

fun Image.toBitmap(): Bitmap {
    val plane: Image.Plane = planes[0]
    val targetBitmap = Bitmap.createBitmap(plane.rowStride / plane.pixelStride, height, Bitmap.Config.ARGB_8888)
    targetBitmap.copyPixelsFromBuffer(plane.buffer)
    return targetBitmap
}

FrancescoSantagati avatar Oct 05 '22 15:10 FrancescoSantagati