javax.imageio.IIOException:无法读取输入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20166713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:39:48  来源:igfitidea点击:

javax.imageio.IIOException: Can't read input file

javabufferedimagejavax.imageio

提问by pacman4565

I am building a program that compresses a given image and saves it as a JPEG. This is the error message I get:

我正在构建一个压缩给定图像并将其保存为 JPEG 的程序。这是我收到的错误消息:

Exception in thread "main" javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at JPEGCompression.main(JPEGCompression.java:23) 

Here is my main:

这是我的主要内容:

public class JPEGCompression {

public static void main(String[] args) throws IOException{

    String imageFile = "/tmp/garden.png";
    BufferedImage i = ImageIO.read(new File(imageFile)); // this line produces the error
    showImage("Original Image", i);

    compressAndShow(i, 0.7f);


}

回答by user2718114

You have no checks for if the file exists / have permissions for the file, that would be my first debugging step.

您没有检查文件是否存在/是否具有该文件的权限,这将是我的第一个调试步骤。

Also try the following: String imageFile = "./tmp/garden.png";

还可以尝试以下操作: String imageFile = "./tmp/garden.png";

I always put a .before my slash to tell it I'm talking about the current directory, though I don't think it is required.

我总是.在我的斜线前面放一个,告诉它我在谈论当前目录,尽管我认为这不是必需的。

回答by yuanfang

I have also met this problem. The answer is that the model of the picture is wrong. So you should change the model of the picture from 'CMYK' TO 'RGB'. CMYK is for printer, and RGB is for computer. You can use photoshop or imageMagick to get it done.

我也遇到过这个问题。答案是图片的型号不对。因此,您应该将图片的模型从“CMYK”更改为“RGB”。CMYK 是打印机用的,RGB 是电脑用的。您可以使用 photoshop 或 imageMagick 来完成它。