get Scaled Image - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Scale

Description

get Scaled Image

Demo Code


//package com.java2s;

import java.awt.Graphics2D;

import java.awt.Image;
import java.awt.RenderingHints;

import java.awt.image.BufferedImage;

public class Main {
    public static Image getScaledImage(Image original, int scale) {

        int aspectRatio = original.getWidth(null)
                / original.getHeight(null);
        int targetWidth = scale * aspectRatio;
        int targetHeight = scale;

        BufferedImage newImage = new BufferedImage(targetWidth,
                targetHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics = newImage.createGraphics();
        try {/*from   w ww  .  j a  va  2s.  c o  m*/
            graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics.drawImage(original, 0, 0, targetWidth, targetHeight,
                    null);
        } finally {
            graphics.dispose();
        }

        return newImage;

    }
}

Related Tutorials