1

I am struggling with the Google’s Zxing Java library. I try to parse several QR-Codes, and it work great for some of them. Other are not recognized, and I can’t tell why.

Here is what I’ve got so far.

Pom.xml

<dependency>
   <groupId>com.google.zxing</groupId>
   <artifactId>core</artifactId>
   <version>3.3.3</version>
</dependency>
<dependency>
   <groupId>com.google.zxing</groupId>
   <artifactId>javase</artifactId>
   <version>3.3.3</version>
</dependency>

Test.java

public static String decodeQRCode(BufferedImage bufferedImage) throws NotFoundException {
    LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

    MultiFormatReader multiFormatReader = new MultiFormatReader();
    Map<DecodeHintType, Object> hints = new HashMap();
    hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    hints.put(DecodeHintType.POSSIBLE_FORMATS, Arrays.asList(BarcodeFormat.QR_CODE));
    hints.put(DecodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.name());
    multiFormatReader.setHints(hints);
    Result result = multiFormatReader.decode(bitmap, hints);

    return result.getText();
}

@Test
public void testQrCode() throws IOException, NotFoundException {
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    InputStream inputStream = classloader.getResourceAsStream("qrcode.png");
    BufferedImage bufferedImage = ImageIO.read(inputStream);
    String decodedText = decodeQRCode(bufferedImage);
    System.out.println("Decoded text = " + decodedText);
}

QR-Code

enter image description here

Jonas_Hess
  • 1,874
  • 1
  • 22
  • 32
  • 1
    `System.out.println("Decoded text = " + Arrays.toString(decodedText.getBytes(StandardCharsets.UTF_8)));` would give a reliable dump of the text, as System.out might use a limited OS encoding. Though I think you get a NotFoundException where the code should have been found. Blacken the middle small square. As QR is corrective, it then might be recognized; it might be that the small squares where erroneously identified as the three anchors. Though unlikely. – Joop Eggen Sep 25 '18 at 08:08
  • I just updated the library to version 3.3.3 This seems to fix the problem! But there are still other QR-Codes that it does not recognize. I will upload another QR-Code that still causes trouble. I try to get the middle block in another color. – Jonas_Hess Sep 25 '18 at 08:15
  • No need, here it those small squares looked too much like the anchors. – Joop Eggen Sep 25 '18 at 08:23
  • 1
    One could try a more permissive StandardCharsets.ISO_8859_1, should there be some for UTF-8 invalid text. More likely the error correction still is too hard for zxing. Often in QR codes there is an intentional "error" in order to draw a central icon. Also try zxing's online QR code analyzer. – Joop Eggen Sep 25 '18 at 08:42

0 Answers0