Search code examples
androiddrawingrectgoogle-mlkit

How to draw a rectangle with the real scale of the object?


I'm using the MLKit library for barcode scanning, so I can get a rectangle where is actually the barcode. But I'm getting a rectangle that is little than the barcode and it's not in the right position. So, the scale and the position of the rectangle are wrong.

Can anyone knows how to fix this problem? Is there any method that could re-scale and re-position the rectangle with the real measures?

Thank you :)

Here is my code:

override fun analyze(image: ImageProxy) {
            val img = image.image
            if (img != null) {
                val inputImage = InputImage.fromMediaImage(img, image.imageInfo.rotationDegrees)

                val scanner = BarcodeScanning.getClient()

                scanner.process(inputImage)
                    .addOnSuccessListener { barcodes ->
                        for (barcode in barcodes) {
                            val myRect = barcode.boundingBox
                            // here I draw the rectangle with canvas and paint
                            // ...
                            canvas.drawRect(myRect, mPaint) 
                            // Finish editing pixels in the surface.
                            mHolder.unlockCanvasAndPost(canvas)
                        }
                    }
             }
}

Solution

  • You can take a look at ML Kit sample which contains the CameraX scenario or check here to learn more about the coordinate transformation.