获取ZXing Reader来解码位图

时间:2011-05-31 16:02:45

标签: java qr-code zxing

我还没能用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(位图,提示)中断。我得到一个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自己的源代码替换回函数,直接使用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();
}

现在可以使用了,谢谢!

1 个答案:

答案 0 :(得分:0)

项目中有BUG的示例代码,由BUG人员提供。见bug/。它虽然很老。

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