16

I have a jpeg file which has 2D bar code. Image resolution is 1593X1212. I am using xing library to decode this barcode from image. I got following code on net.

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
    import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;


public class NewLibTest {
    public static void main(String args[]){
    System.out.println(decode(new File("E:\\xyz.jpg")));
    }

    /**
      * Decode method used to read image or barcode itself, and recognize the barcode,
      * get the encoded contents and returns it.
     * @param <DecodeHintType>
      * @param file image that need to be read.
      * @param config configuration used when reading the barcode.
      * @return decoded results from barcode.
      */
     public static String decode(File file){//, Map<DecodeHintType, Object> hints) throws Exception {
         // check the required parameters
         if (file == null || file.getName().trim().isEmpty())
             throw new IllegalArgumentException("File not found, or invalid file name.");
         BufferedImage image = null;
         try {
             image = ImageIO.read(file);
         } catch (IOException ioe) {
             try {
                throw new Exception(ioe.getMessage());
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
         }
         if (image == null)
             throw new IllegalArgumentException("Could not decode image.");
         LuminanceSource source = new BufferedImageLuminanceSource(image);
         BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
         MultiFormatReader barcodeReader = new MultiFormatReader();
         Result result;
         String finalResult = null;
         try {
             //if (hints != null && ! hints.isEmpty())
               //  result = barcodeReader.decode(bitmap, hints);
             //else
                 result = barcodeReader.decode(bitmap);
             // setting results.
             finalResult = String.valueOf(result.getText());
         } catch (Exception e) {
             e.printStackTrace();
           //  throw new BarcodeEngine().new BarcodeEngineException(e.getMessage());
         }
         return finalResult;
    }

}

When I executed this simple core java program I given exception

com.google.zxing.NotFoundException

Its not even gives any stackstrace.

I want to ask the experts that why such kind of exception is comming. Thanks You!

Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
Param-Ganak
  • 5,787
  • 17
  • 50
  • 62

9 Answers9

15

I had the same problem. When I was running nearly exactly the same code on the Java SE libs it worked. When I run the Android code using the same picture it didn't work. Spend a lot of hours trying to find out...

  1. problem: you have to resize the Picture to be more little. You can't use directly a Smartphone picture. It is to big. In my test it worked with a picture being about 200KB.

You can Scale a bitmap using

Bitmap resize = Bitmap.createScaledBitmap(srcBitmap, dstWidth,dstHeight,false);

  1. problem: You have to turn on some flags. Playing around with nearly all the flags this solution worked for me:

    Map<DecodeHintType, Object> tmpHintsMap = new EnumMap<DecodeHintType, Object>(
            DecodeHintType.class);
    tmpHintsMap.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    tmpHintsMap.put(DecodeHintType.POSSIBLE_FORMATS,
            EnumSet.allOf(BarcodeFormat.class));
    tmpHintsMap.put(DecodeHintType.PURE_BARCODE, Boolean.FALSE);
    

    ...

    MultiFormatReader mfr = null;
    mfr = new MultiFormatReader();
    result = mfr.decode(binaryBitmap, tmpHintsMap);
    
  2. problem: The Android library of ZXing run the barcode scan once, supposing the barcode on the picture already has the right orientation. If this is not the case you have to run it four times, each time rotation the picture around 90 degree!

For rotation you can use this method. Angle is the angle in degrees.

    public Bitmap rotateBitmap(Bitmap source, float angle)
    {
          Matrix matrix = new Matrix();
          matrix.postRotate(angle);
          return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    }
Ivan
  • 661
  • 1
  • 8
  • 15
techgeek89
  • 177
  • 1
  • 2
12

I had the same problem. I used an image that I knew had a valid QR code and I also got the com.google.zxing.NotFoundException.

The problem is that the image you use as a source is to large for the library to decode. After I reduced the size of my image the QR code decoder worked.

For the purpose of my application, the QR code on the image would always be more or less in the same area, so I used the getSubimage function of the BufferedImage class to isolate the QR code.

     BufferedImage image;
     image = ImageIO.read(imageFile);
     BufferedImage cropedImage = image.getSubimage(0, 0, 914, 400);
     // using the cropedImage instead of image
     LuminanceSource source = new BufferedImageLuminanceSource(cropedImage);
     BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
     // barcode decoding
     QRCodeReader reader = new QRCodeReader();
     Result result = null;
     try 
     {
         result = reader.decode(bitmap);
     } 
     catch (ReaderException e) 
     {
         return "reader error";
     }
Stephan Bouwer
  • 121
  • 1
  • 2
4

I have adjusted target resolution in ImageAnalysis and it started to work.

From

ImageAnalysis imageAnalysis =
        new ImageAnalysis.Builder()
                .setTargetResolution(new Size(mySurfaceView.getWidth(), mySurfaceView.getHeight()))
                .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
                .build();

to this one

