使用 ZXing Reader 解码位图
我还无法使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
项目中有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.