Change the alpha value of a BufferedImage?

后端 未结 6 1705
清酒与你
清酒与你 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:30

    I'm 99% sure the methods that claim to deal with an "RGB" value packed into an int actually deal with ARGB. So you ought to be able to do something like:

    for (all x,y values of image) {
      int argb = img.getRGB(x, y);
      int oldAlpha = (argb >>> 24);
      if (oldAlpha == 100) {
        argb = (80 << 24) | (argb & 0xffffff);
        img.setRGB(x, y, argb);
      }
    }
    

    For speed, you could maybe use the methods to retrieve blocks of pixel values.

提交回复
热议问题