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

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

问题描述

大家好,我有一个 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:无法读取输入文件!在 javax.imageio.ImageIO.read(未知来源)在 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!

忘记使用还原代码...我搞砸了然后发布了一些糟糕的版本.修改以显示其他功能代码的原始问题.进一步描述问题:如果您将两个目录都更改为在 testExpect2 文件夹中查找 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

您忘记了路径分隔符.

使用两个文件参数的构造函数来File要好得多:

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 IIOException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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