0

The Mlkit barcode scanning library has an issue with scanning Code 39 barcodes. So I have to replace it with the zxing library that seems to have no issues.

However CameraX is giving me back an ImageProxy object, and the zxing MultiFormatReader accepts a BinaryBitmap object.

I'm having trouble converting from one to the other, and it's giving me an index out of bounds exception. Here's my code:

private fun createBinaryBitmap(imageProxy: ImageProxy): BinaryBitmap {
        val data = ImageUtil.yuvImageToJpegByteArray(imageProxy, imageProxy.cropRect, 100)

        val source = PlanarYUVLuminanceSource(
            data,
            imageProxy.width,
            imageProxy.height,
            0,
            0,
            imageProxy.width,
            imageProxy.height,
            false
        )
        return BinaryBitmap(HybridBinarizer(source))
    }

I've looked at other places and questions, but I honestly can't find the right approach.

Elyes Mansour
  • 126
  • 1
  • 6

1 Answers1

0

Here is what I found in the zxing doc. Arguments dataWidth and dataHeight are different of respectively imageProxy.width and imageProxy.height, since data is a cropped image of imageProxy.

public PlanarYUVLuminanceSource(byte[] yuvData,
                                    int dataWidth,
                                    int dataHeight,
                                    int left,
                                    int top,
                                    int width,
                                    int height,
                                    boolean reverseHorizontal)
SudoKoach
  • 366
  • 1
  • 6
  • Makes sense that they're different, thanks. However the crop rect here is actually equal to the full size when I was testing, so no cropping was happening. – Elyes Mansour Dec 05 '22 at 12:35
  • Are you rotating the image? If the camera is in the Portrait position the ImageProxy is in the Landscape position and you must rotate the image to process it. I've experienced the same exception message "Index out of bounds" when I coded a rotation of an image. – SudoKoach Dec 05 '22 at 16:38
  • I'll try that then. Do you know a good to rotate the image before coding it into a byte array ? – Elyes Mansour Dec 05 '22 at 19:05
  • If you haven’t specifically coded an image rotation, the issue is not there! Are you sure the function "ImageUtil.yuvImageToJpegByteArray(imageProxy, imageProxy.cropRect, 100)" returns a yuvData image of the same dimensions of imageProxy? – SudoKoach Dec 06 '22 at 09:39
  • Could you provide a sample code 39 image that "The Mlkit barcode scanning library has an issue with scanning Code 39 barcodes" while "zxing library that seems to have no issues."? Thanks. – Julie Zhou Dec 08 '22 at 21:23