开发者

Javax ImageIO IIOException for apparently no reason

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 homewo开发者_Go百科rk 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

...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:

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.

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.


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:\\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 actualImageFile = new File(actualDir, expectedFiles[i]);
actualImage = ImageIO.read(actualImageFile);

Hope that helps!


In the problematic line, shouldn't it been actualFiles[i] instead of expectedFiles[i]?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