简体   繁体   English

ImageIO.read(png)失去透明度

[英]ImageIO.read(png) losing its transparency

I've tested my image when using (1) and it's of type TYPE_3BYTE_BGR while it's a transparent image, when using (2) my image appears with transparency, can someone explain me where I'm doing a mistake please? 我在使用(1)时测试过我的图像,而它是TYPE_3BYTE_BGR类型,而它是透明图像,在使用(2)时我的图像以透明显示,有人可以向我解释我在做错什么吗?

Here's my code: 这是我的代码:

public static void main(String[] args){

    Frame frame = new Frame();
    frame.setSize(200, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    frame.getContentPane().setBackground(Color.PINK);
    BufferedImage image;
    try{
        image = ImageIO.read(new File("my_path"));
    }catch(IOException e){
        image = null;
    }
    JLabel label = new JLabel(new ImageIcon("my_path"));
    //JLabel label = new JLabel(new ImageIcon(image));
    frame.getContentPane().add(label);
    label.setBounds(10, 10, 6, 10);
    frame.setVisible(true);

}

Here 's my image. 是我的形象。

Thanks in advance! 提前致谢!

PS: Btw site must be broken I was not able to paste my code, had to rewrite it, weird. PS:Btw网站必须坏掉我无法粘贴我的代码,不得不重写它,很奇怪。

PS2: Also it's been the second time my "Hi!" PS2:这也是我第二次打招呼! as first line gets removed... 当第一行被删除...

Your image file is a 24 bit RGB PNG file, with an optional tRNS chunk, specifying a single RGB color that should be considered transparent (as opposed to a 32 bit RGBA PNG with full alpha channel). 您的图片文件是一个24位RGB PNG文件,带有一个可选的tRNS块,指定了一种应视为透明的RGB颜色(与具有完整alpha通道的32位RGBA PNG相对)。

For some reason, the standard PNGImageReader that comes with the JRE does not create a transparent image for RGB PNGs with a tRNS chunk. 出于某种原因,该标准PNGImageReader自带的JRE 不会创建一个RGB的PNG透明图像tRNS块。 According the spec, optional chunks (starting with a lowercase letter) can be ignored by a decoder, so this is fully acceptable behavior. 根据规范,可选块(以小写字母开头)可以被解码器忽略,因此这是完全可以接受的行为。

You can, however, read the metadata for the PNG using the ImageIO API, and if there's a tRNS chunk, you can create a transparent BufferedImage and apply the transparency yourself (replacing all RGB values equal to the RGB value from the tRNS chunk). 但是,您可以使用ImageIO API读取PNG的元数据,如果有tRNS块,则可以创建一个透明的BufferedImage并自己应用该透明性(替换所有与tRNS块中的RGB值相等的RGB值)。 But this is quite some extra work if you just want to read icons bundled with your application. 但是,如果您只想阅读与应用程序捆绑在一起的图标,则这是很多额外的工作。

The easiest fix would be to just store the PNG with either palette and transparency or a full 32 bit RGBA PNG, which are both supported by ImageIO without any more work. 最简单的解决方法是仅存储具有调色板和透明度的PNG或完整的32位RGBA PNG,ImageIO支持这两种格式,而无需进行任何其他工作。

The reason why it works using the "direct" ImageIcon approach, is that ImageIcon uses a completely different PNG decoder than ImageIO, which seemingly does apply the tRNS chunk (in your case, but I don't think has to, so the behavior may vary by JRE or platform, you will have to test). 它使用“直接” ImageIcon方法工作的原因是, ImageIcon使用的是与ImageIO完全不同的PNG解码器,它似乎确实应用了tRNS块(在您的情况下,但我认为不是必须这样做,因此行为可能因JRE或平台而异,您必须进行测试)。

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

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