Java: Loading png images without indexing (as BufferedImage.TYPE_4BYTE_ABGR), using javax.imageio.ImageIO.read()
I am trying to load a PNG image using the javax.imageio.ImageIO.rea开发者_Python百科d() 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.
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);
精彩评论