当我组合 2 种颜色时,我正在尝试(在 Java 中)获得结果颜色,顶部的颜色具有 alpha 透明度。基本上,我正在尝试为图像分配背景颜色,但我已将其分解为单独更改的每个像素。我看过几篇文章,包括这篇文章,但无济于事。有人知道如何执行这种 RGBA/RGB 颜色混合吗?我当前的代码使用这个 png:
并生成此 JPG:
这是我目前正在使用的功能。演示图像的背景设置为全蓝色,或 int 为 255。
public static void PNGToJPEGConvert(String PNGPath, String NewJPEGPath, int BackColor) throws IOException {
try {
BufferedImage bufferedImage = ImageIO.read(new File(PNGPath));
BufferedImage newImage;
int R1, G1, B1, R2 = (BackColor & 0xFF0000) >> 16, G2 = (BackColor & 0xFF00) >> 8, B2 = (BackColor & 0xFF), W1, W2, W3, M1, M2, R3, G3, B3;
float br;
newImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
for(int x=0; x < bufferedImage.getWidth(); x++) {
for(int y=0; y < bufferedImage.getHeight(); y++) {
R1 = BufferedImageGetPixelARGB(bufferedImage, "R", x, y);
G1 = BufferedImageGetPixelARGB(bufferedImage, "G", x, y);
B1 = BufferedImageGetPixelARGB(bufferedImage, "B", x, y);
W1 = Math.min(R1, Math.min(G1, B1));
W2 = Math.min(R2, Math.min(G2, B2));
R1 -= W1;
G1 -= W1;
B1 -= W1;
R2 -= W2;
G2 -= W2;
M1 = Math.max(R1, Math.max(G1, B1));
M2 = Math.max(R2, Math.max(G2, B2));
br = (M1 + M2)/(2*BufferedImageGetPixelARGB(bufferedImage, "A", x, y));
R3 = (int) ((R1 + R2) * br);
G3 = (int) ((G1 + G2) * br);
B3 = (int) ((B1 + B2) * br);
W3 = (W1 + W2) / 2;
R3 += W3;
G3 += W3;
B3 += W3;
newImage.setRGB(x, y, RGBValuesToInt(R3, G3, B3));
}
}
try {
ImageIO.write(newImage, "jpg", new File(NewJPEGPath));
} catch(Exception e) {
}
} catch(Exception e) {
}
}
感谢您的帮助,-尼尔