Medopal's answer will perform an approximate comparison for RGB images with no alpha chanel. The reason the comparison is approximate is because the RGB values are being "squashed" into a single integer, whereas the image colour resolution could be a lot higher than this.
To perform an exhaustive comparison you need to compare each image band separately:
BufferedImage image1 = ...
BufferedImage image2 = ...
Raster r1 = image1.getData();
Raster r2 = image2.getData();
boolean ret; // Stores result.
// Check image sizes and number of bands. If there are different
// then no need to compare images as they are definitely not equal.
if (r1.getNumBands() != r2.getNumBands() ||
r1.getWidth() != r2.getWidth() ||
r1.getHeight() != r2.getHeight()) {
ret = false;
} else {
// #Bands and image bounds match so compare each sample in turn.
ret = true;
search:
for (int i=0; i<r1.getNumBands(); ++i) {
for (int x=0; x<r1.getWidth(); ++x) {
for (int y=0; y<r1.getHeight(); ++y) {
if (r1.getSample(x, y, i) != r2.getSample(x, y, i) {
// At least one sample differs so result is false;
ret = false;
// Use labeled break to terminate all loops.
break search;
}
}
}
}
done:
}