Smoothly scales the given BufferedImage to the given width and height using the Image#SCALE_SMOOTH algorithm (generally bicubic resampling or bilinear filtering). - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Scale

Description

Smoothly scales the given BufferedImage to the given width and height using the Image#SCALE_SMOOTH algorithm (generally bicubic resampling or bilinear filtering).

Demo Code

/*// w w w.j  av  a  2s .c  o  m
 * Copyright (C) 2011 The Android Open Source Project
 *
 * 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.
 */
//package com.book2s;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.image.BufferedImage;

public class Main {
    /**
     * Smoothly scales the given {@link BufferedImage} to the given width and height using the
     * {@link Image#SCALE_SMOOTH} algorithm (generally bicubic resampling or bilinear filtering).
     *
     * @param source The source image.
     * @param width  The destination width to scale to.
     * @param height The destination height to scale to.
     * @return A new, scaled image.
     */
    public static BufferedImage scaledImage(BufferedImage source,
            int width, int height) {
        Image scaledImage = source.getScaledInstance(width, height,
                Image.SCALE_SMOOTH);
        BufferedImage scaledBufImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        Graphics g = scaledBufImage.createGraphics();
        g.drawImage(scaledImage, 0, 0, null);
        g.dispose();
        return scaledBufImage;
    }
}

Related Tutorials