Example usage for java.awt Graphics2D drawImage

List of usage examples for java.awt Graphics2D drawImage

Introduction

In this page you can find the example usage for java.awt Graphics2D drawImage.

Prototype

public abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y);

Source Link

Document

Renders a BufferedImage that is filtered with a BufferedImageOp .

Usage

From source file:net.chris54721.infinitycubed.utils.Utils.java

public static BufferedImage toBufferedImage(Image img) {
    if (img instanceof BufferedImage)
        return (BufferedImage) img;
    BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D bGr = bimage.createGraphics();
    bGr.drawImage(img, 0, 0, null);
    bGr.dispose();/*  w w w  . ja  v  a  2 s.  c  o  m*/
    return bimage;
}

From source file:org.web4thejob.web.util.MediaUtil.java

public static BufferedImage createThumbnail(byte[] bytes) {
    Image image = getImage(bytes);
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(), image.getHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bufferedImage.createGraphics();
    g.drawImage(image.toImageIcon().getImage(), 0, 0, null);
    g.dispose();/* w  w  w . java2s.co  m*/
    return createThumbnail(bufferedImage);
}

From source file:org.jamwiki.utils.ImageUtil.java

/**
 * Convert a Java Image object to a Java BufferedImage object.
 *///from w  ww .j a v a2 s .  co m
private static BufferedImage imageToBufferedImage(Image image) throws Exception {
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = bufferedImage.createGraphics();
    graphics.drawImage(image, 0, 0, null);
    graphics.dispose();
    return bufferedImage;
}

From source file:it.smartcommunitylab.parking.management.web.manager.MarkerIconStorage.java

private static BufferedImage changeColor(BufferedImage image, Color mask, Color replacement) {
    BufferedImage destImage = new BufferedImage(image.getWidth(), image.getHeight(),
            BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = destImage.createGraphics();
    g.drawImage(image, null, 0, 0);
    g.dispose();//from ww w  . j  a  v  a 2s.  c om

    for (int i = 0; i < destImage.getWidth(); i++) {
        for (int j = 0; j < destImage.getHeight(); j++) {

            int destRGB = destImage.getRGB(i, j);

            if (matches(mask.getRGB(), destRGB)) {
                int rgbnew = getNewPixelRGB(replacement.getRGB(), destRGB);
                destImage.setRGB(i, j, rgbnew);
            }
        }
    }

    return destImage;
}

From source file:Transparency.java

public static BufferedImage makeImageTranslucent(BufferedImage source, double alpha) {
    BufferedImage target = new BufferedImage(source.getWidth(), source.getHeight(),
            java.awt.Transparency.TRANSLUCENT);
    // Get the images graphics
    Graphics2D g = target.createGraphics();
    // Set the Graphics composite to Alpha
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) alpha));
    // Draw the image into the prepared reciver image
    g.drawImage(source, null, 0, 0);
    // let go of all system resources in this Graphics
    g.dispose();//  w ww.j  a va  2s .c o  m
    // Return the image
    return target;
}

From source file:at.gv.egiz.pdfas.common.utils.ImageUtils.java

public static BufferedImage removeAlphaChannel(BufferedImage src) {
    //if (src.getColorModel().hasAlpha()) {
    BufferedImage image = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image.createGraphics();
    g.setComposite(AlphaComposite.Src);
    g.drawImage(src, 0, 0, null);
    g.dispose();/*  w w w. j  a v  a  2 s.c o  m*/
    return image;
    //}
    //return src;
    /*
     * BufferedImage rgbImage = new BufferedImage(src.getWidth(),
     * src.getHeight(), BufferedImage.TYPE_3BYTE_BGR); for (int x = 0; x <
     * src.getWidth(); ++x) { for (int y = 0; y < src.getHeight(); ++y) {
     * rgbImage.setRGB(x, y, src.getRGB(x, y) & 0xFFFFFF); } } return
     * rgbImage;
     */
}

From source file:Utils.java

