Search code examples
javabarcodezxing

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.


Solution

  • I'm not sure when the result points can be null (I forget), but yeah per your pull request we added a defensive null check for this case.