Monday, March 30, 2015

Compare images are same by java

We can compare the given images are same or not by comparing the buffer data of the image.

1. Compare the image sizes are same or not.
2. Compare the binary data of two images are same or not.

sample code :

import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.io.File;
import javax.imageio.ImageIO;

/**
 *
 * @author Uttesh Kumar T.H.
 */
public class compareimage {

    public static boolean compareImage(File fileA, File fileB) {
        try {
            // take buffer data from botm image files //
            BufferedImage biA = ImageIO.read(fileA);
            DataBuffer dbA = biA.getData().getDataBuffer();
            int sizeA = dbA.getSize();
            BufferedImage biB = ImageIO.read(fileB);
            DataBuffer dbB = biB.getData().getDataBuffer();
            int sizeB = dbB.getSize();
            // compare data-buffer objects //
            if (sizeA == sizeB) {
                for (int i = 0; i < sizeA; i++) {
                    if (dbA.getElem(i) != dbB.getElem(i)) {
                        return false;
                    }
                }
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            System.out.println("Failed to compare image files ...");
            return false;
        }
    }

    public static void main(String[] args) {
        File file1 = new File("path to image1");
        File file2 = new File("path to image2");
        System.out.println("result :" + compareImage(file1, file2));
    }
}

Related Posts:

  • Reverse elements in ArrayReverse all elements in array, traditional way how we use to do is get the array and iterate through the for loop and use swap logic to move to temporary array and get the result. But from jdk 1.5 introduced reverse() in ja… Read More
  • find character uppercase/lowercase count from given textFind the character upper/lower case occurrence count with simple single line code without iteration in java. int counta = text.split("(?=[a])").length - 1; This simple code will find all the lowercase character 'a… Read More
  • Spring + JPA + Hibernate Sample IntroductionStand-alone Spring application sample, Spring framework supports the persistence layer to be fully implemented through JPA. Simple example of configure the Spring needed components to perform persistence over sta… Read More
  • Generate PDF page as imageWe can generate the image of pdf page by using the Pdf-renderer Simple class which generate the images file of given pdf file. /* * To change this license header, choose License Headers in Project Properties. * To change … Read More
  • Java get system environment variable detailsGet System environment variable details. import java.util.Map; /** * * @author Uttesh Kumar T.H. */ public class FindJdk { public static void main(String[] args) { Map<String, String> env = System.gete… Read More

0 comments:

Post a Comment