/** 
 * Renders multiple paragraphs of text in an array to an image (created and returned). 
 *
 * @param font The font to use/*w ww  .j  ava 2s .  co m*/
 * @param textColor The color of the text
 * @param text The message in an array of strings (one paragraph in each
 * @param width The width the text should be limited to
 * @return An image with the text rendered into it
 */
public static BufferedImage renderTextToImage(Font font, Color textColor, String text[], int width) {
    LinkedList<BufferedImage> images = new LinkedList<BufferedImage>();

    int totalHeight = 0;

    for (String paragraph : text) {
        BufferedImage paraImage = renderTextToImage(font, textColor, paragraph, width);
        totalHeight += paraImage.getHeight();
        images.add(paraImage);
    }

    BufferedImage image = createCompatibleImage(width, totalHeight);
    Graphics2D graphics = (Graphics2D) image.createGraphics();

    int y = 0;

    for (BufferedImage paraImage : images) {
        graphics.drawImage(paraImage, 0, y, null);
        y += paraImage.getHeight();
    }

    graphics.dispose();
    return image;
}

From source file:net.algart.simagis.live.json.minimal.SimagisLiveUtils.java

private static BufferedImage toThumbnailSize(BufferedImage image) {
    final int w = image.getWidth();
    final int h = image.getHeight();
    final int max = Math.max(w, h);
    if (max <= 256) {
        return image;
    }// www.j a v  a  2 s . c  om
    final double s = max / 256d;
    final BufferedImage result = new BufferedImage(Math.max((int) Math.min(w / s, 256), 1),
            Math.max((int) Math.min(h / s, 256), 1), BufferedImage.TYPE_INT_BGR);
    final Graphics2D graphics = result.createGraphics();
    try {
        graphics.drawImage(image.getScaledInstance(result.getWidth(), result.getHeight(), Image.SCALE_SMOOTH),
                0, 0, null);
    } finally {
        graphics.dispose();
    }
    return result;
}

From source file:editeurpanovisu.ReadWriteImage.java

public static void writeTiff(Image imgImage, String strNomFich, boolean bSharpen, float sharpenLevel)
        throws ImageReadException, IOException {
    File file = new File(strNomFich);
    BufferedImage imageRGBSharpen = null;
    BufferedImage imageRGB = SwingFXUtils.fromFXImage(imgImage, null);

    Graphics2D graphics = imageRGB.createGraphics();
    graphics.drawImage(imageRGB, 0, 0, null);
    if (bSharpen) {
        imageRGBSharpen = new BufferedImage(imageRGB.getWidth(), imageRGB.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        Kernel kernel = new Kernel(3, 3, sharpenMatrix);
        ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        cop.filter(imageRGB, imageRGBSharpen);
    }//from ww w .  j  a v a  2 s  . c o m

    final ImageFormat format = ImageFormats.TIFF;
    final Map<String, Object> params = new HashMap<>();
    params.put(ImagingConstants.PARAM_KEY_COMPRESSION,
            new Integer(TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED));

    if (bSharpen) {
        try {
            Imaging.writeImage(imageRGBSharpen, file, format, params);
        } catch (ImageWriteException ex) {
            Logger.getLogger(ReadWriteImage.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        try {
            Imaging.writeImage(imageRGB, file, format, params);
        } catch (ImageWriteException ex) {
            Logger.getLogger(ReadWriteImage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

From source file:editeurpanovisu.ReadWriteImage.java

/**
 *
 * @param img/*  w  w w  . j  ava 2 s  . c  om*/
 * @param destFile
 * @param sharpen
 * @param sharpenLevel
 * @throws IOException
 */
public static void writePng(Image img, String destFile, boolean sharpen, float sharpenLevel)
        throws IOException {
    sharpenMatrix = calculeSharpenMatrix(sharpenLevel);
    BufferedImage imageRGBSharpen = null;
    IIOImage iioImage = null;
    BufferedImage image = SwingFXUtils.fromFXImage(img, null); // Get buffered image.
    BufferedImage imageRGB = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.BITMASK);
    Graphics2D graphics = imageRGB.createGraphics();
    graphics.drawImage(image, 0, 0, null);
    if (sharpen) {
        imageRGBSharpen = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
        Kernel kernel = new Kernel(3, 3, sharpenMatrix);
        ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        cop.filter(imageRGB, imageRGBSharpen);
    }
    ImageWriter writer = null;
    FileImageOutputStream output = null;
    try {
        writer = ImageIO.getImageWritersByFormatName("png").next();
        ImageWriteParam param = writer.getDefaultWriteParam();
        output = new FileImageOutputStream(new File(destFile));
        writer.setOutput(output);
        if (sharpen) {
            iioImage = new IIOImage(imageRGBSharpen, null, null);
        } else {
            iioImage = new IIOImage(imageRGB, null, null);
        }
        writer.write(null, iioImage, param);
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (writer != null) {
            writer.dispose();
        }
        if (output != null) {
            output.close();
        }
    }
    graphics.dispose();
}