Java – use 2 RGB colors with alpha

When I combine two colors, I'm trying to get the final color (in Java), and the color at the top has alpha transparency Basically, I'm trying to assign a background color to the image, but I've broken it down into each individually changed pixel I've read several articles, including this one, which are useless Who knows how to mix RGBA / RGB colors? My current code uses this PNG:

And generate this JPG:

This is the function I am currently using The background of the demo image is set to all blue or int is 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",y);
                B1 = BufferedImageGetPixelARGB(bufferedImage,"B",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",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,B3));
            }
        }
        try {
            ImageIO.write(newImage,"jpg",new File(NewJPEGPath));
        } catch(Exception e) {

        }
    } catch(Exception e) {

    }
}

Thank you for your help. - Neil

Solution

Basically

float masking_factor = mask_value/255.0;
composed_value = int(
    source_pixel_value * (1 - masking_factor) + 
    overlay_pixel_value * masking_factor
);

We assume that all values are 8 bits, i.e. 0 to 255, and the mask is monochrome The mask is completely black. It is completely transparent and only the source pixels are visible If the mask is completely white, it is completely opaque and only the overlay pixels are visible Intermediate values give intermediate opacity

But if you want alpha blending to be unique, there is a better way to deal with individual pixels See graphics2d and its Setcomposite() method

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>