Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can compare two images pixel by pixel using java
Posted
Updated 17-May-16 23:46pm

If it can help you
Comparing Images using GDI+[^]
 
Share this answer
 
Java
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.PixelGrabber;

public class CompareImage 
{

    static void processImage()
    {
        String file1 = "C:\\Users\\jerald\\Desktop\\Colorful.jpg";
        String file2 = "C:\\Users\\jerald\\Desktop\\demo.jpg";
        
        Image image1 = Toolkit.getDefaultToolkit().getImage(file1);
        Image image2 = Toolkit.getDefaultToolkit().getImage(file2);

        try 
        {    
            PixelGrabber grab1 = new PixelGrabber(image1, 0, 0, -1, -1, false);
            PixelGrabber grab2 = new PixelGrabber(image2, 0, 0, -1, -1, false);

            int[] data1 = null;

            if (grab1.grabPixels()) 
            {
                int width = grab1.getWidth();
                int height = grab1.getHeight();
                data1 = new int[width * height];
                data1 = (int[]) grab1.getPixels();
            }

            int[] data2 = null;

            if (grab2.grabPixels()) {
                int width = grab2.getWidth();
                int height = grab2.getHeight();
                data2 = new int[width * height];
                data2 = (int[]) grab2.getPixels();
            }

            System.out.println("Pixels equal: " + java.util.Arrays.equals(data1, data2));

        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }

    public static void main(String args[]) {
        processImage();
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900