java 显然没有理由的 Javax ImageIO IIOException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5713096/
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
Javax ImageIO IIOException for apparently no reason
提问by rownage
Hey all, I have a Java problem. For my senior research class, I'm pretty much finished but I just have to analyze some data in images I generated. I don't want to tag this as homework because it's not part of any required assignment...it's something I came up with on my own to collect results. I wrote a program that compares two images pixel by pixel. It does this for all .bmp files in two directories. Now, my program reads the filenames into a String array and I checked the values of all the filenames, so I know the directories and filenames are being accessed fine initially. Here's the problematic code:
大家好,我有一个 Java 问题。对于我的高级研究课,我已经快完成了,但我只需要分析我生成的图像中的一些数据。我不想把它标记为家庭作业,因为它不是任何必需作业的一部分……这是我自己想出来的东西来收集结果。我编写了一个程序,可以逐个像素地比较两个图像。它对两个目录中的所有 .bmp 文件执行此操作。现在,我的程序将文件名读入一个字符串数组,并且我检查了所有文件名的值,所以我知道最初访问目录和文件名是正常的。这是有问题的代码:
public static void main(String[]args) throws IOException
{
File actualDir = new File("C:\Users\Rowe\Desktop\testExpect");
String actualFiles[] = actualDir.list();
File expectedDir = new File("C:\Users\Rowe\Desktop\testExpect2");
String expectedFiles[] = expectedDir.list();
int[][] stats = new int[actualFiles.length][6]; // Holds all info
//Columns, Rows, Total, redMatches, shouldaBeenRed, badRed
for(int i = 0; i < actualFiles.length; i++)
{
BufferedImage actualImage = null;
System.out.println(actualFiles[i]); //THIS PRINTS PROPERLY
System.out.println(System.getProperty("user.dir")); //FOR TESTING
actualImage = ImageIO.read(new File("C:\Users\Rowe\Desktop\testExpect\"+actualFiles[i])); //ERROR HERE
BufferedImage expectedImage = null;
expectedImage = ImageIO.read(new File("C:\Users\Rowe\Desktop\testExpect2\"+expectedFiles[i])); //THIS IMAGE WORKS
...rest of code
...rest of code
Now, when I change the directories to be the same, the program runs, and detects all pixels to be 100% alike (as it should, so I know the program does what I want it to do). Here's the error:
现在,当我将目录更改为相同时,程序会运行,并检测所有像素为 100% 相似(应该如此,所以我知道程序会执行我想要的操作)。这是错误:
Exception in thread "main" javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at PixelCompare.main(PixelCompare.java:22)
Exception in thread "main" javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at PixelCompare.main(PixelCompare.java:22)
I've tried different directories to no avail. Could it be something about the .bmp files? What could make one set of BMPs read just fine and another set not work? I can open all the required files in other programs, so they're not corrupted. All properties appear to be the same. One directory was hand-made in Gimp (these read fine), and another was generated by a Java-based program. These can be read in Gimp, Paint, Photoshop, etc, but they won't read in my code.
我试过不同的目录无济于事。可能与 .bmp 文件有关吗?什么可以使一组 BMP 读取正常而另一组无法正常工作?我可以在其他程序中打开所有需要的文件,因此它们不会被损坏。所有属性似乎都相同。一个目录是在 Gimp 中手工制作的(这些很好读),另一个是由基于 Java 的程序生成的。这些可以在 Gimp、Paint、Photoshop 等中读取,但它们不会在我的代码中读取。
Any help is greatly appreciated, thanks!
非常感谢任何帮助,谢谢!
Edit: Forgot to use reverted code...I screwed with it then posted some bad version. Revised to show original problem with what is otherwise functional code. To further describe the problem: if you changed both directories to look in testExpect2 folder for the file list in expectedFiles[], it would run successfully. Also, the System.out.println(actualFiles[i]
prints the correct filename before the error occurs, so I know the correct file is being read into the String array.
编辑:忘记使用恢复的代码......我搞砸了然后发布了一些糟糕的版本。修改以显示其他功能代码的原始问题。进一步描述问题:如果您将两个目录都更改为在 testExpect2 文件夹中查找 expectedFiles[] 中的文件列表,它将成功运行。此外,在System.out.println(actualFiles[i]
错误发生之前打印正确的文件名,所以我知道正确的文件正在被读入字符串数组。
回答by sjr
new File("C:\Users\Rowe\workspace\Senior Research\testExpect"+expectedFiles[i])
Let's shorten the directory to C:\\yourDir
. Your code will be yielding paths like
让我们将目录缩短为C:\\yourDir
. 您的代码将产生类似的路径
C:\yourDirexpectedFiles1.bmp
Not what you want:
不是你想要的:
C:\yourDir\expectedFiles1.bmp
You forgot the path separator.
您忘记了路径分隔符。
It is much better to use the two-File-arg constructor to File
:
最好使用两个文件参数构造函数来File
:
File actualImageFile = new File(actualDir, expectedFiles[i]);
actualImage = ImageIO.read(actualImageFile);
Hope that helps!
希望有帮助!
回答by ZeissS
In the problematic line, shouldn't it been actualFiles[i]
instead of expectedFiles[i]
?
在有问题的行中,不应该actualFiles[i]
代替expectedFiles[i]
吗?