[英]Java: Comparing two Bufferedimages using loops
當我想檢查兩個Bufferedimages是否相同時,我可以使用以下兩個循環逐像素比較它們:
boolean bufferedImagesEqual(BufferedImage img1, BufferedImage img2) {
if (img1.getWidth() == img2.getWidth() && img1.getHeight() == img2.getHeight()) {
for (int x = 0; x < img1.getWidth(); x++) {
for (int y = 0; y < img1.getHeight(); y++) {
if (img1.getRGB(x, y) != img2.getRGB(x, y))
return false;
}
}
} else {
return false;
}
return true;
}
我從這里拿來的
但是,如果img2
旋轉90 or 270
度,我也希望img2
正常工作。 我嘗試在第二個.getRGB()
使用img2.getWidth()-x
類的組合切換x
和y
,但似乎都不起作用。
我知道這可能不是世界上最困難的問題,但我似乎無法弄清楚。
任何幫助將不勝感激。
我認為不可避免地要執行3次“ 2-loop”:一次旋轉0度,第二次旋轉90度,第三次旋轉270度:
對於90度:(置為img1.getWidth()== img2.getHeight()&& img1.getHeight()== img2.getWidth())
for (int x = 0; x < img1.getWidth(); x++) {
for (int y = 0; y < img1.getHeight(); y++) {
if (img1.getRGB(x, y) != img2.getRGB(img1.getHeight() - 1 - y, x))
return false;
}
}
對於270度:(置為img1.getWidth()== img2.getHeight()&& img1.getHeight()== img2.getWidth())
for (int x = 0; x < img1.getWidth(); x++) {
for (int y = 0; y < img1.getHeight(); y++) {
if (img1.getRGB(x, y) != img2.getRGB(y, img1.getWidth() - 1 - x))
return false;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.