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 :
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)); } }
0 comments:
Post a Comment