Java: Loading png images without indexing (as BufferedImage.TYPE_4BYTE_ABGR), using javax.imageio.ImageIO.read()

风流意气都作罢 提交于 2019-12-12 05:59:05

问题


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.


回答1:


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",...) operation with a rendering hint with a key of JAI.KEY_REPLACE_INDEX_COLOR_MODEL.

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);


来源:https://stackoverflow.com/questions/5686519/java-loading-png-images-without-indexing-as-bufferedimage-type-4byte-abgr-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!