Java Image Resize getResizedImage(Image originalImage, int newWidth, int newHeight, int imageType)

Here you can find the source of getResizedImage(Image originalImage, int newWidth, int newHeight, int imageType)

Description

Scales the specified image to the desired dimensions by specified type and the smooth scaling method

License

Apache License

Parameter

Parameter Description
originalImage the image to resize
newWidth the new imageWidth of the image
newHeight the new imageHeight of the image
imageType the type of BufferedImage

Return

the scaled image

Declaration

public static Image getResizedImage(Image originalImage, int newWidth, int newHeight, int imageType) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2013 See AUTHORS file.//from   w  w  w.ja  v  a  2  s .  c  om
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

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

public class Main {
    /**
     * Scales the specified image to the desired dimensions by specified type
     * and the smooth scaling method
     * 
     * @param originalImage the image to resize
     * @param newWidth the new imageWidth of the image
     * @param newHeight the new imageHeight of the image
     * @param imageType the type of {@code BufferedImage}
     * 
     * @return the scaled image
     */
    public static Image getResizedImage(Image originalImage, int newWidth, int newHeight, int imageType) {
        return getResizedImage(originalImage, newWidth, newHeight, imageType, Image.SCALE_SMOOTH);
    }

    /**
     * Scales the specified image to the desired dimensions by specified type
     * and the specified scaling method
     * 
     * @param originalImage the image to resize
     * @param newWidth the new imageWidth of the image
     * @param newHeight the new imageHeight of the image
     * @param imageType the type of {@code BufferedImage}
     * @param scaleType the method of scaling to use under {@code Image}
     * 
     * @return the scaled image
     */
    public static Image getResizedImage(Image originalImage, int newWidth, int newHeight, int imageType,
            int scaleType) {
        if (originalImage == null)
            return null;
        Image temp = originalImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
        BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, imageType);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(temp, 0, 0, newWidth, newHeight, null);
        g.dispose();
        return resizedImage;
    }
}

Related

  1. getResize(Image image, Dimension windowSize)
  2. getResizedImage(Image img, int iNewWidth, int iNewHeight)
  3. getResizedImage(Image original, final Integer w, final Integer h, boolean keepAspectRatio)
  4. getResizedImage(Image originalImage, Dimension newSize)
  5. getResizedImage(String path, int height, int width)
  6. resize(Image img, int newWidth, float quality)
  7. resize(String srcImageFile, String result, int newWidth, float quality)
  8. resizeImage(ImageIcon tmpIcon)