首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >com.google.zxing.NotFoundException同时使用zxing解码qrcode

com.google.zxing.NotFoundException同时使用zxing解码qrcode
EN

Stack Overflow用户
提问于 2016-07-22 01:39:32
回答 1查看 3.7K关注 0票数 0

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

首先,需要一些zxing依赖项:

代码语言:javascript
复制
<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>

然后查看详细代码:

代码语言:javascript
复制
 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));
}

产生的错误:

代码语言:javascript
复制
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代码如下:

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-27 02:11:31

总的来说,解码方法取决于具体的环境,DecodeHintType和条形码读取器需要事先指定。请参考来自https://zxing.org/w/decode.jspx中兴开放源码,注意'processImage‘方法。

谢谢你这些天的追随者。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38516902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档