Java examples for 2D Graphics:BufferedImage Resize
Scales an image to the requested width and height, assuming these are both >= 1; size given in pixels.
/*// ww w . java2 s.co m * {{{ header & license * Copyright (c) 2007 Patrick Wright * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * }}} */ import java.awt.*; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Main{ private static final Map qual; /** * Scales an image to the requested width and height, assuming these are both >= 1; size given in pixels. * If either width or height is <=0, the current image width or height will be used. This method assumes * that, at the moment the method is called, the width and height of the image are available; it won't wait for * them. Therefore, the method should be called once the image has completely loaded and not before. * <p/> * Override this method in a subclass to optimize image scaling operations; note that the legacy * {@link java.awt.Image#getScaledInstance(int,int,int)} is considered to perform poorly compared to more * recent developed techniques. * <p/> * For a discussion of the options from a member of the Java2D team, see * http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html * * @param orgImage The image to scale * @return The scaled image instance. */ public static BufferedImage getScaledInstance(ScalingOptions opt, BufferedImage orgImage) { int w = orgImage.getWidth(null); int h = orgImage.getHeight(null); if (opt.sizeMatches(w, h)) return orgImage; w = (opt.getTargetWidth() <= 0 ? w : opt.getTargetWidth()); h = (opt.getTargetHeight() <= 0 ? h : opt.getTargetHeight()); Scaler scaler = (ImageUtil.Scaler) qual.get(opt .getDownscalingHint()); BufferedImage tmp = scaler.getScaledInstance(orgImage, opt); return tmp; } /** * Scales an image to the requested width and height, assuming these are both >= 1; size given in pixels. * If either width or height is <=0, the current image width or height will be used. This method assumes y * that, at the moment the method is called, the width and height of the image are available; it won't wait for * them. Therefore, the method should be called once the image has completely loaded and not before. * <p/> * Override this method in a subclass to optimize image scaling operations; note that the legacy * {@link java.awt.Image#getScaledInstance(int,int,int)} is considered to perform poorly compared to more * recent developed techniques. * <p/> * For a discussion of the options from a member of the Java2D team, see * http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html * * @param orgImage The image to scale * @param targetWidth The target width in pixels * @param targetHeight The target height in pixels * @return The scaled image instance. */ public static BufferedImage getScaledInstance(BufferedImage orgImage, int targetWidth, int targetHeight) { String downscaleQuality = Configuration.valueFor("xr.image.scale", DownscaleQuality.HIGH_QUALITY.asString()); DownscaleQuality quality = DownscaleQuality.forString( downscaleQuality, DownscaleQuality.HIGH_QUALITY); Object hint = Configuration.valueFromClassConstant( "xr.image.render-quality", RenderingHints.VALUE_INTERPOLATION_BICUBIC); ScalingOptions opt = new ScalingOptions(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB, quality, hint); return getScaledInstance(opt, orgImage); } }