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

时间:2012-05-14 12:45:32

标签: java exception barcode zxing

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

它甚至没有给任何stackstrace。

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

9 个答案:

答案 0 :(得分:8)

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

问题是您用作源的图像对于要解码的库来说很大。在缩小图像尺寸后,QR码解码器工作。

出于我的应用目的,图像上的QR码总是或多或少在同一区域,因此我使用BufferedImage类的getSubimage函数来隔离QR码。

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

答案 1 :(得分:6)

我遇到了同样的问题。当我在Java SE库上运行几乎完全相同的代码时,它可以工作。当我使用相同的图片运行Android代码时,它无法正常工作。花了很多时间试图找出......

  1. 问题:你必须调整图片的大小才能更小。您无法直接使用智能手机图片。它很大。在我的测试中,它使用了大约200KB的图片。
  2. 您可以使用

    缩放位图

    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度左右!

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

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

答案 2 :(得分:3)

如果图像中未找到条形码,则抛出该异常:

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

答案 3 :(得分:2)

这是正常的;它只是意味着没有找到条形码。您尚未提供图像,因此我无法确定您的图像是否具有可读性,更不用说支持条形码格式了。

答案 4 :(得分:0)

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

答案 5 :(得分:0)

如果您使用此代码,

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

答案 6 :(得分:0)

此解决方案对我有用。希望对您有所帮助。我将reader.decode(...)替换为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);

答案 7 :(得分:0)

我已经在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();

答案 8 :(得分:-1)

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