com.google.zxing.NotFoundException exception comes when core java program executed?

Param-Ganak picture Param-Ganak · May 14, 2012 · Viewed 25.1k times · Source

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!

Answer

Stephan Bouwer picture Stephan Bouwer · Feb 18, 2013

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";
     }