Java Image Create createImageCopy(Image image)

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

Description

create Image Copy

License

Apache License

Declaration

public static BufferedImage createImageCopy(Image image) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.Graphics;
import java.awt.Graphics2D;
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.ColorModel;

import java.awt.image.PixelGrabber;

import javax.swing.ImageIcon;

public class Main {
    public static BufferedImage createImageCopy(Image image) {
        return createImageCopy(image, false);
    }//from  w  w w .  j ava 2 s.  com

    public static BufferedImage createImageCopy(Image image, boolean isTranslucent) {
        // if (image instanceof BufferedImage) return (BufferedImage)image;
        // This code ensures that all the pixels in the image are loaded
        image = new ImageIcon(image).getImage();

        // Determine if the image has transparent pixels; for this method's
        // implementation, see Determining If an Image Has Transparent Pixels
        boolean hasAlpha = hasAlpha(image);

        // System.out.println("Image Has Alpha: "+hasAlpha);
        // Create a buffered image with a format that's compatible with the
        // screen
        BufferedImage bimage = createImage(image.getWidth(null), image.getHeight(null), hasAlpha, isTranslucent);

        // 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;
    }

    public static boolean hasAlpha(Image image) {
        // Get the image's color model
        ColorModel cm = getColorModel(image);
        // System.out.println("Color Model Class: "+(cm.getClass().getName())+" Data: "+cm);
        return cm.hasAlpha();
    }

    public static BufferedImage createImage(int imgWt, int imgHt, boolean hasAlpha) {
        return createImage(imgWt, imgHt, hasAlpha, false);
    }

    public static BufferedImage createImage(int imgWt, int imgHt, boolean hasAlpha, boolean translucent) {
        // Create a buffered image with a format that's compatible with the
        // screen
        BufferedImage bimage = null;
        try {
            // Determine the type of transparency of the new buffered image
            int transparency = Transparency.OPAQUE;
            if (hasAlpha)
                transparency = Transparency.BITMASK;
            if (translucent)
                transparency = Transparency.TRANSLUCENT;
            // Create the buffered image
            GraphicsConfiguration gc = getDefaultConfiguration();
            bimage = gc.createCompatibleImage(imgWt, imgHt, transparency);
            // RGPTLogger.logToFile("Created Image using GraphicsConfiguration");
        } catch (HeadlessException e) {
            e.printStackTrace();
        }

        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(imgWt, imgHt, type);
        }
        return bimage;
    }

    public static void drawImage(BufferedImage srcImg, BufferedImage img2Draw, int w, int h) {
        if (w == -1)
            w = (int) (srcImg.getWidth() / 2);
        if (h == -1)
            h = (int) (srcImg.getHeight() / 2);
        System.out.println("AWT Image Wt: " + w + " And Ht: " + h);
        Graphics2D g2 = srcImg.createGraphics();
        g2.drawImage(img2Draw, w, h, null);
        g2.dispose();
    }

    public static ColorModel getColorModel(Image image) {
        // If buffered image, the color model is readily available
        if (image instanceof BufferedImage) {
            BufferedImage bimage = (BufferedImage) image;
            return bimage.getColorModel();
        }

        // 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;
    }

    public static GraphicsConfiguration getDefaultConfiguration() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        return gd.getDefaultConfiguration();
    }
}

Related

  1. createImage(Component comp, Icon icon)
  2. createImage(Icon icon)
  3. createImage(JPanel panel)
  4. createImage(String str)
  5. createImage(URL url, String description, boolean checkPresence)
  6. createImageFromBase64String(String s)
  7. createImageFromInvisibleComponent(JComponent component)
  8. createImageFromVisibleComponent(JComponent comp, int cw, int ch)
  9. createImagePanel(Image image, Color backColor)