Here you can find the source of rescaleImage(BufferedImage img, int targetWidth, int targetHeight, Object hint)
Parameter | Description |
---|---|
img | the original image to be scaled |
targetWidth | the desired width of the scaled instance, in pixels |
targetHeight | the desired height of the scaled instance, in pixels |
hint | one of the rendering hints that corresponds to java.awt.RenderingHints.KEY_INTERPOLATION (e.g. java.awt.RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR , java.awt.RenderingHints.VALUE_INTERPOLATION_BILINEAR , java.awt.RenderingHints.VALUE_INTERPOLATION_BICUBIC ) |
public static BufferedImage rescaleImage(BufferedImage img, int targetWidth, int targetHeight, Object hint)
//package com.java2s; /*/* w ww. ja va 2s . c om*/ * The Shepherd Project - A Mark-Recapture Framework * Copyright (C) 2011 Jason Holmberg * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; public class Main { /** * Convenience method that returns a scaled instance of the * provided {@code BufferedImage}. * Any transparency/alpha of the original image is not preserved. * * @param img the original image to be scaled * @param targetWidth the desired width of the scaled instance, in pixels * @param targetHeight the desired height of the scaled instance, in pixels * @param hint one of the rendering hints that corresponds to * {@link java.awt.RenderingHints.KEY_INTERPOLATION} (e.g. * {@link java.awt.RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR}, * {@link java.awt.RenderingHints.VALUE_INTERPOLATION_BILINEAR}, * {@link java.awt.RenderingHints.VALUE_INTERPOLATION_BICUBIC}) * @return a scaled version of the original {@code BufferedImage} */ public static BufferedImage rescaleImage(BufferedImage img, int targetWidth, int targetHeight, Object hint) { if (img == null) throw new NullPointerException("Invalid (null) image specified"); BufferedImage tmp = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = tmp.createGraphics(); if (hint != null) g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint); g2.drawImage(img, 0, 0, targetWidth, targetHeight, null); g2.dispose(); return tmp; } }