簡體   English   中英

Java:使用循環比較兩個Bufferedimages

[英]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類的組合切換xy ,但似乎都不起作用。

我知道這可能不是世界上最困難的問題,但我似乎無法弄清楚。

任何幫助將不勝感激。

我認為不可避免地要執行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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM