简体   繁体   中英

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 .

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. 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.

    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. It gave me readings on 2 barcodes out of 6 in the image. However by using ByQuadrantReader with above fix it gave me 3 readings( 2 barcodes and 1 qr) for the same image.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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