Javax ImageIO IIO例外显然没有理由 [英] Javax ImageIO IIOException for apparently no reason

查看:126
本文介绍了Javax ImageIO IIO例外显然没有理由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有,我有一个Java问题。对于我的高级研究班,我已经完成了,但我只需要分析我生成的图像中的一些数据。我不想把它标记为作业,因为它不是任何必要任务的一部分......这是我自己想出来收集结果的东西。我写了一个程序,逐个像素地比较两个图像。它为两个目录中的所有.bmp文件执行此操作。现在,我的程序将文件名读入一个String数组,并检查了所有文件名的值,所以我知道目录和文件名最初是正常访问的。这是有问题的代码:

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:

    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

...其余代码

现在,当我将目录更改为相同时,程序会运行,并检测到所有像素都是100%相似(因为它应该如此,所以我知道程序做我想做的事情)。这是错误:

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:

线程main中的异常javax.imageio.IIOException:无法读取输入文件!
at javax.imageio.ImageIO.read(Unknown Source)
at PixelCompare.main(PixelCompare.java:22)

我尝试了不同的目录无济于事。它可能是关于.bmp文件的吗?什么可以使一组BMP读得很好而另一组不起作用?我可以在其他程序中打开所有必需的文件,因此它们没有被破坏。所有属性看起来都是一样的。一个目录是在Gimp中手工制作的(这些读取正常),另一个目录是由基于Java的程序生成的。这些可以在Gimp,Paint,Photoshop等中读取,但它们不会在我的代码中读取。

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.

非常感谢任何帮助,谢谢!

Any help is greatly appreciated, thanks!

编辑:忘记使用恢复的代码......我搞砸了它然后发布了一些不好的版本。修改以显示其他功能代码的原始问题。要进一步描述问题:如果您更改了两个目录以在expectedExpect2文件夹中查找expectedFiles []中的文件列表,它将成功运行。此外, System.out.println(actualFiles [i] 在错误发生之前打印正确的文件名,因此我知道正在将文件读入String数组。

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.

推荐答案

new File("C:\\Users\\Rowe\\workspace\\Senior Research\\testExpect"+expectedFiles[i])

让我们将目录缩短为 C:\\yourDir 。您的代码将产生路径,如

Let's shorten the directory to C:\\yourDir. Your code will be yielding paths like

C:\\yourDirexpectedFiles1.bmp

不是你想要的:

C:\\yourDir\\expectedFiles1.bmp

您忘记了路径分隔符。

使用两个文件更好arg构造函数到文件

It is much better to use the two-File-arg constructor to File:

File actualImageFile = new File(actualDir, expectedFiles[i]);
actualImage = ImageIO.read(actualImageFile);

希望有所帮助!

这篇关于Javax ImageIO IIO例外显然没有理由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