【问题标题】:com.google.zxing.NotFoundException exception comes when core java program executed?执行核心java程序时出现com.google.zxing.NotFoundException异常?
【发布时间】:2012-05-21 22:54:40
【问题描述】:

我有一个带有二维条码的 jpeg 文件。图像分辨率为 1593X1212。我正在使用 xing 库从图像中解码此条形码。我在网上得到了以下代码。

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

}

当我执行这个简单的核心 java 程序时,我给出了异常

com.google.zxing.NotFoundException

它甚至没有给出任何堆栈跟踪。

我想问专家,为什么会出现这种异常。 谢谢你!

【问题讨论】:

  • 你有解决这个问题的方法吗?我也面临同样的问题。请告诉我

标签: java exception barcode zxing


【解决方案1】:

这个解决方案对我有用。我希望这对你有帮助。我用reader.decodeWithState(...)替换reader.decode(...)

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

        Result result = reader.decodeWithState(bitmap);

【讨论】:

  • 如果不使用 decodeHints,为什么还要初始化呢?这让我浪费时间写一些我最终没有使用的东西
  • decodeWithState 成功检测到另一个图像中的二维码。谢谢!
【解决方案2】:

我已经在 ImageAnalysis 中调整了目标分辨率,它开始工作了。

来自

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

到这个

    ImageAnalysis imageAnalysis =
            new ImageAnalysis.Builder()
                    .setTargetResolution(new Size(700, 500))
                    .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
                    .build();

【讨论】:

    【解决方案3】:

    如果您使用,已经有此代码,

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

    更改此代码。其正常工作,

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

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题。当我在 Java SE 库上运行几乎完全相同的代码时,它就可以工作了。当我使用同一张图片运行 Android 代码时,它不起作用。花费大量时间试图找出...

      1. 问题:您必须将图片调整为更小。您不能直接使用智能手机图片。它太大了。在我的测试中,它适用于大约 200KB 的图片。

      您可以使用缩放位图

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

      1. 问题:您必须打开一些标志。玩弄这个解决方案对我有用的几乎所有标志:

        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库运行一次条码扫描,假设图片上的条码已经有正确的方向。如果不是这种情况,您必须运行它四次,每次将图片旋转 90 度左右!

      对于旋转,您可以使用此方法。 Angle 是以度为单位的角度。

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

      【讨论】:

        【解决方案5】:

        我遇到了同样的问题,我正在调用 readQRCode(filePath, charset, hintMap);并且得到了同样的信息。我正在调用我使用 zxing 库编写的库。要修复它,只需将 (zxing) jar 添加到您的顶级代码中,即使那里没有访问这些库。

        【讨论】:

        • 建议重新检查其类路径上的 JAR 不太可能帮助搜索此特定错误的人。与本主题无关
        【解决方案6】:
        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();
                    }
        

        【讨论】:

        • 我不知道为什么这是负面的,但这个答案对我有帮助。需要一个简单的 try catch 来确保使用的位图中有一个二维码。如果没有,并且使用了假图像,那么我们应该捕获错误并向用户显示一些消息。
        【解决方案7】:

        我遇到了同样的问题。我使用了一张我知道有有效 QR 码的图片,并且我还得到了 com.google.zxing.NotFoundException。

        问题是您用作源的图像太大,库无法解码。在我缩小图像尺寸后,二维码解码器工作了。

        对于我的应用来说,图像上的二维码总是或多或少在同一个区域,所以我使用了 BufferedImage 类的 getSubimage 函数来隔离二维码。

             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】:

          正常;这只是意味着没有找到条形码。你还没有提供图片,所以我不能说你的图片是否可读,更不用说支持的条形码格式了。

          【讨论】:

          • 如何“调整”准确性/搜索设置?有这方面的文件吗?当我有一个大的PNG(来自PDF),左上角有一个二维码,其他都是空白区域时,它找不到它......
          【解决方案9】:

          在图像中找不到条形码时会引发该异常:

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

          【讨论】:

          • 谢谢先生的回复!但是上面提供的图像包含一个二维条码,条码的分辨率约为 84Pix X 82pix。那为什么上面的代码没有找到图片上的条形码。
          • 您是否尝试过使用相同的代码但使用不同的条形码图像?是否有任何条形码示例图像可用于测试您的代码?
          • 是的,我已经更改了图像并重新执行了程序,但它仍然给出了相同的异常
          猜你喜欢
          • 2014-06-10
          • 2023-04-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-31
          相关资源
          最近更新 更多