Example usage for java.awt Graphics drawImage

List of usage examples for java.awt Graphics drawImage

Introduction

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

Prototype

public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer);

Source Link

Document

Draws as much of the specified image as is currently available.

Usage

From source file:de.fhg.igd.swingrcp.SwingRCPUtilities.java

/**
 * Applies the given transparency mask to a buffered image. Always creates a
 * new buffered image containing an alpha channel. Copies the old image into
 * the new one and then sets the alpha pixels according to the given mask.
 * /*  www. j a  v a2 s .c  o m*/
 * @param img the old image
 * @param mask the alpha mask
 * @return the new image with alpha channel applied
 * @throws IllegalArgumentException if the image's size does not match the
 *             mask's size
 */
public static BufferedImage applyTransparencyMask(BufferedImage img, ImageData mask) {
    if (mask.width != img.getWidth() || mask.height != img.getHeight()) {
        throw new IllegalArgumentException("Image size does not match the mask size");
    }

    // copy image and also convert to RGBA
    BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics g = result.getGraphics();
    g.drawImage(img, 0, 0, null);

    WritableRaster alphaRaster = result.getAlphaRaster();
    int alpha0[] = new int[] { 0 };
    int alpha255[] = new int[] { 255 };
    for (int y = 0; y < img.getHeight(); y++) {
        for (int x = 0; x < img.getWidth(); x++) {
            alphaRaster.setPixel(x, y, mask.getPixel(x, y) == 0 ? alpha0 : alpha255);
        }
    }

    return result;
}

From source file:com.mirth.connect.server.util.DICOMUtil.java

private static byte[] saveAsJpeg(ImagePlus imagePlug, int quality) {
    int imageType = BufferedImage.TYPE_INT_RGB;

    if (imagePlug.getProcessor().isDefaultLut()) {
        imageType = BufferedImage.TYPE_BYTE_GRAY;
    }//from   w  w  w. ja v a2 s .  c o  m

    BufferedImage bufferedImage = new BufferedImage(imagePlug.getWidth(), imagePlug.getHeight(), imageType);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try {
        Graphics graphics = bufferedImage.createGraphics();
        graphics.drawImage(imagePlug.getImage(), 0, 0, null);
        graphics.dispose();
        ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
        writer.setOutput(ImageIO.createImageOutputStream(baos));
        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(quality / 100f);

        if (quality == 100) {
            param.setSourceSubsampling(1, 1, 0, 0);
        }

        IIOImage iioImage = new IIOImage(bufferedImage, null, null);
        writer.write(null, iioImage, param);
        return baos.toByteArray();
    } catch (Exception e) {
        logger.error("Error converting dcm file", e);
    } finally {
        IOUtils.closeQuietly(baos);
    }

    return null;
}

From source file:com.mirth.connect.server.util.DICOMMessageUtil.java

private static byte[] saveAsJpeg(ImagePlus imagePlug, int quality) {
    int imageType = BufferedImage.TYPE_INT_RGB;

    if (imagePlug.getProcessor().isDefaultLut()) {
        imageType = BufferedImage.TYPE_BYTE_GRAY;
    }//from   w  w w  .  j  a va  2  s.  co  m

    BufferedImage bufferedImage = new BufferedImage(imagePlug.getWidth(), imagePlug.getHeight(), imageType);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try {
        Graphics graphics = bufferedImage.createGraphics();
        graphics.drawImage(imagePlug.getImage(), 0, 0, null);
        graphics.dispose();
        ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
        writer.setOutput(ImageIO.createImageOutputStream(baos));
        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(quality / 100f);

        if (quality == 100) {
            param.setSourceSubsampling(1, 1, 0, 0);
        }

        IIOImage iioImage = new IIOImage(bufferedImage, null, null);
        writer.write(null, iioImage, param);
        return baos.toByteArray();
    } catch (Exception e) {
        logger.error("Error Converting DICOM image", e);
    } finally {
        IOUtils.closeQuietly(baos);
    }

    return null;
}

From source file:com.igormaznitsa.sciareto.ui.UiUtils.java

@Nonnull
public static Image makeBadgedRightBottom(@Nonnull final Image base, @Nonnull final Image badge) {
    final int width = Math.max(base.getWidth(null), badge.getWidth(null));
    final int height = Math.max(base.getHeight(null), badge.getHeight(null));
    final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final Graphics gfx = result.getGraphics();
    try {//from ww  w.  j a v a  2  s  .  co m
        gfx.drawImage(base, (width - base.getWidth(null)) / 2, (height - base.getHeight(null)) / 2, null);
        gfx.drawImage(badge, width - badge.getWidth(null) - 1, height - badge.getHeight(null) - 1, null);
    } finally {
        gfx.dispose();
    }
    return result;
}

From source file:com.igormaznitsa.sciareto.ui.UiUtils.java

@Nonnull
public static Image makeBadgedRightTop(@Nonnull final Image base, @Nonnull final Image badge) {
    final int width = Math.max(base.getWidth(null), badge.getWidth(null));
    final int height = Math.max(base.getHeight(null), badge.getHeight(null));
    final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final Graphics gfx = result.getGraphics();
    try {/*  w  w w. java 2  s  . c o  m*/
        gfx.drawImage(base, (width - base.getWidth(null)) / 2, (height - base.getHeight(null)) / 2, null);
        gfx.drawImage(badge, width - badge.getWidth(null) - 1, 1, null);
    } finally {
        gfx.dispose();
    }
    return result;
}

