Jan 29, 2013

Resize Images using Java

Posted Jan 29, 2013
When resizing images in Java, one would normally use Image.getScaledInstance() but it has been proven that this is not the most efficient way to scale images, in terms of quality, performance and resource usage.

An alternative would be ImageMagick which is a popular tool used for programmatically resizing images. However, it wasn't implemented in Java so you still need an interface for it a.k.a. JMagick. This could be a pain especially when you need to set-up additional dependencies just to resize images.

The best solution I found is to use imgescalr, a native Java library for resizing images. It uses hardware acceleration if available. The best thing about it is you don't have to set-up dependencies. It has been tested and used in production environments, so it should be reliable. Here's the link to their website and github.

Example:
BufferedImage img = ImageIO.read(new File("c:/img/test.jpg"));
   
BufferedImage scaledImg = Scalr.resize(img, Mode.AUTOMATIC, 640, 480);
   
File destFile = new File("c:/img/resized.jpg");
   
ImageIO.write(scaledImg, "jpg", destFile);
    
System.out.println("Done resizing");

2 comments:

  1. thank you very much.,u r the best
    thankx.

    ReplyDelete
  2. Renato Martin De La Rosa CastiMay 22, 2014 at 8:06 AM

    you have a example using rotation, please

    ReplyDelete