简体   繁体   English

BufferedImage的透明区域被写成黑色

[英]Transparent regions of a BufferedImage are writing out black

I know this is something to do with compositing but I can't work out what. 我知道这与合成有关,但我无法解决。 In an earlier section of code, a particular list of pixels in a BufferedImage are set to be transparent black: 在代码的较早部分,将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;

Then I attempt to write the image out: 然后我尝试将图像写出:

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) {

        }

Now, I know that 'out' is clear before I attempt to draw the image onto it. 现在,我知道在尝试将图像绘制到图像上之前,“输出”是清晰的。 What I'm not getting is what's wrong with my compositing. 我没有得到的是合成方面的问题。 Instead of coming out as transparent, I'm getting full-black. 我不是全透明的,而是全黑的。

All bufferedimages used are INT_ARGB. 使用的所有缓冲图像均为INT_ARGB。

EDIT - This has been solved. 编辑-这已经解决。 The image source was from ImageIO.read and the BufferedImage returned did not support alpha. 图像源来自ImageIO.read,返回的BufferedImage不支持alpha。 A quick post-read conversion let the rest of the code run smoothly. 快速的读取后转换使其余代码可以平稳运行。

Things that comes to my mind... (thanks to Andrew): 我想到的事情...(感谢安德鲁):

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 http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html

so it seems as basically it's a mixup. 所以看起来基本上是一个混乱。

Besides, what is the effect you want to achieve? 此外,什么你想达到什么效果? you clear an empty image, then draw fully transparent pixels to it... I just don't get it. 您清除一个空白图像,然后为其绘制完全透明的像素...我只是不明白。

Whelp, this has been downvoted now so I'm not sure this will be relevant, but the issue was that the original BufferedImage was being read in by ImageIO, and this image was not supporting ARGB . Whelp,现在这已经被否决了,所以我不确定这是否有意义,但是问题是ImageIO正在读取原始BufferedImage,而该图像不支持ARGB A quick post-read conversion allowed the rest of the code to work. 快速的读后转换使其余代码可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM