Monday, May 6, 2013

Convert large Images into thumbnails and maintain aspect ratio.

Description : 

Image scalar libraries are use to perform the action with images like to create images thumbnails and many more. there are different vendors provide different type of libraries to perform the action one is Google java API here  and other is scalar API here  for image scaling libraries and in this post define these libraries are easy to use and there is no need to implement large set of coding or mathematical computations . but if you want to customize something else or perform some action that are not present in this libraries so implement our own libraries . following describe "how to use and create image into thumbnails with image scalar libraries" .. 

Requirements:

  • Download and install JDK
  • Download the imgscalr-lib-4.2.jar or inject dependency
<dependencies>
 ...
 <dependency>
  <groupId>org.imgscalr</groupId>
  <artifactId>imgscalr-lib</artifactId>
  <version>4.2</version>
  <type>jar</type
  <scope>compile</scope>
 </dependency>
 ...
<dependencies>

Following is the example how to use : 

import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;

import org.imgscalr.Scalr;
import org.imgscalr.Scalr.Method;

class ImageThumbnail{
     private void createThumbnail() throws FileNotFoundException,IOException{
          File file = new File("1.jpg");
          BufferedImage img = ImageIO.read(file);
          BufferedImage thumb = Scalr.resize(img, Scalr.Method.SPEED,102, 102, Scalr.OP_ANTIALIAS);
          ImageIO.write(thumb, "png", new File("thumb.png"));
     }

     public static void main(String[] args) throws FileNotFoundException,IOException{
          new ImageThumbnail().createThumbnail();
     }
}

Download the Source Code From this link