绘制完全透明的“白色”在Java BufferedImage中 [英] Drawing fully transparent "white" in Java BufferedImage

查看:2815
本文介绍了绘制完全透明的“白色”在Java BufferedImage中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能听起来像是一个奇怪的标题,但忍耐着我,的一个原因:我试图产生

一个灰色的背景上的文本周围的白色辉光。



为了生成辉光,我创建了一个比文本更大的新BufferedImage,然后我将文本绘制成白色图像的画布,并通过,它将所有alpha值设置为0.这可以起到作用,但就性能而言这可能是一种可怕的方法。



我可以做得更好吗?



PS:我也乐意提供完全不同的建议来创造这样的发光效果

解决方案

我发现 clearRect 应绘制透明颜色。

  g.setBackground(new Color(0x00FFFFFF,true)); 
g.clearRect(0,0,img.getWidth(),img.getHeight());

您还应该能够通过直接设置像素数据来强制BufferedImage填充透明颜色。

  public static void forceFill(BufferedImage img,int rgb){
for(int x = 0; x< ; img.getWidth(); x ++){
for(int y = 0; y< img.getHeight(); y ++){
img.setRGB(x,y,rgb);
}
}
}

我测试了它并且 setRGB 似乎接受ARGB值。


This might sound like a bit of strange title, but bear with me, there is a reason:

I am trying to generate a white glow around a text on a gray background.

To generate the glow, I created a new BufferedImage that's bigger than the text, then I drew the text in white onto the canvas of the image and ran a Gaussian Blur over the image via a ConvolveOp, hoping for something like this:

                                                                 

At first I was a bit surprised when the glow turned out darker than the gray background of the text:

                                                                 

But after a bit of thinking, I understood the problem:

The convolution operates on each color channel (R, G, B, and A) independently to calculate the blurred image. The transparent background of the picture has color value 0x00000000, i.e. a fully transparent black! So, when the convolution filter runs over the image, it not only blends the alpha value, but also mixes the black into the RGB values of the white pixels. This is why the glow comes out dark.

To fix this, I need to initialize the image to 0x00FFFFFF, i.e. a fully transparent white instead, but if I just set that color and fill a rectangle with it, it simply does nothing as Java says "well, it's a fully transparent rectangle that you're drawing! That's not going to change the image... Let me optimize that away for you... Done... You're welcome.".

If I instead set the color to 0x01FFFFFF, i.e. an almost fully transparent white, it does draw the rectangle and the glow looks beautiful, except I end up with a very faint white box around it...

Is there a way I can initialize the image to 0x00FFFFFF everywhere?

UPDATE:

I found one way, but it's probably as non-optimal as you can get:

I draw an opaque white rectangle onto the image and then I run a RescaleOp over the image that sets all alpha values to 0. This works, but it's probably a terrible approach as far as performance goes.

Can I do better somehow?

PS: I'm also open to entirely different suggestions for creating such a glow effect

解决方案

I've found that clearRect should paint a transparent color.

g.setBackground(new Color(0x00FFFFFF, true));
g.clearRect(0, 0, img.getWidth(), img.getHeight());

You should also be able to force the BufferedImage to fill with a transparent color by setting the pixel data directly.

public static void forceFill(BufferedImage img, int rgb) {
    for(int x = 0; x < img.getWidth(); x++) {
        for(int y = 0; y < img.getHeight(); y++) {
            img.setRGB(x, y, rgb);
        }
    }
}

It is not clearly documented but I tested it and setRGB appears to accept an ARGB value.

这篇关于绘制完全透明的“白色”在Java BufferedImage中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