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 boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer);

Source Link

Document

Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.

Usage

From source file:net.mindengine.oculus.frontend.web.controllers.project.ProjectEditController.java

private static BufferedImage resize(BufferedImage image, int width, int height) {
    BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = resizedImage.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    g.drawImage(image, 0, 0, width, height, null);
    g.dispose();//  w ww  . j a  v a 2s . com
    return resizedImage;
}

From source file:net.sqs2.omr.session.logic.PageImageRenderer.java

private static void copyImage(BufferedImage src, BufferedImage image) {
    Graphics2D g = (Graphics2D) image.getGraphics();
    int w = image.getWidth();
    int h = image.getHeight();
    g.drawImage(src, 0, 0, w, h, null);
    src.flush();/*from   www. ja va2s. co m*/
}

From source file:org.elasticwarehouse.core.parsers.FileTools.java

@SuppressWarnings("unused")
private static BufferedImage resizeImage(BufferedImage originalImage, int type, int IMG_WIDTH, int IMG_HEIGHT) {
    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
    g.dispose();//  w  w  w.  ja  v a 2 s  . c om

    return resizedImage;
}

From source file:org.elasticwarehouse.core.parsers.FileTools.java

private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type, int IMG_WIDTH,
        int IMG_HEIGHT) {

    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
    g.dispose();//w ww  . jav a 2 s .  c  om
    g.setComposite(AlphaComposite.Src);

    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    return resizedImage;
}

From source file:org.apache.stratos.theme.mgt.ui.processors.AddThemeResourceProcessor.java

private static DataHandler scaleImage(DataHandler dataHandler, int height, int width) throws IOException {

    Image image = ImageIO.read(new BufferedInputStream(dataHandler.getInputStream()));
    // Check if the image has transparent pixels
    boolean hasAlpha = ((BufferedImage) image).getColorModel().hasAlpha();

    // Maintain Aspect ratio
    int thumbHeight = height;
    int thumbWidth = width;
    double thumbRatio = (double) width / (double) height;
    double imageRatio = (double) image.getWidth(null) / (double) image.getHeight(null);
    if (thumbRatio < imageRatio) {
        thumbHeight = (int) (thumbWidth / imageRatio);
    } else {//w  w  w  .j  a va  2 s . c  om
        thumbWidth = (int) (thumbHeight * imageRatio);
    }

    BufferedImage thumb;
    // Check if transparent pixels are available and set the color mode accordingly 
    if (hasAlpha) {
        thumb = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB);
    } else {
        thumb = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
    }
    Graphics2D graphics2D = thumb.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

    // Save the image as PNG so that transparent images are rendered as intended
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ImageIO.write(thumb, "PNG", output);

    DataSource dataSource = new ByteArrayDataSource(output.toByteArray(), "application/octet-stream");
    return new DataHandler(dataSource);
}

From source file:ch.rasc.downloadchart.DownloadChartServlet.java

private static void writeImage(HttpServletResponse response, byte[] imageData, Integer width, Integer height,
        String formatName) throws IOException {

    BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageData));
    Dimension newDimension = calculateDimension(originalImage, width, height);

    if (newDimension != null) {
        int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
        BufferedImage resizedImage = new BufferedImage(newDimension.width, newDimension.height, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, newDimension.width, newDimension.height, null);
        g.dispose();/*from w  w w .ja v  a2 s.c  o  m*/
        g.setComposite(AlphaComposite.Src);

        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        ImageIO.write(resizedImage, formatName, response.getOutputStream());
    } else {
        if ("png".equals(formatName)) {
            response.getOutputStream().write(imageData);
        } else {
            ImageIO.write(originalImage, "gif", response.getOutputStream());
        }
    }
}

From source file:com.sun.socialsite.util.ImageUtil.java

