Java BufferedImage Resize downsizeImage(Image imgSource, int w, int h)

Here you can find the source of downsizeImage(Image imgSource, int w, int h)

Description

Resizes the given image using a Graphics2D object backed by a BufferedImage object.

License

Open Source License

Parameter

Parameter Description
imgSource - source image to scale
w - desired width
h - desired height

Return

- the new resized image

Declaration

public static Image downsizeImage(Image imgSource, int w, int h) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

public class Main {
    /**//  w  w  w . j  a  v a 2 s  . co m
     * <p>
     * Resizes the given image using a 
     * <code>Graphics2D</code> object backed by a 
     * <code>BufferedImage</code> object.
     * This method is the main engine for all the 
     * image re-scaling methods in this class.
     * </p>
     * <p>
     * Use this method when the resultant image is smaller
     * than the original.
     * </p>
     * 
     * @param imgSource - source image to scale
     * @param w - desired width
     * @param h - desired height
     * 
     * @return - the new resized image
     * 
     * @since  Jun 15, 2009
     * @author Christopher K. Allen
     *
     */
    public static Image downsizeImage(Image imgSource, int w, int h) {
        BufferedImage imgResized = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2dResized = imgResized.createGraphics();

        //        g2dResized.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2dResized.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        //        g2dResized.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2dResized.drawImage(imgSource, 0, 0, w, h, null);
        g2dResized.dispose();

        return imgResized;
    }
}

Related

  1. fitImage(JLabel label, BufferedImage image)
  2. getResizedImage(BufferedImage image)
  3. getResizeFactor(BufferedImage source, int maxWidth, int maxHeight)
  4. imageResize(BufferedImage image, int width, int height)