I am trying to compare an embedded image located in a MP3 file with the exact same image saved as a JPG. If the images are identical then I would like to perform some further processing, however, when I compare the 2 images (RGB comparison) I keep getting false.
I am sure that the images are identical because I extracted the image from the same MP3 to originally create the JPG using the following code.
Artwork aw = tag.getFirstArtwork();
ByteArrayInputStream bis = new ByteArrayInputStream(aw.getBinaryData());
BufferedImage imgA = ImageIO.read(bis);
File outputfile = new File("expectedImage.jpg");
ImageIO.write(imgA, "jpg", outputfile);
After I ran that to get the image I just commented out that section, now I have the following code in place to compare the MP3 embedded image with the JPG
Extract the MP3 image and call the comparison method
try {
Artwork aw = tag.getFirstArtwork();
ByteArrayInputStream bis = new ByteArrayInputStream(aw.getBinaryData());
BufferedImage imgA = ImageIO.read(bis);
File expectedImageFile = new File("expectedImage.jpg");
BufferedImage imgB = ImageIO.read(expectedImageFile);
if(compareImages(imgA, imgB)) {
System.out.println("The Images Match.");
}else {
System.out.println("The Images Do Not Match.");
}
}
catch (IOException e) {
e.printStackTrace();
}
Compare The Images
The method fails when comparing the pixels for equality on the first pass through the loop.
public static boolean compareImages(BufferedImage imgA, BufferedImage imgB) {
// The images must be the same size.
if (imgA.getWidth() != imgB.getWidth() || imgA.getHeight() != imgB.getHeight()) {
return false;
}
int width = imgA.getWidth();
int height = imgA.getHeight();
// Loop over every pixel.
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// Compare the pixels for equality.
if (imgA.getRGB(x, y) != imgB.getRGB(x, y)) {
return false;
}
}
}
return true;
}