List of usage examples for java.awt Graphics2D drawRenderedImage
public abstract void drawRenderedImage(RenderedImage img, AffineTransform xform);
From source file:se.vgregion.webbisar.svc.impl.ImageUtil.java
/** * Resizes and overwrites the image. Will keep the original pictures dimensions. * // ww w.j av a 2 s . c om * @param sourceFile * the file to rezise * @param newLongSideSize * the new size, in pixels. * @param quality * a number between 0 and 100 where 100 gives the best quality * @throws IOException */ public synchronized static void scaleImage(File sourceFile, ImageSize imageSize, float quality) throws IOException { // System.gc(); BufferedImage sourceImage = ImageIO.read(sourceFile); int srcWidth = sourceImage.getWidth(); int srcHeight = sourceImage.getHeight(); double longSideForSource = Math.max(srcWidth, srcHeight); double longSideForDest = srcWidth > srcHeight ? imageSize.getWidth() : imageSize.getHeight(); double multiplier = longSideForDest / longSideForSource; int destWidth = (int) (srcWidth * multiplier); int destHeight = (int) (srcHeight * multiplier); BufferedImage destImage = new BufferedImage((int) imageSize.getWidth(), (int) imageSize.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D graphics = destImage.createGraphics(); graphics.setPaint(Color.WHITE); graphics.fillRect(0, 0, destImage.getWidth(), destImage.getHeight()); AffineTransform affineTransform = AffineTransform.getScaleInstance(multiplier, multiplier); AffineTransform trans = new AffineTransform(); trans.setToTranslation((imageSize.getWidth() - destWidth) / 2, (imageSize.getHeight() - destHeight) / 2); graphics.transform(trans); graphics.drawRenderedImage(sourceImage, affineTransform); saveImageAsJPEG(sourceFile.getAbsolutePath(), destImage, quality); }