【问题标题】:com.google.zxing.NotFoundException while using zxing to decode qrcode使用zxing解码二维码时出现com.google.zxing.NotFoundException
【发布时间】:2016-07-27 10:11:31
【问题描述】:

我们正在使用zxing从图像中解码二维码,大部分二维码通常可以从原始图像中提取,但有些不能。我将展示解码器代码,我们是否应该一起讨论导致 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

提供的模板二维码如下:

感谢所有的小帮助。

【问题讨论】:

    标签: java zxing


    【解决方案1】:

    整体而言,解码方式视具体情况而定,DecodeHintType和条码阅读器需要提前指定。从zxing参考https://zxing.org/w/decode.jspx的开源,注意方法'processImage'。

    感谢这几天的关注。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多