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.

rownage
  • 2,392
  • 3
  • 22
  • 31
  • 1
    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. – Robin Green Apr 19 '11 at 07:41
  • Other note: you're using `expectedFiles[i]` twice, and `actualFiles[i]` only for debug output. This is fishy. – Mat Apr 19 '11 at 07:45
  • are you sure expectedFiles[] contains relative paths? – andijcr Apr 19 '11 at 07:47
  • 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. – rownage Apr 19 '11 at 07:48

2 Answers2

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!

sjr
  • 9,769
  • 1
  • 25
  • 36
  • 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. – rownage Apr 19 '11 at 07:56
  • Can you put assertions before your call to ImageIO? like... `assert actualImageFile.isFile() && actualImageFile.canRead();` – sjr Apr 19 '11 at 07:58
  • 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? – rownage Apr 19 '11 at 08:10
  • The assert didn't change anything, just putting those "\\" back in did fix it for you though. ;) – sjr Apr 19 '11 at 08:11
  • 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. – rownage Apr 19 '11 at 08:13
1

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

ZeissS
  • 11,867
  • 4
  • 35
  • 50