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