没有 Alpha 通道的 PNG 透明度
我正在尝试从 Java 中的 BufferedImage 创建透明的 PNG 图像。
PNG 将被加载到不支持 Alpha 通道的另一个软件中。
这应该没问题,因为根据 第 8 章第 5 节第 4 部分PNG书中,我可以通过指定像素值透明来实现透明度。这是通过在 png 文件中创建 tRNS 标头来实现的。
我不确定如何将此技术细节转换为 Java 代码。实际图像本身是单色的;每个像素都是黑色或白色。 我想用透明像素替换每个白色像素,而不使用 Alpha 通道。有人可以把我推向正确的方向吗?
I am trying to create a transparent PNG image from a BufferedImage in Java.
The PNG will be loaded into another piece of the software, that does not support the alpha channel.
That should be fine, because, according to Chapter 8, section 5, part 4 of the PNG book, I can achieve transparency by specifying a pixel value to be transparent. This works by creating a tRNS
header in the png file.
I am unsure how to translate this technical detail to Java code. The actual image itself is monochrome; each pixel is going to be black or white. I would like to replace each white pixel with a transparent pixel, without using the alpha channel. May someone push me in the right direction please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用以下代码。
创建单色且透明的 BufferedImage:
将 BufferedImage 保存在 PNG 文件中:
You can use the following code.
Create a monochrome and transparent BufferedImage:
Save the BufferedImage in a PNG file:
请尝试以下操作:
IndexColorModel
与所需的参数。ColorConvertOp
。您可以将null
作为RenderingHints
参数传递。ColorConvertOp.createCompatibleDestImage
][3] 接收新的BufferedImage
实例(目标)。ColorConvertOp.filter
][4] 执行从源到目标的转换。我对此并不完全确定,但
ColorConvertOp
似乎是一个很好的起点。告诉我们效果如何![3]: http://java.sun.com/javase/6/docs/api/java/awt/image/ColorConvertOp.html#createCompatibleDestImage(java.awt.image.BufferedImage, java.awt.图像.ColorModel)
[4]: http://java.sun.com/javase/6/docs/api/java/awt/image/ColorConvertOp.html#filter(java.awt.image.BufferedImage, java.awt.图像.BufferedImage)
Try the following:
BufferedImage
.IndexColorModel
with the desired parameters.ColorConvertOp
. You can passnull
as theRenderingHints
argument.ColorConvertOp.createCompatibleDestImage
][3] to receive a newBufferedImage
instance (destination).ColorConvertOp.filter
][4] to perform the conversion from the source to the destination.I'm not completely sure about this, but the
ColorConvertOp
seems like a good starting point. Tell us how it works out![3]: http://java.sun.com/javase/6/docs/api/java/awt/image/ColorConvertOp.html#createCompatibleDestImage(java.awt.image.BufferedImage, java.awt.image.ColorModel)
[4]: http://java.sun.com/javase/6/docs/api/java/awt/image/ColorConvertOp.html#filter(java.awt.image.BufferedImage, java.awt.image.BufferedImage)
我想你会想要使用 索引颜色模型。
I think you'll want to use an IndexColorModel.
我不知道,但我确信答案就在 png 规范。希望这有帮助!
是否有一个用于编写 png 文件的 java 库(也许是 libpng 的 java 绑定?)可以为您完成这项艰苦的工作? (当事情不太顺利时,可能会为你省去很多麻烦)
I don't know, but I'm sure the answer is in The png Specification. Hope this helps!
Isn't there a library for java for writing png files (java bindings for libpng perhaps?) that will do the hard work for you? (and probably save you many headaches when things don't quite work)