AllLuckBased
AllLuckBased

Reputation: 188

Cannot find a image file that exists in java

I have written a function which takes in a BufferedImage and compares it to a pre-existing image in my hard drive checking if they are same or not.

public boolean checkIfSimilarImages(BufferedImage imgA, File B) {

    DataBuffer imgAdata = imgA.getData().getDataBuffer();
    int sizeA = imgAdata.getSize();

    BufferedImage imgB = null;
    try {
        imgB = ImageIO.read(B);
    } catch (IOException ex) {
        Logger.getLogger(SupportClass.class.getName()).log(Level.SEVERE, null, ex);
    }
    DataBuffer imgBdata = imgB.getData().getDataBuffer();
    int sizeB = imgBdata.getSize();

    if(sizeA == sizeB) {

        for(int i = 0; i < sizeA; i++) {

            if (imgAdata.getElem(i) != imgBdata.getElem(i)) {

                return false;
            }
        }
    }
    return true;
}

This throws IOException "Cant read input file". Idk why this is happening. I am calling the function like this...

while(support.checkIfSimilarImages(currentDisplay, new File(pathToOriginalImage)) == false) {

            System.out.println("Executing while-loop!");
            bot.delay(3000);
            currentDisplay = bot.createScreenCapture(captureArea);
        }

where,

String pathToOriginalImage = "‪‪‪‪C:\\Users\\Chandrachur\\Desktop\\Home.jpg";

I can see that the path is valid. But as I am testing it for File.exists() or File.canRead() or File.absoluteFile().exists() inside the checkIfSimilarImages function and everything is returning false.

I have researched my question here and tried out these suggestions:

It is not only for this location, I have tried a variety of other locations but in vain. Also it is not a problem where I have hidden file extensions and the actual file might be Home.jpg.jpg .

The only thing that might be is that permissions might be different. I dont really know how to verify this, but there is no reason it should have some permission which is not readable by java. It is just another normal jpg file.

Can it be because I am passing the file object reference into a function so in this process somehow the reference is getting modified or something. I just dont know. I am running out of possibilities to test for...

The whole stack trace is as follows:

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at battlesbot.SupportClass.checkIfSimilarImages(SupportClass.java:77)
    at battlesbot.AutomatedActions.reachHomeScreen(AutomatedActions.java:72)
    at battlesbot.BattlesBot.main(BattlesBot.java:22)
Exception in thread "main" java.lang.NullPointerException
    at battlesbot.SupportClass.checkIfSimilarImages(SupportClass.java:81)
    at battlesbot.AutomatedActions.reachHomeScreen(AutomatedActions.java:72)
    at battlesbot.BattlesBot.main(BattlesBot.java:22)
C:\Users\Chandrachur\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 11 seconds)

I am on Windows 10, IDE is NetBeans.

UPDATE: Huge thanks to @k5_ . He told me to paste this in path and it worked.

"C:/Users/Chandrachur/Desktop/Home.jpg";

It seems some invisible characters were in the path. But I still don't understand what that means.

Upvotes: 1

Views: 235

Answers (1)

k5_
k5_

Reputation: 5558

Usually this kind of problem lies with access problem or typos in the filename.

In this case there were some invisible unicode characters x202A in the filename. The windows dialog box, the file path was copied from, uses them for direction of writing (left to right).

One way of displaying them would be this loop, it has 4 invisible characters at the start of the String. You would also see them in a debugger.

    String x = "‪‪‪‪C:\\Users\\Chandrachur\\Desktop\\Home.jpg";
    for(char c : x.toCharArray()) {
        System.out.println( c + " " + (int) c);
    }

Upvotes: 1

Related Questions