Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使具有特定颜色的图像的每个像素透明_Java_Image_Transparency - Fatal编程技术网

Java 使具有特定颜色的图像的每个像素透明

Java 使具有特定颜色的图像的每个像素透明,java,image,transparency,Java,Image,Transparency,此问题与此问题重复: 但我需要一个Java等价物。我需要一种图像类型(比如PNG,BMP,…),它可以保持这种颜色的完全透明性(alpha=0)。当然还有一种将其保存为文件的方法。用于读取文件和写入文件。使用的getRGB和setRGB方法更改颜色 import java.awt.*; import java.awt.image.*; public class Transparency { public static Image makeColorTransparent (Ima

此问题与此问题重复:

但我需要一个Java等价物。我需要一种图像类型(比如
PNG
BMP
,…),它可以保持这种颜色的完全透明性(alpha=0)。当然还有一种将其保存为文件的方法。

用于读取文件和写入文件。使用的
getRGB
setRGB
方法更改颜色

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);
    }
}
修改代码使每个像素透明

资料来源:http://www.rgagnon.com/javadetails/java-0265.html

您可以将a与四分量
LookupTable
一起使用,将与背景匹配的颜色的alpha分量设置为零。示例可在和中找到