changing BufferedImage type

HiCan I have any influence on the type of BufferedImage returned by ImageIO.read() method or maybe it can be changed later ? I keep getting TYPE_3BYTE_BGR but I'd like to get TYPE_INT_ARGB. Is it possible ?laryy
[233 byte] By [laryya] at [2007-11-26 20:08:41]
# 1

You can definately change it.

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class ConvertIt {

static int DESIRED_TYPE = BufferedImage.TYPE_INT_ARGB;

public static void main(String[] args) throws IOException {

String path = "images/cougar.jpg";

BufferedImage orig = ImageIO.read(new File(path));

System.out.println("orig type = " + orig.getType());

BufferedImage converted = null;

if(orig.getType() != DESIRED_TYPE) {

converted = convert(orig, DESIRED_TYPE);

JPanel panel = new JPanel();

panel.add(new JLabel(new ImageIcon(orig)));

panel.add(new JLabel(new ImageIcon(converted)));

JOptionPane.showMessageDialog(null, panel, "",

JOptionPane.PLAIN_MESSAGE);

} else {

JOptionPane.showMessageDialog(null, orig, "",

JOptionPane.PLAIN_MESSAGE);

}

}

private static BufferedImage convert(BufferedImage src, int type) {

System.out.println("converting to type " + type);

int w = src.getWidth();

int h = src.getHeight();

BufferedImage image = new BufferedImage(w, h, type);

Graphics2D g2 = image.createGraphics();

g2.drawRenderedImage(src, null);

g2.dispose();

return image;

}

}

crwooda at 2007-7-9 23:11:29 > top of Java-index,Security,Cryptography...
# 2

Yes that's a smart way to do it ;)

but I also found another way:

java.awt.image.ColorConvertOp cco = new java.awt.image.ColorConvertOp(null);

BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ABGR);

dest = cco.filter(src, dest);

Thank you for your answer.

laryya at 2007-7-9 23:11:29 > top of Java-index,Security,Cryptography...