/**
 * Convenience method that returns a scaled instance of the
 * provided {@code BufferedImage}./*w w  w. j av  a 2  s. com*/
 *
 * NOTE: Adapted from code at
 * {@link http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html}.
 *
 * @param img the original image to be scaled
 * @param targetWidth the desired width of the scaled instance,
 *    in pixels
 * @param targetHeight the desired height of the scaled instance,
 *    in pixels
 * @param hint one of the rendering hints that corresponds to
 *    {@code RenderingHints.KEY_INTERPOLATION} (e.g.
 *    {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR},
 *    {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR},
 *    {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC})
 * @param higherQuality if true, this method will use a multi-step
 *    scaling technique that provides higher quality than the usual
 *    one-step technique (only useful in downscaling cases, where
 *    {@code targetWidth} or {@code targetHeight} is
 *    smaller than the original dimensions, and generally only when
 *    the {@code BILINEAR} hint is specified)
 * @return a scaled version of the original {@code BufferedImage}
 */
private static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight,
        Object hint, boolean higherQuality) {
    int type = BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = (BufferedImage) img;
    int w, h;
    if (higherQuality) {
        // Use multi-step technique: start with original size, then
        // scale down in multiple passes with drawImage()
        // until the target size is reached
        w = img.getWidth();
        h = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        w = targetWidth;
        h = targetHeight;
    }

    do {
        if (higherQuality && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }

        if (higherQuality && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }

        BufferedImage tmp = new BufferedImage(w, h, type);
        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();

        ret = tmp;
    } while (w > targetWidth || h > targetHeight);

    return ret;
}

From source file:FileUtils.java

/**
 * Convenience method that returns a scaled instance of the
 * provided {@code BufferedImage}./*from   w  w w .j  ava2  s. c o  m*/
 *
 * @param img           the original image to be scaled
 * @param targetWidth   the desired width of the scaled instance,
 *                      in pixels
 * @param targetHeight  the desired height of the scaled instance,
 *                      in pixels
 * @param hint          one of the rendering hints that corresponds to
 *                      {@code RenderingHints.KEY_INTERPOLATION} (e.g.
 *                      {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR},
 *                      {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR},
 *                      {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC})
 * @param higherQuality if true, this method will use a multi-step
 *                      scaling technique that provides higher quality than the usual
 *                      one-step technique (only useful in downscaling cases, where
 *                      {@code targetWidth} or {@code targetHeight} is
 *                      smaller than the original dimensions, and generally only when
 *                      the {@code BILINEAR} hint is specified)
 * @return a scaled version of the original {@code BufferedImage}
 */
public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    final int type = img.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = (BufferedImage) img;
    int w;
    int h;
    if (higherQuality) {
        // Use multi-step technique: start with original size, then
        // scale down in multiple passes with drawImage()
        // until the target size is reached
        w = img.getWidth();
        h = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        w = targetWidth;
        h = targetHeight;
    }

    do {
        if (higherQuality && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }

        if (higherQuality && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }

        final BufferedImage tmp = new BufferedImage(w, h, type);
        final Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();

        ret = tmp;
    } while (w != targetWidth || h != targetHeight);

    return ret;
}

From source file:com.jaeksoft.searchlib.util.ImageUtils.java

public final static BufferedImage reduceImage(BufferedImage image, int width, int height) {
    int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = (BufferedImage) image;
    int w = image.getWidth();
    int h = image.getHeight();

    while (w != width || h != height) {
        if (w > width) {
            w /= 2;//from w  ww . j av  a  2s.c  o  m
            if (w < width)
                w = width;
        }

        if (h > height) {
            h /= 2;
            if (h < height)
                h = height;
        }

        BufferedImage tmp = new BufferedImage(w, h, type);

        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();
        ret = tmp;
    }
    return ret;
}

From source file:net.sourceforge.subsonic.controller.CoverArtController.java

public static BufferedImage scale(BufferedImage image, int width, int height) {
    int w = image.getWidth();
    int h = image.getHeight();
    BufferedImage thumb = image;//from ww  w . j av a 2  s .c  o m

    // For optimal results, use step by step bilinear resampling - halfing the size at each step.
    do {
        w /= 2;
        h /= 2;
        if (w < width) {
            w = width;
        }
        if (h < height) {
            h = height;
        }

        BufferedImage temp = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = temp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(thumb, 0, 0, temp.getWidth(), temp.getHeight(), null);
        g2.dispose();

        thumb = temp;
    } while (w != width);

    return thumb;
}