gpt4 book ai didi

java - BufferedImage 的透明区域写出黑色

转载 作者:行者123 更新时间:2023-12-01 14:59:49 25 4
gpt4 key购买 nike

我知道这与合成有关,但我不知道是什么。在前面的代码部分中,BufferedImage 中的特定像素列表被设置为透明黑色:

        for(Pixel p : closed){
Color c = new Color(image.getRGB(p.x, p.y));
Color newC = new Color(0,0,0, 0);
image.setRGB(p.x, p.y, newC.getRGB() & 0x00000000);
}

if(andCrop){
image = image.getSubimage(left, top, right-left, bottom-top);
}


return image;

然后我尝试写出图像:

try {

BufferedImage out = new BufferedImage(image.getWidth(), image.getHeight(), java.awt.Transparency.TRANSLUCENT);
Graphics2D g2d = out.createGraphics();
g2d.setComposite(AlphaComposite.Clear);
g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
g2d.setComposite(AlphaComposite.Src);
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
g2d.dispose();

File outputfile = new File(file);
ImageIO.write(out, "png", outputfile);
} catch (IOException e) {

}

现在,在我尝试将图像绘制到“out”上之前,我知道“out”是清楚的。我没有得到的是我的合成出了什么问题。我不再是透明的,而是变成全黑的。

使用的所有缓冲图像都是 INT_ARGB。

编辑-这个问题已经解决了。图像源来自ImageIO.read,返回的BufferedImage不支持alpha。快速的读后转换让其余代码顺利运行。

最佳答案

我想到的事情......(感谢安德鲁):

java.awt.Transparency.TRANSLUCENT = 3
TYPE_INT_ARGB = 2
TYPE_INT_ARGB_PRE = 3

public BufferedImage(int width,
int height,
int imageType)

Constructs a BufferedImage of one of the predefined image types. (TYPE_...)

http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html

所以看起来基本上是一个混淆。

此外,您想要达到的效果是什么??你清除一个空图像,然后向它绘制完全透明的像素......我只是不明白。

关于java - BufferedImage 的透明区域写出黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13831448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com