The com.google.zxing.NotFoundException exception occurs when the java kernel program starts?

I have a jpeg file with a 2D barcode. Image Resolution - 1593X1212. I am using the xing library to decode this barcode from an image. I got the following code online.

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 basic java program, I gave an exception

 com.google.zxing.NotFoundException 

He doesn't even give any stack.

I want to ask experts why such an exception occurs. Thanks you!

+8
java exception barcode zxing
source share
8 answers

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

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

For my application, the QR code on the image will always be more or less in the same area, so I used the getSubimage function of the BufferedImage class to highlight 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"; } 
+8
source share

I had the same problem. When I worked with almost the same code in Java SE libs, it worked. When I run the Android code using the same picture, it does not work. Spend many hours trying to figure out ...

  • problem: you need to resize the image to a smaller one. You cannot use the Smartphone image directly. He is big. In my test, it worked with a picture of about 200 KB.

You can scale the bitmap with

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

  1. Problem: you need to enable some flags. Playing around almost 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. : ZXing Android library launches a barcode scan once, assuming that the barcode in the picture already has the correct orientation. If this is not the case, you should run it four times, each time rotating the image by about 90 degrees!

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); } 
+6
source share

This exception is thrown if a barcode is not displayed in the image:

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

+3
source share

This is normal; it just means that the barcode is not found. You did not provide an image, so I can’t say whether your image is even readable, not to mention the supported barcode format.

+2
source share

I had the same problem, I called readQRCode (filePath, charset, hintMap); and received the same message. I called the library that I wrote using the zxing libraries. To fix this, simply add banks (zxing) to the top-level code, even if libraries are not available there.

0
source share

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(); } 

Make changes to this code. its compatibility

 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(); } 
0
source share

This solution works for me. I hope this helps you. I am replacing reader.decode(...) with reader.decodeWithState(...)

  MultiFormatReader reader = new MultiFormatReader();// use this otherwise Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>(); decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); decodeHints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE); decodeHints.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class)); Result result = reader.decodeWithState(bitmap); 
0
source share
 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(); } 
-one
source share

All Articles