Java:使用Zxing解码QR码失败

时间:2018-09-25 07:59:04

标签: java qr-code zxing

我正在努力使用Google的Zxing Java库。我尝试解析几个QR码,它对其中一些非常有用。其他人无法识别,我无法说出原因。

这是我到目前为止所掌握的。

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码

enter image description here

0 个答案:

没有答案