com.google.zxing.NotFoundException 核心java程序执行时出现异常?

新手上路,请多包涵

我有一个包含二维条码的 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

它甚至不提供任何堆栈跟踪。

想请教高手,为什么会出现这样的异常。感谢您!

原文由 Param-Ganak 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2k
1 个回答

我有同样的问题。我使用了一张我知道有有效 QR 码的图像,我也得到了 com.google.zxing.NotFoundException。

问题是您用作源的图像对于库解码来说太大了。在我缩小图像尺寸后,QR 码解码器就可以工作了。

出于我的应用目的,图像上的二维码总是或多或少在同一区域,所以我使用 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";
     }

原文由 Stephan Bouwer 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题