在java中,如何将java.awt.image.BufferedImage写入8位png文件?

发布于 2024-07-08 02:48:40 字数 1223 浏览 6 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

奢欲 2024-07-15 02:48:40

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.

渔村楼浪 2024-07-15 02:48:40

我在这里找到了如何将 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.

听闻余生 2024-07-15 02:48:40

这是一个有趣的问题...现在很晚了,我明天实验一下。 我将首先尝试使用 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 来保存一些行。

public class ImageUtil
{
  public static int ALPHA_BIT_MASK = 0xFF000000;

  public static BufferedImage imageToBufferedImage(Image image, int width, int height)
  {
    return imageToBufferedImage(image, width, height, BufferedImage.TYPE_INT_ARGB);
  }

  public static BufferedImage imageToBufferedImage(Image image, int width, int height, int type)
  {
    BufferedImage dest = new BufferedImage(width, height, type);
    Graphics2D g2 = dest.createGraphics();
    g2.drawImage(image, 0, 0, null);
    g2.dispose();
    return dest;
  }

  public static BufferedImage convertRGBAToIndexed(BufferedImage srcImage)
  {
    // Create a non-transparent palletized image
    Image flattenedImage = transformTransparencyToMagenta(srcImage);
    BufferedImage flatImage = imageToBufferedImage(flattenedImage,
        srcImage.getWidth(), srcImage.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
    BufferedImage destImage = makeColorTransparent(flatImage, 0, 0);
    return destImage;
  }

  private static Image transformTransparencyToMagenta(BufferedImage image)
  {
    ImageFilter filter = new RGBImageFilter()
    {
      @Override
      public final int filterRGB(int x, int y, int rgb)
      {
        int pixelValue = 0;
        int opacity = (rgb & ALPHA_BIT_MASK) >>> 24;
        if (opacity < 128)
        {
          // Quite transparent: replace color with transparent magenta
          // (traditional color for binary transparency)
          pixelValue = 0x00FF00FF;
        }
        else
        {
          // Quite opaque: get pure color
          pixelValue = (rgb & 0xFFFFFF) | ALPHA_BIT_MASK;
        }
        return pixelValue;
      }
    };

    ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
      return Toolkit.getDefaultToolkit().createImage(ip);
  }

  public static BufferedImage makeColorTransparent(BufferedImage image, int x, int y)
  {
    ColorModel cm = image.getColorModel();
    if (!(cm instanceof IndexColorModel))
      return image; // No transparency added as we don't have an indexed image

    IndexColorModel originalICM = (IndexColorModel) cm;
    WritableRaster raster = image.getRaster();
    int colorIndex = raster.getSample(x, y, 0); // colorIndex is an offset in the palette of the ICM'
    // Number of indexed colors
    int size = originalICM.getMapSize();
    byte[] reds = new byte[size];
    byte[] greens = new byte[size];
    byte[] blues = new byte[size];
    originalICM.getReds(reds);
    originalICM.getGreens(greens);
    originalICM.getBlues(blues);
    IndexColorModel newICM = new IndexColorModel(8, size, reds, greens, blues, colorIndex);
    return new BufferedImage(newICM, raster, image.isAlphaPremultiplied(), null);
  }
}

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.

public class ImageUtil
{
  public static int ALPHA_BIT_MASK = 0xFF000000;

  public static BufferedImage imageToBufferedImage(Image image, int width, int height)
  {
    return imageToBufferedImage(image, width, height, BufferedImage.TYPE_INT_ARGB);
  }

  public static BufferedImage imageToBufferedImage(Image image, int width, int height, int type)
  {
    BufferedImage dest = new BufferedImage(width, height, type);
    Graphics2D g2 = dest.createGraphics();
    g2.drawImage(image, 0, 0, null);
    g2.dispose();
    return dest;
  }

  public static BufferedImage convertRGBAToIndexed(BufferedImage srcImage)
  {
    // Create a non-transparent palletized image
    Image flattenedImage = transformTransparencyToMagenta(srcImage);
    BufferedImage flatImage = imageToBufferedImage(flattenedImage,
        srcImage.getWidth(), srcImage.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
    BufferedImage destImage = makeColorTransparent(flatImage, 0, 0);
    return destImage;
  }

  private static Image transformTransparencyToMagenta(BufferedImage image)
  {
    ImageFilter filter = new RGBImageFilter()
    {
      @Override
      public final int filterRGB(int x, int y, int rgb)
      {
        int pixelValue = 0;
        int opacity = (rgb & ALPHA_BIT_MASK) >>> 24;
        if (opacity < 128)
        {
          // Quite transparent: replace color with transparent magenta
          // (traditional color for binary transparency)
          pixelValue = 0x00FF00FF;
        }
        else
        {
          // Quite opaque: get pure color
          pixelValue = (rgb & 0xFFFFFF) | ALPHA_BIT_MASK;
        }
        return pixelValue;
      }
    };

    ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
      return Toolkit.getDefaultToolkit().createImage(ip);
  }

  public static BufferedImage makeColorTransparent(BufferedImage image, int x, int y)
  {
    ColorModel cm = image.getColorModel();
    if (!(cm instanceof IndexColorModel))
      return image; // No transparency added as we don't have an indexed image

    IndexColorModel originalICM = (IndexColorModel) cm;
    WritableRaster raster = image.getRaster();
    int colorIndex = raster.getSample(x, y, 0); // colorIndex is an offset in the palette of the ICM'
    // Number of indexed colors
    int size = originalICM.getMapSize();
    byte[] reds = new byte[size];
    byte[] greens = new byte[size];
    byte[] blues = new byte[size];
    originalICM.getReds(reds);
    originalICM.getGreens(greens);
    originalICM.getBlues(blues);
    IndexColorModel newICM = new IndexColorModel(8, size, reds, greens, blues, colorIndex);
    return new BufferedImage(newICM, raster, image.isAlphaPremultiplied(), null);
  }
}
缺⑴份安定 2024-07-15 02:48:40

感谢您的回复,我打算尝试使用 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文