Here you can find the source of convertToBufferedImage(BufferedImage image, int imageType)
Parameter | Description |
---|---|
image | the image to convert |
imageType | image color format/model |
public static BufferedImage convertToBufferedImage(BufferedImage image, int imageType)
//package com.java2s; /* /*from w w w . j a va2 s .c om*/ * ImageUtils.java * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 2009?2012 Steinbeis Forschungszentrum (STZ ?lbronn), * Copyright (c) 2006?2012 by Michael Hoffer * * This file is part of Visual Reflection Library (VRL). * * VRL is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 * as published by the Free Software Foundation. * * see: http://opensource.org/licenses/LGPL-3.0 * file://path/to/VRL/src/eu/mihosoft/vrl/resources/license/lgplv3.txt * * VRL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * This version of VRL includes copyright notice and attribution requirements. * According to the LGPL this information must be displayed even if you modify * the source code of VRL. Neither the VRL Canvas attribution icon nor any * copyright statement/attribution may be removed. * * Attribution Requirements: * * If you create derived work you must do three things regarding copyright * notice and author attribution. * * First, the following text must be displayed on the Canvas: * "based on VRL source code". In this case the VRL canvas icon must be removed. * * Second, the copyright notice must remain. It must be reproduced in any * program that uses VRL. * * Third, add an additional notice, stating that you modified VRL. In addition * you must cite the publications listed below. A suitable notice might read * "VRL source code modified by YourName 2012". * * Note, that these requirements are in full accordance with the LGPL v3 * (see 7. Additional Terms, b). * * Publications: * * M. Hoffer, C.Poliwoda, G.Wittum. Visual Reflection Library - * A Framework for Declarative GUI Programming on the Java Platform. * Computing and Visualization in Science, 2011, in press. */ 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 javax.swing.ImageIcon; public class Main { /** * Converts an image to a buffered image with specified color model/format. * * @param image the image to convert * @param imageType image color format/model * @return the converted image */ public static BufferedImage convertToBufferedImage(BufferedImage image, int imageType) { BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), imageType); Graphics2D g2 = result.createGraphics(); g2.drawImage(image, null, 0, 0); return result; } /** * Converts image to buffered image. * * @param image image to convert * @return image as buffered image */ public static BufferedImage convertToBufferedImage(Image image) { // based on http://www.exampledepot.com/egs/java.awt.image/image2buf.html 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 = false; //hasAlpha(image); // Create a buffered image with a format that's compatible with the screen BufferedImage bimage = null; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); try { // Determine the type of transparency of the new buffered image int transparency = Transparency.OPAQUE; if (hasAlpha) { transparency = Transparency.BITMASK; } // 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; } /** * Returns a new compatible image. * * @param width the image width * @param height the image height * @return the compatible image */ public static BufferedImage createCompatibleImage(int width, int height) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration(); BufferedImage out = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT); return out; } }