1

I noticed a strange issue with Google's ML Kit Barcode scanner. I try to scan a QR code which has a green background, and it does not recognize the QR code at all. However if I crop the same QR code and remove the green background then it scans perfectly well. I don't put any code examples of my implementation here as the official example has the exact same issue. Here is my image. I don't even know how to research this problem as I totally don't understand what green background can do. enter image description here

Andranik
  • 2,729
  • 1
  • 29
  • 45
  • 1
    While for a different decoder, it may be required barcode margin, see: [Reduce border width on QR Codes generated by ZXing?](https://stackoverflow.com/q/10142748/295004) – Morrison Chang Feb 27 '22 at 03:01
  • Thanks @MorrisonChang. Yes adding a white margin with photoshop made this QR be scanned immediately, but I wanted to find a programatic solution to this, as there are QR scanners in play store which scan this QR without any issue even without the margin. – Andranik Mar 01 '22 at 08:45
  • 2
    If the color is known around a barcode then you could apply a color filter (i.e. green becomes white) before barcode processing. Note that local lighting conditions may make this inconsistent, and other barcode scanners may be doing other image preprocessing tricks to improve the image before decoding. I would check to see how ZXing performs and perhaps use that library if possible. I would also ask the creator of the barcode to add appropriate margins as they are part of the barcode specification. – Morrison Chang Mar 01 '22 at 09:25

1 Answers1

1

Well, after spending some time on trying to solve this problem with various image processing techniques etc. I found out that the solution is rather simple and was always there right in front of me.

So while building the image analyzer, there is a configuration function to set Target Resolution setTargetResolution(@NonNull Size resolution), which if not set explicitly is defaulting to 640x480, which is probably ok for general use cases related to image analyzer (otherwise I wonder why Google should pick this resolution as a default). And it's also OK for normal QR codes, but for problematic QRs like this it seems to mess thing up, so ML kit needs a higher resolution images for processing.

So just changing this default 640x480 to 1920x1440 immediately solved the issue, and the QR code without borders started to be scanned immediately and with very good performance. I tried other, smaller resolutions as well, tried on different high and low end devices with good and bad cameras, and came to this resolution, which seems to perform the best.

So currently me Image Analyzer builder looks like this, and it works just fine

private val analyzerUseCase = ImageAnalysis.Builder()
        .setTargetResolution(Size(1440, 1920))
        .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
        .build()
Andranik
  • 2,729
  • 1
  • 29
  • 45