Java:使用 javax.imageio.ImageIO.read() 在没有索引的情况下加载 png 图像(如 BufferedImage.TYPE_4BYTE_ABGR) [英] Java: Loading png images without indexing (as BufferedImage.TYPE_4BYTE_ABGR), using javax.imageio.ImageIO.read()

查看:119
本文介绍了Java:使用 javax.imageio.ImageIO.read() 在没有索引的情况下加载 png 图像(如 BufferedImage.TYPE_4BYTE_ABGR)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 javax.imageio.ImageIO.read() 方法加载 PNG 图像.但是,我希望结果类型为BufferedImage.TYPE_4BYTE_ABGR",但它最终会作为索引图像(BufferedImage.TYPE_BYTE_INDEXED").当原始图像被编入索引时,有没有办法将图像加载为未编入索引?大约有 120 张图像,因此手动将它们全部取消索引需要很长时间.

I am trying to load a PNG image using the javax.imageio.ImageIO.read() method. However, I want the resulting type to be "BufferedImage.TYPE_4BYTE_ABGR", but it ends up as an indexed image ("BufferedImage.TYPE_BYTE_INDEXED"). Is there any way to load an image as unindexed, when the original image is indexed? There are about 120 images, so it would take too long to make them all unindexed by hand.

推荐答案

如果你不反对使用 JAI,你可以为 RenderedImage 创建一个渲染链(BufferedImage 实现接口),并在链中添加格式化操作:

If you're not opposed to using JAI, you can create a rendering chain for the RenderedImage (BufferedImage implements the interface) and add a format operation to the chain:

带有渲染提示的 JAI.create("format",...) 操作,键为 JAI.KEY_REPLACE_INDEX_COLOR_MODEL.

JAI.create("format",...) operation with a rendering hint with a key of JAI.KEY_REPLACE_INDEX_COLOR_MODEL.

纯 ImageIO 方法是创建一个您想要的类型的新 BufferedImage 并将从 ImageIO.read 加载的那个绘制到新的 BufferedImage 中:

A pure-ImageIO approach would be to create a new BufferedImage of the type you want and draw the one loaded from ImageIO.read into the new BufferedImage:

BufferedImage image = ImageIO.read(inputFile);
BufferedImage convertedImage = new BufferedImage(image.getWidth(), 
    image.getHeight(), BufferedImage.TYPE_4BYTE_ABRG);
convertedImage.createGraphics().drawRenderedImage(image, null);

这篇关于Java:使用 javax.imageio.ImageIO.read() 在没有索引的情况下加载 png 图像(如 BufferedImage.TYPE_4BYTE_ABGR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