Change the alpha value of a BufferedImage?

后端 未结 6 1714
清酒与你
清酒与你 2020-12-11 04:21

How do I change the global alpha value of a BufferedImage in Java? (I.E. make every pixel in the image that has a alpha value of 100 have a alpha value of 80)

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-11 04:43

    for a nicer looking alpha change effect, you can use relative alpha change per pixel (rather than static set, or clipping linear)

    public static void modAlpha(BufferedImage modMe, double modAmount) {
            //
        for (int x = 0; x < modMe.getWidth(); x++) {          
            for (int y = 0; y < modMe.getHeight(); y++) {
                    //
                int argb = modMe.getRGB(x, y); //always returns TYPE_INT_ARGB
                int alpha = (argb >> 24) & 0xff;  //isolate alpha
    
                alpha *= modAmount; //similar distortion to tape saturation (has scrunching effect, eliminates clipping)
                alpha &= 0xff;      //keeps alpha in 0-255 range
    
                argb &= 0x00ffffff; //remove old alpha info
                argb |= (alpha << 24);  //add new alpha info
                modMe.setRGB(x, y, argb);            
            }
        }
    }
    

提交回复
热议问题