我正在使用一種方法,它將圖像與給定顏色的每個像素混合在一起。我的問題是,每次運行使用相同圖像的方法時,結果都越來越飽和。像這樣: example
我存儲由該方法作爲比原來的一個不同的變量返回的圖像,並且每次我穿過原始作爲image
參數。每次我在圖像上運行混合方法時,結果都越來越飽和。我該如何解決?
這是混合方法我用:
public static BufferedImage blendImage (BufferedImage image, Color blend) {
BufferedImage newImage = image;
for (int i = 0; i < image.getWidth(); i ++) for (int j = 0; j < image.getHeight(); j ++) {
Color c1 = new Color(image.getRGB(i, j), true);
Color c2 = blend;
float r1 = ((float)c1.getRed())/255.0F;
float g1 = ((float)c1.getGreen())/255.0F;
float b1 = ((float)c1.getBlue())/255.0F;
float a1 = ((float)c1.getAlpha())/255.0F;
float r2 = ((float)c2.getRed())/255.0F;
float g2 = ((float)c2.getGreen())/255.0F;
float b2 = ((float)c2.getBlue())/255.0F;
float a2 = ((float)c2.getAlpha())/255.0F;
Color c3 = new Color(r1 * r2, g1 * g2, b1 * b2, a1 * a2);
newImage.setRGB(i, j, c3.getRGB());
}
return newImage;
}
我想知道是否有一些修正這個,或者是否有更好的方式來融合圖像,任何人都知道。
編輯:事實證明,該方法正在改變原始圖像。我不知道如何,但它與BufferedImage newImage = image;
行有關。我的解決方案是將newImage
設置爲新的BufferedImage
對象,並使其與通過的image
具有相同的寬度,高度和類型。我不知道爲什麼原始圖像被修改了。