Java examples for 2D Graphics:BufferedImage Scale
rescale BufferedImage Y Maintain Aspect Ratio
//package com.java2s; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; public class Main { /**//www. ja va 2s . c om * @param im * @param height * @return the resized image */ public static BufferedImage rescaleYMaintainAspectRatio( BufferedImage im, int height) { if (im.getHeight() == height) { return im; } double iny = im.getHeight(); double dy = height / iny; return rescaleFractional(im, dy, dy); } /** * Rescale an image based on scale factors for width and height. * * @param in - original buffered image. * @param dx - multiplier for the width (x direction) * @param dy - multiplier for the height (y direction) * @return - scaled buffered image */ public static BufferedImage rescaleFractional(BufferedImage in, double dx, double dy) { int width = (int) (in.getWidth() * dx); int height = (int) (in.getHeight() * dy); BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = newImage.createGraphics(); AffineTransform at = AffineTransform.getScaleInstance(dx, dy); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2.drawRenderedImage(in, at); return newImage; } }