2 Replies Latest reply on Mar 27, 2013 8:33 AM by 999429

    Image comparison in java

      Currently I need a piece of code to compare 2 images and return a % match. I have written a sample and was wondering is its ok or there are better ways to do. Please let me know of any jars for this that can be used in java. I tried googling it but did not find anything straightforward. Iam new to this area so any help is highly appreciated.

      The code :

      import java.awt.image.BufferedImage;
      import java.awt.image.Raster;
      import java.io.File;
      import java.io.IOException;
      import java.net.MalformedURLException;
      import java.net.URL;

      import javax.imageio.ImageIO;
      import net.coobird.thumbnailator.Thumbnails;

      public class ImageUtil {
           public static void main(String[] args) {
                boolean result = false;
                try {
                     result = compareImage(
                               new URL(
                               new URL(
                } catch (MalformedURLException e) {
                     // TODO Auto-generated catch block

           public static boolean compareImage(URL img1, URL img2) {
                boolean ret = false; // Stores result.
                int passed=0,failed=0,percentage=0;
                try {

                     BufferedImage image1 = ImageIO.read(img1);
                     BufferedImage image2 = ImageIO.read(img2);

                     image1=Thumbnails.of(image1).forceSize(image2.getWidth(), image2.getHeight()).asBufferedImage();

                     Raster r1 = image1.getData();
                     Raster r2 = image2.getData();

                     // Check image sizes and number of bands. If there are different
                     // then no need to compare images as they are definitely not equal.

                     System.out.println("image1 band" + r1.getNumBands()
                               + " image2 band" + r2.getNumBands() + " image1 width"
                               + r1.getWidth() + " image2 width" + r2.getWidth()
                               + " image1 height" + r1.getHeight() + " image2 height"
                               + r2.getHeight());
                     if (r1.getNumBands() != r2.getNumBands()     ||r1.getWidth() != r2.getWidth()
                               || r1.getHeight() != r2.getHeight()) {
                          ret = false;
                          System.out.println("Primary comparison failure");
                          // image2.setData(r1);


                     else {
                          // #Bands and image bounds match so compare each sample in turn.
                          ret = true;
                          int numBands=0;
                          if( r1.getNumBands()>r2.getNumBands()){
                          search: for (int i = 0; i < numBands; ++i) {
                               for (int x = 0; x < r2.getWidth(); ++x) {
                                    for (int y = 0; y < r2.getHeight(); ++y) {

                                         if (r1.getSample(x, y, i) != r2.getSample(x, y, i)) {
      //                                        // At least one sample differs so result is
      //                                        // false;
      //                                        ret = false;
      //                                        System.out.println("sample difference");
      //                                        // Use labeled break to terminate all loops.
      //                                        break search;

                     percentage= (passed* 100)/(passed+failed);
                     return ret;
                } catch (IOException e) {
                     // TODO Auto-generated catch block
                return ret;
        • 1. Re: Image comparison in java
          I tried googling it but did not find anything straightforward
          that's because this is not a straightforward problem in the sense that it is a common requirement, you are doing something only a very small percentage of people in the world want to do so there are likely no big ongoing projects to make an easy to use API for it; nobody would really profit from that. Research/university environments is more a source of this kind of material. Google "java image compare" - that is what you have to work with. Good luck.
          • 2. Re: Image comparison in java
            Yes, I did look for java image compare in google. I found explanation to some algorithms but iam not so comfortable in java to implement those in code. I thought I could use some available jars.