【问题标题】:Get ZXing Reader to decode a bitmap获取 ZXing Reader 解码位图
【发布时间】:2011-09-05 15:05:52
【问题描述】:

我还不能用 ZXing 解码二维码。我正在使用来自 buglabs.net 的 BUG,但问题似乎与硬件无关,而与图像格式有关。

这是我目前所拥有的:

try {
    LuminanceSource source = new AWTImageLuminanceSource(bimage);
    bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
    Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
    hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    Result result = reader.decode(bitmap, hints) ;
    System.out.println("result is:" + result.getText());
    } catch (ReaderException re) {
            System.out.println("I can't find a barcode here");
    }

代码在 reader.decode(bitmap, hints) 处中断。我收到带有以下跟踪的 ​​NullPointerException:

java.lang.NullPointerException
    at qrcoder.QRCoder.shoot(QRCoder.java:152) // this is the camera shoot function
    at qrcoder.QRCoder.buttonEvent(QRCoder.java:89) // press button
    at com.buglabs.bug.input.pub.InputEventProvider.run(InputEventProvider.java:90)

不确定 InputEventProvider 试图告诉我什么。

非常感谢, 萨拉

不确定如何,但 Reader 从未被写入。最后,将 Reader 自己的源代码替换回函数中,直接使用 Decoder 代替。

private void shoot() throws IOException, NotFoundException, FormatException,     ChecksumException {
    // take the picture
    Hashtable hints = null;
    DecoderResult decoderResult;
    cameraLED.setLEDFlash(true);
    camera.grabPreview(buf);
    camera.grabPreview(buf);
    cameraLED.setLEDFlash(false);
    InputStream in = new ByteArrayInputStream(camera.grabFull());
    BufferedImage bImageFromConvert = ImageIO.read(in);
    LuminanceSource source = new BufferedImageLuminanceSource(bImageFromConvert);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    ResultPoint[] points;

      final Decoder decoder = new Decoder();
      DetectorResult detectorResult = new Detector(bitmap.getBlackMatrix()).detect(hints);
      decoderResult = decoder.decode(detectorResult.getBits(), hints);
      points = detectorResult.getPoints();
      Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.QR_CODE);

    if (result.getText() != null){
        System.out.println("result is:" + result.getText());
        }           
        else {System.out.println("bitmap is null");}     


    ic.repaint();
}

现在可以了,谢谢!

【问题讨论】:

    标签: java qr-code zxing


    【解决方案1】:

    项目中有BUG的示例代码,由BUG大佬提供。见bug/。不过已经很老了。

    QRCoder 是你的班级,不是吗?所以不知道是BUG还是库的问题。

    【讨论】:

    • 该库运行良好 - 我直接从 Bug Labs 人员那里得到它。我暂时没有使用相机,我正在将图像直接导入到函数中。
    • 终于通过用自己的源代码替换 Reader 来实现它。不知道怎么做,但读者从来没有被写过。
    猜你喜欢
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    相关资源
    最近更新 更多