Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

toBufferedImage example code

843806Nov 13 2008 — edited Jan 29 2009
Today I was having some issues with converting an Image to a BufferedImage. I figured out that there really isn't any built in methods around that will convert for you, however it isn't too difficult to do it yourself. Poking around the net, I did find an example that comes from the Java Developer's Almanac 1.4 -- which lists a static method 'toBufferedImage'. This code looked like it would do the trick, but I had a nagging feeling there was something not quite right about it.

Anyway, I copied the code examples and tried them out, and had much difficulties with them -- The BufferedImage, when displayed to the screen using a JLabel constructed from a new ImageIcon created from the buffered image resulted in nothing being painted to the screen.
Upon inspection of the BufferedImage, I found it had plenty of data in it's bufImg.raster.data data buffer, so it seemed it should be displaying properly...

After poking at it for a while, and trying some other images, I found that other images were displaying properly. Anyway, to cut to the chase, it seems that the original toBufferedImage code from the Java Developers Almanac 1.4 doesn't properly cover all cases of transparency in source images (it doesn't detect it right). It only handles if an image is Opaque, or has a BitMask. It doesn't handle the case where an image has > 1bit alpha channels (Transparency.Translucent). In my case, my image had some transparency on every single pixel in the image. The original toBufferedImage code just treated this image as if it had only a 1bit alpha channel, and, then treated every pixel as being entirely transparent.

So, I've now created a new version of this method that now works for images with >1bit alpha channels.

The original code can be had here:
http://www.exampledepot.com/egs/java.awt.image/Image2Buf.html
http://www.exampledepot.com/egs/java.awt.image/HasAlpha.html

and below is my modified code:
package org.kyzivat.utilities;

import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author kkyzivat
 */
public class ImageUtils {
    private static Logger logger = Logger.getLogger(ImageUtils.class.getName());

    /**
     * This method returns a buffered image with the contents of an image
     * Code from: http://www.exampledepot.com/egs/java.awt.image/Image2Buf.html
     * 
     * @param image the Image to convert to a BufferedImage
     * @return a new BufferedImage that contains the contents of the image passed in
     */
    public static BufferedImage toBufferedImage(Image image) {
        // Just to illustrate that this works for BufferedImage to BufferedImage
        // comment below short-circuit out (3 lines).
//        if (image instanceof BufferedImage) {
//            return (BufferedImage)image;
//        }

        // This code ensures that all the pixels in the image are loaded
        image = new ImageIcon(image).getImage();

        // Get the type of transparency this image is using.
        int transparency = getTransparency(image);
        // could also use hasAlpha(Image), but why waste the cycles?
        boolean hasAlpha = transparency != Transparency.OPAQUE; 

        // Create a buffered image with a format that's compatible with the screen
        BufferedImage bimage = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        try {
            // Create the buffered image
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();
            bimage = gc.createCompatibleImage(
                image.getWidth(null), image.getHeight(null), transparency);
        } catch (HeadlessException e) {
            // The system does not have a screen
        }

        if (bimage == null) {
            // Create a buffered image using the default color model
            int type = BufferedImage.TYPE_INT_RGB;
            if (hasAlpha) {
                type = BufferedImage.TYPE_INT_ARGB;
            }
            bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        }

        // Copy image to buffered image
        Graphics g = bimage.createGraphics();

        // Paint the image onto the buffered image
        g.drawImage(image, 0, 0, null);
        g.dispose();

        return bimage;
    }
    
    /**
     * This method returns true if the specified image has transparent pixels
     * Code from http://www.exampledepot.com/egs/java.awt.image/HasAlpha.html
     * 
     * @param image the image to determine the transparency of
     * @return <code>true</code> if image has an alpha layer
     */
    public static boolean hasAlpha(Image image) {
        // If buffered image, the color model is readily available
        if (image instanceof BufferedImage) {
            BufferedImage bimage = (BufferedImage)image;
            return bimage.getColorModel().hasAlpha();
        }

        // Use a pixel grabber to retrieve the image's color model;
        // grabbing a single pixel is usually sufficient
         PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
        }

        // Get the image's color model
        ColorModel cm = pg.getColorModel();
        return cm.hasAlpha();
    }
    
    /**
     * This method returns the type of transparency this image has, as specified
     * in the java.awt.Transparency interface.
     * 
     * @see java.awt.Transparency
     * @param image the image to determine the transparency of
     * @return an int specifying the type of transparency (opaque is one)
     */
    public static int getTransparency(Image image) {
        // If buffered image, the color model is readily available
        if (image instanceof BufferedImage) {
            BufferedImage bimage = (BufferedImage)image;
            return bimage.getColorModel().getTransparency();
        }

        // Use a pixel grabber to retrieve the image's color model;
        // grabbing a single pixel is usually sufficient
         PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
        }

        // Get the image's color model
        ColorModel cm = pg.getColorModel();
        return cm.getTransparency();
    }
    
    public static void main(String[] args) {
        final File imgFile = new File("images", "testImage.png");
        try {
            ImageIcon imgImgIOIcon = new ImageIcon(ImageIO.read(imgFile));
            BufferedImage biImgIo = ImageUtils.toBufferedImage(imgImgIOIcon.getImage());
            
            ImageIcon imgTkIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(imgFile.getPath()));
            BufferedImage biTk = ImageUtils.toBufferedImage(imgTkIcon.getImage());
            
            final JLabel labelImgIo = new JLabel(new ImageIcon(biImgIo));
            
            final JLabel labelTk = new JLabel(new ImageIcon(biTk));

            java.awt.EventQueue.invokeLater(new Runnable() {

                public void run() {
                    JFrame frame = new JFrame();
                    JPanel panel = new JPanel();
                    
                    panel.add(labelTk);
                    panel.add(labelImgIo);
                    frame.add(panel);
                    
                    frame.addWindowListener(new java.awt.event.WindowAdapter() {

                        @Override
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    frame.setSize(200,200);
                    frame.setVisible(true);
                }
            });
        } catch (IOException e) {
            logger.log(Level.SEVERE, String.format("Couldn't load image %s", imgFile.getPath()), e);
        }
    }
    
}
To make for efficient code, when you use this outside of testing, be sure to uncomment the lines:
//        if (image instanceof BufferedImage) {
//            return (BufferedImage)image;
//        }
I commented that out so the problem, and solution, could be made clear, as the ImageIO.read() method returns a BufferedImage -- which most folks use these days, and thus, it doesn't require any of the conversion shown.
However, I've been using the Swing Application Framework, and it's resource handling and loading returns Image objects that are ToolkitImage objects, which require this conversion.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 26 2009
Added on Nov 13 2008
2 comments
875 views