Converting an ImageIcon to a BufferedImage

2019-01-24 07:23发布

问题:

I've been trying to convert a ImageIcon to BufferedImage... And I've had no luck.

I have a pre-existing ImageIcon that needs to be converted to a Buffered Image for the vast amount of BufferedImage operations that exist.

I have found a few ways, but all of them are hugely CPU intensive.

回答1:

What's wrong with:

BufferedImage bi = new BufferedImage(
    icon.getIconWidth(),
    icon.getIconHeight(),
    BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics();
// paint the Icon to the BufferedImage.
icon.paintIcon(null, g, 0,0);
g.dispose();


回答2:

See ImageIcon, Image and BufferedImage:

ImageIcon yourImage;
Image image = yourImage.getImage();
BufferedImage buffered = (BufferedImage) image;


回答3:

I tried something called Scalr, view the code below

Scalr.resize((BufferedImage) ImageIO.read(file), Method.SPEED, 250, OP_ANTIALIAS, OP_BRIGHTER);

Cheers.