1

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.

AllLuckBased
  • 157
  • 1
  • 14
  • use double slashes instead of single ones – Iłya Bursov Aug 07 '18 at 16:06
  • or use single forward slash (/), Java will convert to your OS. – PeterMmm Aug 07 '18 at 16:07
  • I tried /, //, \\ but no difference – AllLuckBased Aug 07 '18 at 16:10
  • 1
    `I have hidden file extensions and the actual file might be Home.jpg.jpg` , did you try your path with filename `Home.jpg.jpg` ? – PeterMmm Aug 07 '18 at 16:13
  • Tried just now and it did not work. Moreover, I got the file path by right clicking then properties and the first thing under security tab (windows). It has the full path without any confusions like that. So I am pretty sure that the path is correct. – AllLuckBased Aug 07 '18 at 16:20
  • Can you post the whole stack trace? – Dan W Aug 07 '18 at 16:28
  • and all code, you enter `while` and it is not clear that `currentDisplay` is not null. – PeterMmm Aug 07 '18 at 16:30
  • Check if the folder is actually readable: `System.out.println(Files.isReadable(Paths.get("C:/Users", "Chandrachur"))); System.out.println(Files.isReadable(Paths.get("C:/Users", "Chandrachur", "Desktop")));System.out.println(Files.isReadable(Paths.get("C:/Users", "Chandrachur", "Desktop", "Home.jpg")));` – k5_ Aug 07 '18 at 16:30
  • @PeterMmm, currentDisplay is a buffered image variable and it has nothing to do with my error. My error is arising while trying to read File B... – AllLuckBased Aug 07 '18 at 16:33
  • @k5_ Tested it in the main function. All of those files returned true in the console there. – AllLuckBased Aug 07 '18 at 16:38
  • @DanW, added the whole stack trace – AllLuckBased Aug 07 '18 at 16:42
  • It throws `IIOException` ( != IOException), maybe this helps https://stackoverflow.com/questions/7177655/java-imageio-iioexception-unsupported-image-type – PeterMmm Aug 07 '18 at 16:48
  • After copying the file String, i got 4 invisible characters between the first quote and C. Can you make sure you dont have them (just copy `"C:/Users/Chandrachur/Desktop/Home.jpg";` ) – k5_ Aug 07 '18 at 16:53
  • wow, OMG, what was that. @k5_ you just solved my problem. Now it is no longer throwing the exception. Please explain what is this invisible character thing that you are talking about. I did not understand how it just worked. Thanks again... – AllLuckBased Aug 07 '18 at 17:04

1 Answers1

1

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);
    }
AllLuckBased
  • 157
  • 1
  • 14
k5_
  • 5,450
  • 2
  • 19
  • 27