ImageIO 忽略 PNG Alpha 通道
似乎用 ImageIO.read 加载的 PNG 忽略了 alpha 通道。 (我尝试使用 JRE 6 update 20)
Bug ?
例子 :
public class Test extends JFrame
{
public Test()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b = new JButton("Test");
try
{
b.setIcon(new ImageIcon(ImageIO.read(new File("D:\\image.png"))));
}
catch (IOException e2)
{
}
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
getContentPane().add(b, BorderLayout.CENTER);
setSize(500,500);
setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args)
{
new Test();
}
}
It seems that PNG loaded with ImageIO.read ignore the alpha channel.
(I tried with JRE 6 update 20)
Bug ?
Example :
public class Test extends JFrame
{
public Test()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b = new JButton("Test");
try
{
b.setIcon(new ImageIcon(ImageIO.read(new File("D:\\image.png"))));
}
catch (IOException e2)
{
}
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
getContentPane().add(b, BorderLayout.CENTER);
setSize(500,500);
setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args)
{
new Test();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你怎么知道它忽略了 Alpha 通道。下面的代码生成此屏幕截图:
代码:
How do you know that it ignores the alpha channel. The code below produces this screenshot:
Code:
根据我的经验 - 使用 JDK 1.6.0_21 进行测试,Java imageio png 解码器部分支持透明度。它支持带有附加 Alpha 通道的 24 位全彩色图像(每像素总共 32 位),以及带有 tRNS 主干的索引彩色图像,其中包含可与现有 RGB 调色板结合使用的 Alpha 贴图来定义哪种颜色是透明的。但它不支持 24 位 RGB 以及包含图像的单个透明 RGB 颜色值的 tRNS 主干。也许您的图像是 imageio 不支持的格式之一。
Through my experience - tested with JDK 1.6.0_21, Java imageio png decoder supports transparency partially. It supports 24-bit full color image with an additional alpha channel (totally 32-bit per pixel), as well as indexed color image with tRNS trunk which includes an alpha map that can be combined with the existing RGB color palette to define which color is transparent. But it DOES NOT support 24-bit RGB with a tRNS trunk that includes a single transparent RGB color value for the image. Perhaps your image is one of such formats that are not supported by imageio.
您可以使用 Sixlegs Java PNG Decoder ,它没有 alpha 透明度错误。作为参考,Sun Java Bug #6371389 讨论了 PNG alpha 的类似问题渠道。
You can use Sixlegs Java PNG Decoder , it doesn't have alpha transparency bug. For reference, Sun Java Bug #6371389 talks about a similar issue with PNG alpha channels.