使用 ZXing Reader 解码位图

发布于 2024-11-10 13:15:45 字数 2361 浏览 2 评论 0原文

我还无法使用 ZXing 解码 QR 码。我正在使用 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,hins) 处中断。我收到一个带有以下跟踪的 ​​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 试图告诉我什么。

非常感谢, 莎拉

不知道怎么回事,但读者从未收到过写信。最后,通过直接使用 Decoder 将 Reader 自己的源代码替换回函数中来工作。

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();
}

这暂时有效,谢谢!

I haven't yet been able to decode a QR code with ZXing. I'm using a BUG from buglabs.net but the problem doesn't seem to have anything to do with the hardware, but rather the format of the image.

This is what I have so far:

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");
    }

The code breaks at reader.decode(bitmap, hints). I'm getting a NullPointerException with the following trace:

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)

Not sure what the InputEventProvider is trying to tell me.

Thanks so much,
Sara

Not sure how, but Reader was never getting written to. Finally works by substituting the Reader's own source code back into the function, using Decoder directly instead.

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();
}

This works for now, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反话 2024-11-17 13:15:45

项目中有BUG的示例代码,由BUG人员贡献。请参阅错误/。虽然它已经很旧了。

QRCoder 是你的课程,不是吗?所以不知道这是BUG还是库的问题。

There is sample code for BUG in the project, contributed by the BUG guys. See bug/. It's quite old though.

QRCoder is your class, no? so I don't know if this is a problem with BUG or the library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文