    ImageAnalysis imageAnalysis =
            new ImageAnalysis.Builder()
                    .setTargetResolution(new Size(700, 500))
                    .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
                    .build();
trixo
  • 544
  • 4
  • 14
3

That exception is thrown when no barcode is found in the image:

http://zxing.org/w/docs/javadoc/com/google/zxing/NotFoundException.html

Colin D
  • 5,641
  • 1
  • 23
  • 35
  • 1
    Thank You Replying Sir! But the image provided above contains a 2D Barcode the resolution of the barcode is approximately 84Pix X 82pix. Then why the above code is not finding the barcode on image. – Param-Ganak May 14 '12 at 13:01
  • Have you tried using the same code but with a different barcode image? Are there any barcode sample images to test your code against? – Colin D May 14 '12 at 13:22
  • Yes I have changed the image an re-executed the program still it has been giving the same exception – Param-Ganak May 14 '12 at 13:30
2

It is normal; it just means no barcode was found. You haven't provided the image, so I can't say whether your image is even readable, let alone has a supported barcode format.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • How can you "tweak" with the accuracy/search settings? Is there documentation on that? When I have a large PNG (from a PDF) which has a qr-code in the top left corner, and all else is blank white space, it cannot find it... – Don Cheadle Feb 10 '15 at 16:34
1

Already this code if you use,

public static String readQRCode(String filePath, String charset, Map hintMap)
throws FileNotFoundException, IOException, NotFoundException {

    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
        new BufferedImageLuminanceSource(
            ImageIO.read(new FileInputStream(filePath)))));

    Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, tmpHintsMap);

    return qrCodeResult.getText();
}

public static String readQRCode(String filePath, String charset, Map hintMap)
throws FileNotFoundException, IOException, NotFoundException {

    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
        new BufferedImageLuminanceSource(
            ImageIO.read(new FileInputStream(filePath)))));

    Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, tmpHintsMap);

    return qrCodeResult.getText();
}

To make changes of this code. its conformly working,

public static String readQRCode(String filePath, String charset, Map hintMap)
throws FileNotFoundException, IOException, NotFoundException {
    Map < DecodeHintType, Object > tmpHintsMap = new EnumMap < DecodeHintType, Object > (
        DecodeHintType.class);

    //tmpHintsMap.put(DecodeHintType.TRY_HARDER, Boolean.FALSE);
    //tmpHintsMap.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));
    tmpHintsMap.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);

    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
        new BufferedImageLuminanceSource(
            ImageIO.read(new FileInputStream(filePath)))));

    Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, tmpHintsMap);

    return qrCodeResult.getText();
}
Ashish Patil
  • 1,624
  • 2
  • 11
  • 27
Dharma Raj
  • 11
  • 2
0
try {
                String a = textField_1.getText(); //my image path
                InputStream barCodeInputStream = new FileInputStream(""+a);
                BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);

                LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
                BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
                MultiFormatReader reader = new MultiFormatReader();
                com.google.zxing.Result result = reader.decode(bitmap);

                System.out.println("Barcode text is " + result.getText());
                textField.setText(""+result.getText());
            } catch (Exception e) {
                // TODO: handle exception
                JOptionPane.showMessageDialog(null, "This image does not contain barcode", "Warning", JOptionPane.WARNING_MESSAGE);
                e.printStackTrace();
            }
kamini
  • 11
  • I dont know why this got negative but this answer was helpful for me. a simple try catch is needed to ensure that the bitmap used has a qr code in it. if not and a fake image is used then we should catch the error and show some message to user. – Reza Nov 17 '20 at 14:37
0

I had the same problem, I was calling a readQRCode(filePath, charset, hintMap); and was getting the same message. I was calling a library I had written using the zxing libraries. To fix it just add the (zxing) jars to your top level code, even if the libraries are not accessed there.

  • 1
    suggesting to re-check the JAR's on their classpath is not likely to help someone searching for this specific error. It's not really relevant to this topic – Don Cheadle Feb 10 '15 at 16:15
0

This solution works for me. I hope this help you. I replace reader.decode(...) with reader.decodeWithState(...)

        MultiFormatReader reader = new MultiFormatReader();// use this otherwise

        Result result = reader.decodeWithState(bitmap);
Harun
  • 667
  • 7
  • 13