java背景透明
我有一个 gif 图像,其中仅包含彩色形状和透明背景。
我想用我想要的颜色替换形状的颜色(这个 gif 的调色板只有 2 种颜色:在我的例子中是透明和白色)。
我创建了一个过滤器,可以正确地将白色替换为红色(这是一个测试)。
但是,我的方法 imageToBufferedImage 遇到了问题,它删除了透明度并将其替换为黑色(不知道为什么)。
所以到目前为止我所做的是:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.File;
import javax.imageio.ImageIO;
public class TestPNG {
public static void main(String[] args) throws Exception {
File in = new File("bg.gif");
BufferedImage source = ImageIO.read(in);
int color = source.getRGB(0, 0);
Image image = makeColorTransparent(source, new Color(color), new Color(255, 0, 0));
BufferedImage transparent = imageToBufferedImage(image);
File out = new File("bg2.gif");
ImageIO.write(transparent, "gif", out);
}
private static BufferedImage imageToBufferedImage(Image image) {
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bufferedImage.createGraphics();
//g2.setBackground(Color.blue);
g2.clearRect(0, 0, 200, 40);
g2.drawImage(image, 0, 0, null);
g2.dispose();
return bufferedImage;
}
public static Image makeColorTransparent(BufferedImage im, final Color search, final Color replace) {
ImageFilter filter = new RGBImageFilter() {
public final int filterRGB(int x, int y, int rgb) {
if (rgb == search.getRGB()) {
return replace.getRGB();
} else {
return rgb;
}
}
};
ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
I have a gif image which contains only a colored shape, and a transparent background.
I would like to replace the shape's color by the one I want (the color pallet for this gif is only 2 colors: transparent and white in my case).
I've created a filter which correctly replace white with red (this is a test).
However I'm encountering an issue with my method imageToBufferedImage, it removes the transparency and replace it with black (don't know why).
So what I've done so far is this:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.File;
import javax.imageio.ImageIO;
public class TestPNG {
public static void main(String[] args) throws Exception {
File in = new File("bg.gif");
BufferedImage source = ImageIO.read(in);
int color = source.getRGB(0, 0);
Image image = makeColorTransparent(source, new Color(color), new Color(255, 0, 0));
BufferedImage transparent = imageToBufferedImage(image);
File out = new File("bg2.gif");
ImageIO.write(transparent, "gif", out);
}
private static BufferedImage imageToBufferedImage(Image image) {
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bufferedImage.createGraphics();
//g2.setBackground(Color.blue);
g2.clearRect(0, 0, 200, 40);
g2.drawImage(image, 0, 0, null);
g2.dispose();
return bufferedImage;
}
public static Image makeColorTransparent(BufferedImage im, final Color search, final Color replace) {
ImageFilter filter = new RGBImageFilter() {
public final int filterRGB(int x, int y, int rgb) {
if (rgb == search.getRGB()) {
return replace.getRGB();
} else {
return rgb;
}
}
};
ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的代码中有 3 个问题:
1)替换
为
和 ,
因为
由于某种原因,
source.getRGB(0, 0)
忽略 alpha 值,它变成白色 ((255, 255, 255, 0) 变成 (255, 255, 255, 255))2) 不能使用
int color = source.getRGB(0, 0)
,因为它使用第一个像素的颜色(透明) 。您应该使用一些其他代码(例如在控制台中询问颜色)来找出要存储在int color
中的像素颜色3) 您正在清除
BufferedImage bufferedImage
的颜色在imageToBufferedImage(...)
中使用Color.BLACK
(默认)。将//g2.setBackground(Color.blue);
替换为g2.setBackground(new Color(0, 0, 0, 0));
或删除g2.clearRect(...);
There are 3 problems in your code:
1) Replace
with
and
with
BECAUSE for some reason, the
source.getRGB(0, 0)
ignores alpha value and it becomes white ((255, 255, 255, 0) becomes (255, 255, 255, 255))2) You can't use
int color = source.getRGB(0, 0)
, because it uses the color of first pixel (transparent). You should use some other code (like asking for the color in console) to find out what pixel's color to store inint color
3) You are clearing the color of
BufferedImage bufferedImage
inimageToBufferedImage(...)
withColor.BLACK
(default). Replace//g2.setBackground(Color.blue);
withg2.setBackground(new Color(0, 0, 0, 0));
or remove theg2.clearRect(...);
确定您的图像支持 Alpha 通道吗?
尝试先将非黑色全尺寸矩形绘制到 bufferedImage 中?
Sure your image supports an alpha channel?
Tried drawing a non-black full size rectangle into bufferedImage first?
得到
您可以从该方法的 Javadoc 中
由于您清除了图像并用背景颜色填充了它,因此像素已经是黑色的,因此它们保持黑色。
尝试一下。
在绘制图像之前
You have this:
From the Javadoc for this method:
Since you cleared your image, filling it with the background colour, the pixels are already black so they stay black.
Try
before painting your image.
只是猜测:
a) 也许您不需要
clearRect(...)
方法。b)也许你可以使用类似的东西:
Just guessing:
a) Maybe you don't need the
clearRect(...)
method.b) Maybe you can use something like:
您可以使用合成来清除像素。您不能只在其他像素或任何垃圾上绘制透明像素。 Porter-Duff 规则很明确,执行任何操作都会导致像素根据复合规则进行混合。所以只要改变一下,规则就会清除像素。
问题是大多数解决方案都试图使用规则来诱使图像最终获得您想要的像素。稍微改变一下规则,一切都会变得更容易。
这将清除图形对象的当前像素。然后再画回你想要的东西。我的示例恢复了之前的合成,因为您会惊讶地发现这些东西变得奇怪的频率如此之高,这样您就可以获得清除的像素,并且可以重新开始,而无需更改图形上下文的任何内容。
You clear out the pixels with the use of a composite. You can't just paint transparent pixels over other pixels or any of that trash. The Porter-Duff rules are clear, doing any of that will cause pixels to mix according to the composite rule. So just change, the rule clear the pixels.
The problem is most of the solutions are trying to coax the image into getting the pixels you want at the end, using the rule. Change the rule for a second and everything gets way easier.
That will clear a graphics object of the current pixels. Then just paint back on what you want. My example restored the previous composite as you'll be surprised how often that stuff goes weird, this way you just get cleared pixels and can start over without changing anything with the graphics context.