Click here to Skip to main content
12,954,522 members (58,640 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
How can compare two images pixel by pixel using java
Posted 13-Mar-12 21:30pm
gavkk1.6K
Updated 17-May-16 23:46pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

If it can help you
Comparing Images using GDI+[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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();
    }
}
  Permalink  
v2

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,539
CHill60 3,490
Maciej Los 3,143
ppolymorphe 2,030
Jochen Arndt 1,975


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 10 Jun 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100