2

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?

All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111
  • Hi, you can look at my solution that I gave here https://stackoverflow.com/a/67348548/13300615. It should help you to crop image. – Alex F. May 02 '21 at 09:56
  • 1
    Does this answer your question? [Is there a way to crop Image/ImageProxy (before passing to MLKit's analyzer)?](https://stackoverflow.com/questions/63390243/is-there-a-way-to-crop-image-imageproxy-before-passing-to-mlkits-analyzer) – momvart Jun 28 '22 at 16:15

1 Answers1

4

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.

Chenxi Song
  • 557
  • 3
  • 6