Search code examples
javaandroidkotlinandroid-cameraxgoogle-mlkit

Android CameraX cropping media.Image?


I am using CameraX for capturing images then feeding into mlkit scanner to scan barcodes, I have created a crop rect and applied to the media.image but it doesn't seem cropping the frame...

@androidx.camera.core.ExperimentalGetImage
override fun analyze(imageProxy: ImageProxy) {
    imageProxy.image ?. let {

        val rotationDegrees = imageProxy.imageInfo.rotationDegrees
        val imageHeight = it.height
        val imageWidth = it.width

        val cropRect = Rect(0, 0, imageWidth, imageHeight)
        cropRect.inset(
            (imageWidth/ 2),
            (imageHeight/ 2)
        )

        it.cropRect = cropRect

        val image = InputImage.fromMediaImage(it, imageProxy.imageInfo.rotationDegrees)
    }
}

I tried saving the media.Image fed to scanner to a bitmap and that is not cropped too.. Let me know what I am doing wrong.

I checked this answer but it is explaining with bitmaps and I am working with media.Image

Android: How to crop images using CameraX?


Solution

  • ImageProxy.setCropRect()is just metadata that is passed along with the Image, and it is up to the consumer of the Image to apply the crop rect while accessing the pixels. ImageProxy.setCropRect() does not affect the crop rect of the underlying android.media.Image, it only changes the crop rect metadata for the ImageProxy instance.