Karthik Balakrishnan
Karthik Balakrishnan

Reputation: 4383

Image comparator returns false

So, I'm using a file sharing app on Android. It creates a duplicate copy which is uploaded to it's server.

PROBLEM The following code works for a duplicate copy I manually create. That is, I long press and copy the file into the same directory with a File Manager. Then my function returns true. When it compares the duplicate image due to the app and the original image, I get false.

MD5-checksums are different so that is out of the options.

CODE

    public boolean equals(Bitmap bitmap1, Bitmap bitmap2) {
        ByteBuffer buffer1 = ByteBuffer.allocate(bitmap1.getHeight()
                * bitmap1.getRowBytes());
        bitmap1.copyPixelsToBuffer(buffer1);

        ByteBuffer buffer2 = ByteBuffer.allocate(bitmap2.getHeight()
                * bitmap2.getRowBytes());
        bitmap2.copyPixelsToBuffer(buffer2);

        return Arrays.equals(buffer1.array(), buffer2.array());
    }

Here are the images :

Original image - original

Duplicate image created by the app - duplicate

My code currently returns false while comparing these two images. How do I get the code to return true?

Upvotes: 1

Views: 286

Answers (1)

mmgp
mmgp

Reputation: 19221

Your problem is due to artefacts created by JPEG compression, if you can always keep the images in PNG then your problem is most likely solved. If you can't do that, then you need a better algorithm to compare the images. This is exactly the same problem discussed at Comparing image in url to image in filesystem in python

For instance, running the algorithms mentioned in the earlier discussion, we get a similarity of more than 99%. With that similarity value, you can say the images are the same.

Upvotes: 1

Related Questions