Is it possible to reduce the size of an image with Java? (I want to create thrumbnails on an image.)

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)
   }

}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact