无法在程序中读取输入文件以读取QR码

问题描述:

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

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

首先,使用File.separator代替'/'因为它根据它正在运行的OS将正确的分隔符。

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

我不知道哪个IDE,你运行它,但要确保您的工作空间当前目录设置到项目的根目录,以便src/img.png会被发现(假设src是在你根当前目录),否则你将得到的文件没有发现异常

+0

最后,我可以解决这个问题。 (1)我把我的图像放在src目录之外。 (2)开始时,我制作了一个大尺寸的QR码并试图读取它。但不能。 然后我用一个小尺寸的QR代替它,并可以阅读。但我不知道为什么。 非常感谢您的帮助! – Benben 2012-04-16 11:52:50