简体   繁体   English

使用Zxing扫描图像中的多个条形码ByQuadrantReader:Nullpointer

[英]Scanning multiple barcodes in an Image with Zxing ByQuadrantReader : Nullpointer

Looks like a similar issue like this. 看起来像是类似的问题。 SO 所以

My requirement is to scan an image which is having multiple barcodes/qr codes on it. 我的要求是扫描上面有多个条形码/二维码的图像。 I'm using zxing 3.3.3 . 我正在使用zxing 3.3.3。

What I did. 我做了什么。

private static void scan(byte[] imageBytes) {
        BufferedImage image = ImageUtils.byteArrayToBufferedImage(imageBytes);

        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        ByQuadrantReader byQuadrantReader = new ByQuadrantReader(reader);
        GenericMultipleBarcodeReader multipleBarcodeReader = new GenericMultipleBarcodeReader(byQuadrantReader);
        Result[] results = multipleBarcodeReader.decodeMultiple(bitmap);

        foreach(Result result : results) {
                System.out.println(result.getText());
        }
} 

Here multipleBarcodeReader.decodeMultiple(bitmap) is throwing a NullPointerException. 在这里,multipleBarcodeReader.decodeMultiple(bitmap)抛出NullPointerException。 Its thrown from here. 它从这里抛出。

  private static void makeAbsolute(ResultPoint[] points, int leftOffset, int topOffset) {
        if (points != null) {
            for (int i = 0; i < points.length; i++) {
                 ResultPoint relative = points[i];
                 points[i] = new ResultPoint(relative.getX() + leftOffset, relative.getY() + topOffset);

its in com.google.zxing.multi.ByQuadrantReader.java Line num 110. I downloaded source and updated code to check for null before going in. 它位于com.google.zxing.multi.ByQuadrantReader.java行号110中。我下载了源代码并更新了代码,以检查是否为空。

    ResultPoint relative = points[i];
    if (relative != null) {
      points[i] = new ResultPoint(relative.getX() + leftOffset, relative.getY() + topOffset);
    }

Now its working fine. 现在工作正常。 Is it a bug or did i do something wrong ? 是错误还是我做错了什么? Btw its working fine when I'm not using the ByQuadrantReader. 顺便说一句,当我不使用ByQuadrantReader时,它的工作正常。 It gave me readings on 2 barcodes out of 6 in the image. 它使我可以读取图像中6个条形码中的2个条形码。 However by using ByQuadrantReader with above fix it gave me 3 readings( 2 barcodes and 1 qr) for the same image. 但是,通过在上述修复程序中使用ByQuadrantReader,可以为同一张图像提供3个读数(2个条形码和1个qr)。

我不确定结果何时可以为空(我忘记了),但是是的,根据您的请求请求,我们在这种情况下添加了防御性的空检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM