public static void main(final String[] args) throws Exception {
final BufferedImage data = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB_PRE);
{//all images include alpha
final Graphics2D g2d = data.createGraphics();
final BufferedImage img1 = ImageIO.read(new File("z:\\img1.png"));
g2d.drawImage(img1, 0, 0, null);
final BufferedImage img2 = ImageIO.read(new File("z:\\img2.png"));
g2d.drawImage(img2, 100, 100, null);
g2d.dispose();
}
final File fa = new File("z:\\a\\test.png");
ImageIO.write(data, "png", fa);
final BufferedImage newData = ImageIO.read(fa);
final File fb = new File("z:\\b\\test.png");
ImageIO.write(newData, "png", fb);
final BufferedImage newData2 = ImageIO.read(fb);
System.out.println(equal(data, data)); // true
System.out.println(equal(data, newData)); // false
System.out.println(equal(newData, newData2)); // true
}
public static boolean equal(final BufferedImage img1, final BufferedImage img2) {
final int width = img1.getWidth();
final int height = img1.getHeight();
if (width == img2.getWidth() && height == img2.getHeight()) {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
final int argb1 = img1.getRGB(x, y);
final int argb2 = img2.getRGB(x, y);
if (argb1 != argb2){
return false;
}
}
}
return true;
} else {
return false;
}
}
我尝试验证图像的一致性,但是结果是,为什么相等的data和newData之间返回false?我应该如何纠正代码?
顺便说一句,ImageIO.write似乎有一个可疑的问题。 我已经通过ImageIO.write,PngEncoderB(http://catcode.com/pngencoder/),PngEncoder(http://objectplanet.com/pngencoder/)和image4j(https://github.com/imcdonagh/image4j)编写了一些图像。 PngEncoderB,PngEncoder和image4j是相同的图像像素,只是ImageIO.write不同。其实我不知道哪一个是正确的。如果您有兴趣,请使用比对/ DiffImg等图像比较软件来检查Alpha。