在java中,如何将java.awt.image.BufferedImage写入8位png文件?
我正在尝试从 java.awt.image.BufferedImage 写出一个 png 文件。 一切正常,但生成的 png 是一个 32 位文件。
有没有办法让png文件变成8位的? 该图像是灰度的,但我确实需要透明度,因为这是一个覆盖图像。 我使用的是java 6,我更愿意返回一个OutputStream,以便我可以让调用类处理将文件写到磁盘/数据库。
这是代码的相关部分:
public static ByteArrayOutputStream createImage(InputStream originalStream)
throws IOException {
ByteArrayOutputStream oStream = null;
java.awt.Image newImg = javax.imageio.ImageIO.read(originalStream);
int imgWidth = newImg.getWidth(null);
int imgHeight = newImg.getHeight(null);
java.awt.image.BufferedImage bim = new java.awt.image.BufferedImage(imgWidth,
imgHeight, java.awt.image.BufferedImage.TYPE_INT_ARGB);
Color bckgrndColor = new Color(0x80, 0x80, 0x80);
Graphics2D gf = (Graphics2D)bim.getGraphics();
// set transparency for fill image
gf.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
gf.setColor(bckgrndColor);
gf.fillRect(0, 0, imgWidth, imgHeight);
oStream = new ByteArrayOutputStream();
javax.imageio.ImageIO.write(bim, "png", oStream);
oStream.close();
return oStream;
}
I am trying to write out a png file from a java.awt.image.BufferedImage. Everything works fine but the resulting png is a 32-bit file.
Is there a way to make the png file be 8-bit? The image is grayscale, but I do need transparency as this is an overlay image. I am using java 6, and I would prefer to return an OutputStream so that I can have the calling class deal with writing out the file to disk/db.
Here is the relevant portion of the code:
public static ByteArrayOutputStream createImage(InputStream originalStream)
throws IOException {
ByteArrayOutputStream oStream = null;
java.awt.Image newImg = javax.imageio.ImageIO.read(originalStream);
int imgWidth = newImg.getWidth(null);
int imgHeight = newImg.getHeight(null);
java.awt.image.BufferedImage bim = new java.awt.image.BufferedImage(imgWidth,
imgHeight, java.awt.image.BufferedImage.TYPE_INT_ARGB);
Color bckgrndColor = new Color(0x80, 0x80, 0x80);
Graphics2D gf = (Graphics2D)bim.getGraphics();
// set transparency for fill image
gf.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
gf.setColor(bckgrndColor);
gf.fillRect(0, 0, imgWidth, imgHeight);
oStream = new ByteArrayOutputStream();
javax.imageio.ImageIO.write(bim, "png", oStream);
oStream.close();
return oStream;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
imageio png writer 中的构建将在我使用过的所有平台上写入 32 位 png 文件,无论源图像是什么。 您还应该意识到,许多人抱怨生成的压缩比 png 格式的压缩要低得多。 有几个独立的 png 库 可以让您指定确切的格式,但我不这样做实际上对其中任何一个都有任何经验。
The build in imageio png writer will write 32bit png files on all the platforms I have used it on, no matter what the source image is. You should also be aware that many people have complained that the resulting compression is much lower than what is possible with the png format. There are several independent png libraries available that allow you to specify the exact format, but I don't actually have any experience with any of them.
我在这里找到了如何将 RGBA 转换为索引的答案: http://www.eichberger.de/2007/07/transparent-gifs-in-java.html
但是,生成的 8 位 png 文件仅具有 100% 或 0% 透明度。 您可能可以调整 IndexColorModel 数组,但我们决定将生成的文件(覆盖遮罩)制作为底层 jpg 并使用静态基础作为透明覆盖。
I found the answer as to how to convert RGBA to Indexed here: http://www.eichberger.de/2007/07/transparent-gifs-in-java.html
However, the resulting 8-bit png file only has 100% or 0% transparency. You could probably tweak the IndexColorModel arrays, but we have decided to make the generated file (what was an overlay mask) into an underlay jpg and use what was the static base as the transparent overlay.
这是一个有趣的问题...现在很晚了,我明天实验一下。 我将首先尝试使用 BufferedImage.TYPE_BYTE_INDEXED(可能在绘制之后)来查看 Java 是否足够智能来生成 8 位 PNG。
或者也许某些图像库可以允许这样做。
[编辑] 几年后...实际上,我当时编写了代码,但忘记更新此线程...我使用 Kat 指向的代码,对透明度的处理进行了一些细化,并保存为 PNG 格式而不是 Gif 格式。 它适用于制作具有全有或全无透明度的 8 位 PNG 文件。
您可以在 http://bazaar.launchpad.net/~philho/+junk/Java/view/head:/Tests/src/org/philhosoft/tests/image/AddTransparency.java
使用我的 ImageUtil 类。
由于代码不是那么大,为了后代的缘故,我将其发布在这里,没有 JavaDoc 来保存一些行。
It is an interesting question... It is late, I will experiment tomorrow. I will first try and use a BufferedImage.TYPE_BYTE_INDEXED (perhaps after drawing) to see if Java is smart enough to generate an 8bit PNG.
Or perhaps some image library can allow that.
[EDIT] Some years later... Actually, I made the code at the time, but forgot to update this thread... I used the code pointed at by Kat, with a little refinement on the handling of transparency, and saving in PNG format instead of Gif format. It works in making a 8-bit PNG file with all-or-nothing transparency.
You can find a working test file at http://bazaar.launchpad.net/~philho/+junk/Java/view/head:/Tests/src/org/philhosoft/tests/image/AddTransparency.java
using my ImageUtil class.
Since the code isn't that big, for posterity sake, I post it here, without the JavaDoc to save some lines.
感谢您的回复,我打算尝试使用 IndexColorModel 进行
TYPE_BYTE_INDEXED
,但如果 ImageIO 写出 32 位,那么我可能会在那里浪费时间。我试图写出的图像可能非常大(高达 8000x4000),但只是下面图像的一个简单蒙版,因此只有 ~30% 透明的灰色和 100% 透明的切口。 我会使用 GIF,但 IE6 似乎无法显示那么大的 GIF。
它仅在内部设置类型屏幕中生成一次,因此性能也不是问题,但它必须在 java 代码中完成,而不是由离线实用程序完成。
您指定的库可能会在编写时用于转换它......我要去检查一下。
如果谁有更好的方法请告诉我!!
谢谢!
Thanks for responding, I was going to try an
TYPE_BYTE_INDEXED
with an IndexColorModel and may still but if ImageIO writes out 32-bit regardless it appears that I may be wasting my time there.The image I am trying to write out can be very large (up to 8000x4000) but is just a simple mask for the image underneath, so will only have a ~30% transparent gray and a 100% transparent cutout. I would use a GIF but IE6 seems to have trouble with displaying one that large.
It only gets generated once and in an internal set-up type screen, so performance isn't an issue either, but it does have to be done within the java code and not by an offline utility.
The libraries that you specified might be used to transform it while writing... I am going to go check that out.
If anyone has a better way, please let me know!!
Thanks!