无法读取程序中的输入文件来读取QR码

时间:2012-04-16 10:34:04

标签: java qr-code

我正在尝试让程序读取QR码,但是当我的代码运行时,我收到异常javax.imageio.IIOException: Can't read input file。该图像位于 src 目录中。有人可以帮我在我的代码中找到问题......

public class BarcodeSample {    

    public static void main(String[] args) {
        Reader reader = new MultiFormatReader();

        try {
            BufferedImage image = ImageIO.read(new File("src/img.png"));
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

            Result result = reader.decode(bitmap);

            BarcodeFormat format = result.getBarcodeFormat();
            String text = result.getText();

            ResultPoint[] points = result.getResultPoints();
            for (int i=0; i < points.length; i++) {
                System.out.println("    Point[" + i + "] = " + points[i]);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先,使用File.separator代替'/',因为它会根据运行的操作系统放置正确的分隔符。

现在问题在于src/img.png。我建议你把你的图像放在src目录之外,因为这个目录用于代码(不是必须的)。

我不知道你在哪个IDE上运行它,但确保你的工作空间当前目录设置为你的项目根目录,这样才能找到src/img.png(假设src在你当前目录下面) ,否则你将得到文件未找到异常

相关问题