15

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

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

我想请教专家,为什么会出现这种异常。感谢您!

4

9 回答 9

12

我有同样的问题。我使用了一张我知道有有效 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";
     }
于 2013-02-18T12:04:07.757 回答
11

我有同样的问题。当我在 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 度左右!

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

    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);
    }
于 2015-05-08T17:17:54.100 回答
3

That exception is thrown when no barcode is found in the image:

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

于 2012-05-14T12:55:03.313 回答
2

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

于 2012-05-14T13:59:58.493 回答
1

我已经在 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();
于 2020-11-23T12:16:59.310 回答
0
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();
            }
于 2013-12-27T10:24:09.617 回答
0

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

于 2014-05-27T15:15:53.027 回答
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();
}
于 2016-09-02T05:32:06.637 回答
0

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

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

        Result result = reader.decodeWithState(bitmap);
于 2019-04-18T09:15:13.940 回答