问题描述
我正在使用以下代码阅读PNG图像:
I'm reading a PNG image with the following code:
BufferedImage img = ImageIO.read(new URL(url));
在显示它时,有一个黑色背景,我知道这是由PNG透明度引起的。我尝试修复这个问题是将每个像素的alpha值设置为不等于255,以 Color.white
。这并没有解决问题。
Upon displaying it, there is a black background, which I know is caused from PNG transparency. My attempt to fixing this was setting each pixel with an alpha value not equal to 255, to Color.white
. This did not solve the problem.
我找到了问题的答案,建议使用 BufferedImage.TYPE_INT_RGB
,但鉴于上面的代码,我不确定如何应用这个。任何帮助将不胜感激。
I found answers to the question, suggesting the use of BufferedImage.TYPE_INT_RGB
, however I am unsure of how to apply this given my code above. Any help would be appreciated.
推荐答案
创建第二个 BufferedImage
类型 TYPE_INT_RGB
...
Create a second BufferedImage
of type TYPE_INT_RGB
...
BufferedImage copy = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
将原件涂成副本......
Paint the original to the copy...
Graphics2D g2d = copy.createGraphics();
g2d.setColor(Color.WHITE); // Or what ever fill color you want...
g2d.fillRect(0, 0, copy.getWidth(), copy.getHeight());
g2d.drawImage(img, 0, 0, null);
g2d.dispose();
您现在拥有图片的非透明版本...
You now have a non transparent version of the image...
要保存图像,请查看
这篇关于删除PNG BufferedImage中的透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!