com.google.zxing.NotFoundException使用zxing解码qrcode

时间:2016-07-22 01:39:32

标签: java zxing

我们正在使用zxing从图像中解码qrcode,大多数qrcode通常可以从原始图像中提取,但有些则不能。我将展示解码器代码,我们将一起讨论导致NotFoundException的原因并找出解决方案。

首先,需要一些zxing依赖:

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

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

然后看详细代码:

 public static String decodeQrCode(BufferedImage image) throws DependencyServiceException
{
    // Convert the image to a binary bitmap source
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

    // Decode the barcode
    QRCodeReader reader = new QRCodeReader();
    Result result = null;
    try
    {
        result = reader.decode(bitmap);
    }
    catch (NotFoundException | ChecksumException | FormatException e)
    {
        throw new DependencyServiceException(e);
    }
    return result == null ? null : result.getText();
}

public static void main(String[] args)
{
    File file = new File("/tmp/ivan_qr_code.jpg");
    String qrCodeOriginUrlExtracted = "";
    try
    {
        FileInputStream imageInFile = new FileInputStream(file);
        byte imageData[] = new byte[(int) file.length()];
        imageInFile.read(imageData);

        String qrCodeBase64 = Base64.encodeBase64URLSafeString(imageData);
        BufferedImage image = Base64StringToImageUtil.generateImageFromString(qrCodeBase64);
        qrCodeOriginUrlExtracted = QrCodeDecoderUtil.decodeQrCode(image);
        imageInFile.close();
    }
    catch (Exception e)
    {
        // TODO: handle exception
    }
    System.out.println(String.format("Extracted text from qr code: %1$s", qrCodeOriginUrlExtracted));
}

生成错误:

Exception in thread "main"  {"errorCode": "3000", "debugInfo":"null","message": "com.google.zxing.NotFoundException"}
at com.waijule.common.util.image.QrCodeDecoderUtil.decodeQrCode(QrCodeDecoderUtil.java:44)
at com.waijule.common.util.image.QrCodeDecoderUtil.main(QrCodeDecoderUtil.java:58)

引起:com.google.zxing.NotFoundException

模板qr代码如下:

enter image description here

感谢所有的帮助。

1 个答案:

答案 0 :(得分:0)

总的来说,解码方法取决于具体情况,DecodeHintType和条形码阅读器需要事先指定。请参阅https://zxing.org/w/decode.jspxzxing的开源,注意方法&#39; processImage&#39;。

感谢追随者们。