Java examples for 2D Graphics:BufferedImage Scale
create Thumbnail
//package com.java2s; import java.awt.Graphics2D; import java.awt.Image; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.imageio.ImageIO; public class Main { public static byte[] createThumbnail(byte[] source_bytes, int maximum_width, int maximum_height) throws IOException { byte[] out_bytes = new byte[0]; Image image = ImageIO.read(new ByteArrayInputStream(source_bytes)); if (image == null) return out_bytes; int width = image.getWidth(null); int height = image.getHeight(null); /* nothing to do, the image is smaller */ if (width < maximum_width && height < maximum_height) { System.out.println("Invalid dimensions"); return out_bytes; }/* www . j a v a2 s. c o m*/ double scale = (double) maximum_height / (double) height; if (width > height) { scale = (double) maximum_width / (double) width; } int scale_width = (int) (scale * width); int scale_height = (int) (scale * height); BufferedImage image_out = new BufferedImage(scale_width, scale_height, BufferedImage.TYPE_INT_RGB); AffineTransform at = new AffineTransform(); if (scale < 1.0d) { at.scale(scale, scale); } Graphics2D g2d = image_out.createGraphics(); image_out.flush(); g2d.drawImage(image, at, null); g2d.dispose(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image_out, "jpg", baos); return baos.toByteArray(); } }