From source file:com.reydentx.core.common.PhotoUtils.java

public static byte[] waterMarkJPG(String baseImagePath, String waterMartPath) {
    try {/*from ww w  .j  ava2 s  .com*/
        File origFile = new File(baseImagePath);
        ImageIcon icon = new ImageIcon(origFile.getPath());
        BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bufferedImage.getGraphics();
        graphics.drawImage(icon.getImage(), 0, 0, null);

        ImageIcon png = new ImageIcon(waterMartPath);
        graphics.drawImage(png.getImage(), 0, 0, null);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "jpg", baos);
        baos.flush();
        byte[] imageInByte = baos.toByteArray();

        return imageInByte;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.geoserver.wms.WMSTestSupport.java

/**
 * Shows <code>image</code> in a Frame.
 * //from  www  .  ja va2  s  .  co m
 * @param frameName
 * @param timeOut
 * @param image
 */
public static void showImage(String frameName, long timeOut, final BufferedImage image) {
    int width = image.getWidth();
    int height = image.getHeight();

    if (((System.getProperty("java.awt.headless") == null)
            || !System.getProperty("java.awt.headless").equals("true")) && INTERACTIVE) {
        Frame frame = new Frame(frameName);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                e.getWindow().dispose();
            }
        });

        Panel p = new Panel(null) { // no layout manager so it respects
                                    // setSize
            public void paint(Graphics g) {
                g.drawImage(image, 0, 0, this);
            }
        };

        frame.add(p);
        p.setSize(width, height);
        frame.pack();
        frame.setVisible(true);

        try {
            Thread.sleep(timeOut);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        frame.dispose();
    }
}

From source file:org.jfree.experimental.swt.SWTUtils.java

/**
 * Converts an AWT image to SWT./*from  w  w  w  .  j  ava2s .  co  m*/
 *
 * @param image  the image (<code>null</code> not permitted).
 *
 * @return Image data.
 */
public static ImageData convertAWTImageToSWT(Image image) {
    ParamChecks.nullNotPermitted(image, "image");
    int w = image.getWidth(null);
    int h = image.getHeight(null);
    if (w == -1 || h == -1) {
        return null;
    }
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return convertToSWT(bi);
}

From source file:de.darkblue.bongloader2.utils.ToolBox.java

public static BufferedImage grayScale(BufferedImage image) {
    BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(),
            BufferedImage.TYPE_BYTE_GRAY);
    Graphics g = newImage.getGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();//from w  w  w  .j a v  a2 s  .  c o  m

    return newImage;
}

From source file:de.mpg.imeji.logic.storage.util.ImageUtils.java

/**
 * Scale a {@link BufferedImage} to new size. Is faster than the basic {@link ImageUtils}.scaleImage method, has the
 * same quality. If it is a thumbnail, cut the images to fit into the raster
 * //from www.j  a v  a2 s .  c o m
 * @param image original image
 * @param size the size to be resized to
 * @param resolution the type of the image. Might be thumb or web
 * @return the resized images
 * @throws Exception
 */
public static BufferedImage scaleImageFast(BufferedImage image, int size, FileResolution resolution)
        throws Exception {
    int width = image.getWidth(null);
    int height = image.getHeight(null);
    BufferedImage newImg = null;
    Image rescaledImage;
    if (width > height) {
        if (FileResolution.THUMBNAIL.equals(resolution)) {
            newImg = new BufferedImage(height, height, BufferedImage.TYPE_INT_RGB);
            Graphics g1 = newImg.createGraphics();
            g1.drawImage(image, (height - width) / 2, 0, null);
            if (height > size)
                rescaledImage = getScaledInstance(newImg, size, size,
                        RenderingHints.VALUE_INTERPOLATION_BILINEAR, RESCALE_HIGH_QUALITY);
            else
                rescaledImage = newImg;
        } else
            rescaledImage = getScaledInstance(image, size, height * size / width,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR, RESCALE_HIGH_QUALITY);
    } else {
        if (FileResolution.THUMBNAIL.equals(resolution)) {
            newImg = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB);
            Graphics g1 = newImg.createGraphics();
            g1.drawImage(image, 0, (width - height) / 2, null);
            if (width > size)
                rescaledImage = getScaledInstance(newImg, size, size,
                        RenderingHints.VALUE_INTERPOLATION_BILINEAR, RESCALE_HIGH_QUALITY);
            else
                rescaledImage = newImg;
        } else
            rescaledImage = getScaledInstance(image, width * size / height, size,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR, RESCALE_HIGH_QUALITY);
    }
    BufferedImage rescaledBufferedImage = new BufferedImage(rescaledImage.getWidth(null),
            rescaledImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
    Graphics g2 = rescaledBufferedImage.getGraphics();
    g2.drawImage(rescaledImage, 0, 0, null);
    return rescaledBufferedImage;
}