Is it possible to reduce the size of an image with Java? (I want to create thrumbnails on an image.)
Created May 4, 2012
Daniel del Rio Yes. Use a BufferedImage to retrieve the image and an AffineTransform to scale it. Here is some example code:
public class Thumbnail extends ImageIcon { public Thumbnail(ImageIcon originalIcon) { double scale = 0.5; // 50 % int w = originalIcon.getWidth() * scale; int h = originalIcon.getHeight() * scale; BufferedImage outImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); AffineTransform trans = new AffineTransform(); trans.scale(scale, scale); Graphics2D g = outImage.createGraphics(); g.drawImage(originalIcon.getImage(), trans, null); g.dispose(); setImage(outImage) } }