0

I am writing a program with Java, which is merging all pictures of several folders into only one folder. The program itself is working very good, but now, I want to check before merging a picture into the "overallfolder", if it is already in the folder. For example if the picture was in two or more folders in the same time, so I will not have duplicates in it.

It doesn't matter if the file name is the same, I want to prove if the picture itself is the same.

The following function in my program lists all files and folders in one folder:

private void listSubFolders(String path) {
    //System.out.println("Controll Folder: "+path); //DEBUG
    ArrayList<String> localPicturePaths = new ArrayList<String>();
    File[] files = new File(path).listFiles();

    if(files != null) {
        for(File file : files) {
            if(file.isDirectory()) {
                //System.out.println("Folder: "+file.getName()); //DEBUG
                listSubFolders(file.getAbsolutePath());
            } else if(file.isFile()) {
                if(containsIgnoreCase(getFileExtension(file), fileTypes)) {
                    //System.out.println("File: "+file.getName()+" in Folder "+file.getParent()); //DEBUG
                    localPicturePaths.add(file.getAbsolutePath());
                } else {
                    //System.out.println("Ignore File: "+file.getName()); //DEBUG
                }
            }
        }
    }

So, there is a function to go over all pictures who are already in the folder. What I need is a good working function to check for the next picture, if it's already is in the list of moved pictures. I am especially searching for an efficient way which isn't making the program hours of time slower.

Do you have any ideas?

Hemmelig
  • 803
  • 10
  • 22
  • 2
    Statistically speaking, the probability that 2 different images should have the same pixel color on 3 random points is 256^9. Basically you don't have to check every pixel if they are the same, checking for 3 and comparing them to each image at the same x-y coordinate, should be enough. – Mordechai Nov 11 '16 at 05:00
  • 1
    You could keep a set of md5-hashes from all moved files and check for the hash of the actual file. – Turo Nov 11 '16 at 05:44
  • @Turo Is File().hashCode() working in Java for images? Because I get different hashes for two files which are the same pictures (one is a copy of the other). – Hemmelig Nov 11 '16 at 06:43
  • @MouseEvent I will check that possibility later, if the hash method isn't working, because if I want to extend my program for more than only photos, the hash could be better. I will have it in mind! – Hemmelig Nov 11 '16 at 06:44
  • 1
    I meant an md5-Hash, see http://stackoverflow.com/questions/415953/how-can-i-generate-an-md5-hash – Turo Nov 11 '16 at 07:43
  • Thanks. From this question on, I came to this page, with very good code, working for all photo-types: https://sites.google.com/site/matthewjoneswebsite/java/md5-hash-of-an-image – Hemmelig Nov 12 '16 at 01:30

0 Answers0