resize BufferedImage - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Resize

Description

resize BufferedImage

Demo Code

/**/*from   w  ww. j  a  v  a2  s  .  c o  m*/
 * maps4cim - a real world map generator for CiM 2
 * Copyright 2013 - 2014 Sebastian Straub
 *
 * 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.java2s;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage resize(BufferedImage img, int width,
            int height) {
        BufferedImage resized = new BufferedImage(width, height,
                img.getType());
        Graphics2D g = resized.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.drawImage(img, 0, 0, width, height, 0, 0, img.getWidth(),
                img.getHeight(), null);
        g.dispose();
        return resized;
    }

    public static BufferedImage resize(BufferedImage img, double scale) {
        int width = (int) Math.round(img.getWidth() * scale);
        int height = (int) Math.round(img.getHeight() * scale);
        return resize(img, width, height);
    }

    public static BufferedImage resize(BufferedImage img, int edgeLength) {
        double scale = edgeLength
                / ((double) (Math.max(img.getWidth(), img.getHeight())));
        return resize(img, scale);
    }
}

Related Tutorials