Javax ImageIO IIOException for apparently no reason
Asked Answered
N

2

0

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

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

Nadinenadir answered 19/4, 2011 at 7:38 Comment(4)
I'm confused - I don't think that code would work in any case because it doesn't seem to be putting a backslash between the directory name and the filename, as it should.Comfortable
Other note: you're using expectedFiles[i] twice, and actualFiles[i] only for debug output. This is fishy.Humanize
are you sure expectedFiles[] contains relative paths?Fiddlestick
Hey guys, sorry that looks odd...I copied and pasted that from the version I'm screwing with now. It works when I add the double backslash to the end of the String...and I was using expectedFiles twice from the earlier testing when I was checking functionality in just one directory. Making changes to reflect that. Original problem still stands.Nadinenadir
Z
3
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!

Zinciferous answered 19/4, 2011 at 7:43 Comment(5)
That was just a bad copy and paste on my part. Everything is being accessed correctly up until I try to access the first BMP file for actualFiles, even though all files are properly listed in the actualFiles String array.Nadinenadir
Can you put assertions before your call to ImageIO? like... assert actualImageFile.isFile() && actualImageFile.canRead();Zinciferous
sjr, I don't know what the the heck that assert statement did to make my code work differently...but you're amazing. Should I do the same for the second ImageIO call to play it safe?Nadinenadir
The assert didn't change anything, just putting those "\\" back in did fix it for you though. ;)Zinciferous
sigh...I can't get why it worked for one and not the other this whole time, but whatever. It's working and you win teh prize.Nadinenadir
B
1

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

Baun answered 19/4, 2011 at 7:50 Comment(2)
Yep...wrong copy&paste on my end. Problem still stands, see comments. Thanks for the heads up!Nadinenadir
@rownage: Then try sjr's answer, please.Baun

© 2022 - 2024 — McMap. All rights reserved.