如何在JAVA中加载/读取图像

huangapple go评论59阅读模式
英文:

How can I load/Read images in JAVA

问题

我正在尝试在Java中加载图像,但我遇到了这个错误:

javax.imageio.IIOException: 无法读取输入文件!
    	at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1308)
    	at Main.main(Main.java:10)

这是我的代码:

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Main {
    private static BufferedImage tmp;
    public static void main(String[] args) {
        try {
             tmp = ImageIO.read(new File("defaults.png"));
            System.out.println("读取完成");
        }catch (IOException e){
            System.out.println("加载图像时出错");
            e.printStackTrace();
        }
    }
}
英文:

I'm trying to load image in Java but I'm facing this error:

javax.imageio.IIOException: Can't read input file!
	at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1308)
	at Main.main(Main.java:10)

This is my code:

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Main {
    private static BufferedImage tmp;
    public static void main(String[] args) {
        try {
             tmp = ImageIO.read(new File("defaults.png"));
            System.out.println("reading completed ");
        }catch (IOException e){
            System.out.println("Error loading image ");
            e.printStackTrace();
        }
    }
}

答案1

得分: 2

这个特定的错误消息是在文件无法被读取时抛出的。请检查ImageIO.read()的源代码:

> if (!input.canRead()) {
> throw new IIOException("无法读取输入文件!");
> }

它使用File.canRead()方法来检查文件是否可读。该方法的文档说明如下:

> 返回值:
>
> 仅当由此抽象路径名指定的文件存在且可以被应用程序读取时,返回true;否则返回false

因此,您尝试加载的文件必须存在,并且权限必须正确。当File对象的exists()方法返回false时,您就知道您尝试加载的文件不存在(在您所在的工作目录中)。当文件确实存在时,这是一个权限问题,您的应用程序不被允许读取该文件。

英文:

This specific error message is thrown when, well, the file can't be read. Check the source code of ImageIO.read():

> if (!input.canRead()) {
> throw new IIOException("Can't read input file!");
> }

It uses the File.canRead() method to check if the file can be read. The documentation from that method say:

> Returns:
>
> true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise

So the file you are trying to load must exists and the permission must be correct. When the exists() method returns false for your File object you know that the file you are trying to load does not exists (in the working directory you are in). When the file does indeed exists, it is a permission issue that your application are not allowed to read the file.

答案2

得分: 1

在这种情况下,我建议:

  • 登出文件的绝对路径,以检查系统是否正在您认为的位置寻找它

  • 尝试使用较新的文件API调用之一(通过Files类提供),这些调用通常会提供更多关于为何无法读取文件的线索:

      File f = new File("defaults.png");
      System.out.println(f.getAbsolutePath());
    
      try {
          Path p = f.toPath();
          Files.readAllBytes(p);
      } catch (IOException ioex) {
          ioex.printStackTrace();
      }
    
英文:

In a case like this, I would suggest:

  • Log out the absolute path of the file, to check that the system is looking for it where you think it is looking for it

  • Try to read the file with one of the newer file API calls (available via the Files class) which will tend to give more clueful errors us to why the file cannot be read:

      File f = new File("defaults.png");
      System.out.println(f.getAbsolutePath());
    
      try {
          Path p = f.toPath();
          Files.readAllBytes(p);
      } catch (IOException ioex) {
          ioex.printStackTrace();
      }
    

huangapple
  • 本文由 发表于 2020年9月12日 05:13:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63854371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定