我希望我的图像(缓冲图像)具有透明背景,我首先尝试使用 png,然后是 gif,然后我尝试使用 imageFilters,但我也无法一拍即合,所以现在我决定使用简单的jpeg,将背景设置为一种颜色,然后去掉该颜色,同样,我认为 imageFilters 适合这种情况,但我不知道到底如何使用它们,我想去掉的颜色是 0xff00d8 (洋红色)。
任何人都可以帮忙提供一种方法或示例吗?
最佳答案
jpeg
不支持透明度。确保您的缓冲图像也支持透明度:
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
TYPE_INT_ARGB
中的 A 代表 alpha,它是不透明度的度量。
您需要将像素值设置为 0x00000000 才能使其透明。
//Load the image
BufferedImage in = ImageIO.read(img);
int width = in.getWidth(), height = in.getHeight();
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.drawImage(in, 0, 0, null);
g.dispose();
//Change the color
int colorToChange = 0xff00d8;
for (int x=0;x<width;x++)
for (int y=0;y<height;y++)
if(bi.getRGB(x,y)==colorToChange)
bi.setRGB(x,y,0x00FFFFFF&colorToChange);
bi.save(new File("out.png"));
关于java - 如何去除图像中的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16374986/