1

我必须从图像中读取二维数据矩阵条形码。我正在使用 zxing 读取条形码。这是我正在使用的代码。

import java.awt.image.BufferedImage;
import java.io.FileInputStream;
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 BarcodeGeneration {

    public static void main(String[] args) throws IOException {
        InputStream barCodeInputStream = new FileInputStream("file.jpg");  
        BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);  
        LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);  
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
        Reader reader = new MultiFormatReader();  
        Result result;
        try {
            result = reader.decode(bitmap);
            System.out.println("Barcode text is " + result.getText());
        } catch (NotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ChecksumException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  


    }

}

问题是我没有得到所有图像的输出。我从网上下载了一张工作正常的图片。但是对于实际的输入图像,我得到了“ com.google.zxing.NotFoundException ”异常,尽管它有数据。任何人都可以帮助克服这个问题或提供替代解决方案来读取 2D 数据矩阵。!

谢谢

图片:

4

1 回答 1

0

这是因为二维矩阵不在提取图像的中心。所以我使用 Image 类调整了图像的大小然后它工作了。

于 2017-08-25T09:03:17.080 回答