Java Image to BufferedImage imageToBufferedImage(Image image)

Here you can find the source of imageToBufferedImage(Image image)

Description

Converts an Image to a BufferedImage .

License

Open Source License

Parameter

Parameter Description
image Image

Return

A BufferedImage

Declaration

public static BufferedImage imageToBufferedImage(Image image) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors://from  ww w . j  av a2  s . c om
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

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.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;

import javax.swing.ImageIcon;

public class Main {
    /**
     * Converts an {@code Image} to a {@code BufferedImage}.
     *
     * @param image {@code Image}
     * @return A {@code BufferedImage}
     */
    public static BufferedImage imageToBufferedImage(Image image) {
        if (image instanceof BufferedImage)
            return (BufferedImage) image;

        image = new ImageIcon(image).getImage();

        boolean hasAlpha = hasAlpha(image);
        BufferedImage bi = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        try {
            int transparency = Transparency.OPAQUE;
            if (hasAlpha == true)
                transparency = Transparency.BITMASK;

            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();
            bi = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
        } catch (HeadlessException e) {
        }

        if (bi == null) {
            int type = BufferedImage.TYPE_INT_RGB;
            if (hasAlpha == true)
                type = BufferedImage.TYPE_INT_ARGB;

            bi = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        }

        Graphics g = bi.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();

        return bi;
    }

    private static boolean hasAlpha(Image image) {
        if (image instanceof BufferedImage)
            return ((BufferedImage) image).getColorModel().hasAlpha();

        PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
        }

        return pg.getColorModel().hasAlpha();
    }
}

Related

  1. getBufferedImage(Image image)
  2. getBufferedImage(Image image)
  3. getBufferedImage(Image img)
  4. getBufferedImage(java.awt.Image image)
  5. getScaledBufferedImage(Image icon, double scale)
  6. ImageToBufferedImage(Image image, int width, int height)
  7. imageToBufferedImage(Image img)
  8. imageToBufferedImage(Image img)
  9. imageToBufferedImage(Image pImage)