Making every pixel of an image having a specific color transparent

前端 未结 3 1399
忘了有多久
忘了有多久 2020-12-20 02:06

This question is a duplicate of this question: Making every pixel of an image having a specific color transparent

But I need a Java equivalent. And I need a image-t

相关标签:
3条回答
  • 2020-12-20 02:33

    Use ImageIO.read for reading a file and ImageIO.write for writing. Use the getRGB and setRGB methods of BufferedImage to change the colours.

    0 讨论(0)
  • 2020-12-20 02:41
    import java.awt.*;
    import java.awt.image.*;
    
    public class Transparency {
      public static Image makeColorTransparent
        (Image im, final Color color) {
        ImageFilter filter = new RGBImageFilter() {
          // the color we are looking for... Alpha bits are set to opaque
          public int markerRGB = color.getRGB() | 0xFF000000;
    
          public final int filterRGB(int x, int y, int rgb) {
            if ( ( rgb | 0xFF000000 ) == markerRGB ) {
              // Mark the alpha bits as zero - transparent
              return 0x00FFFFFF & rgb;
              }
            else {
              // nothing to do
              return rgb;
              }
            }
          }; 
    
        ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
        return Toolkit.getDefaultToolkit().createImage(ip);
        }
    }
    

    Modified the code to make each pixel transparent

    Source :http://www.rgagnon.com/javadetails/java-0265.html

    0 讨论(0)
  • 2020-12-20 02:42

    You can use a LookupOp with a four-component LookupTable that sets the alpha component to zero for colors that match the background. Examples may be found in Using the Java 2D LookupOp Filter Class to Process Images and Image processing with Java 2D.

    0 讨论(0)
提交回复
热议问题