Friday, 1 July 2011

Thumbnail Generator using Java

What is Aspect Ratio ?
            The ratio of a picture's width-to-height is known as its aspect ratio.
Landscape Image ?
            An image with an aspect ratio greater than 1, is called a "Landscape" image.
Portrait Image ?
            An image with an aspect ratio lesser than 1, is called a "Portrait" image.
Square Image ?
            An image with an aspect ratio equal to 1, is called a "Square" image.

Retain the aspect ration is very important to keep the image with good resolution even it after resize has been done.

Let see how to retain the Aspect Ratio of an image when the large image make to smaller one(thumbnail image) or Smaller one to large image.

A) Landscape Image


The Original Width and Height of the above Landscape image is 1024 * 768.  Now let see how we can retain the aspect ratio of an image with the space provided to display the image to be 100 * 100 (Bounding Box).

Let's do some mathematical calculation to find the ratio of an image
thumb_image_ratio   = boundingbox_width / boundingbox_height  
                                 = 100 / 100
                                 = 1
original_image_ratio    = original_image_width / original_image_height
                                    = 1024 / 768
                                    = 1.33                                      

That's show the thumb_image_ratio < original_image_ratio. So we can fix the resized_image_width to be boundingbox_width(100).
Now we have calculate the resized_image_height ?

resized_image_height   = resized_image_width / original_image_ratio
                                     = 100/1.33 
                                     = 75.1879

Finally the outcome of the resized_image_width = 100 and resized_image_height = 75 and it will retain the same Aspect ratio.

B) Portrait Image


The Original Width and Height of the above Portrait image is 294 * 429.  Now let see how we can retain the aspect ratio of an image with the space provided to display the image to be 100 * 100 (Bounding Box).

Let's do some mathematical calculation to find the ratio of an image
thumb_image_ratio    = boundingbox_width / boundingbox_height
                                  = 100 / 100
                                  = 1


 original_image_ratio    = original_image_width / original_image_height 
                                     = 294 / 429
                                     = 0.6853

That's show the thumb_image_ratio > original_image_ratio. So we can fix the resized_image_height to be boundingbox_height(100). Now we have calculate the resized_image_width ?

resized_image_width   = resized_image_height / original_image_ratio
                                    = 100/0.6853
                                    = 68

Finally the outcome of the resized_image_width = 68 and resized_image_height = 100 and it will retain the same Aspect ratio.

/**
 * @author anand
 */
public class ImageTest {

   /**
    * @param args
    */
   public static void main(String[] args) {
      String path = "/home/devaraj/Image/portraits.jpg";
      File file = new File(path);// Specify the input image source file location.
      try {
         FileInputStream fis = new FileInputStream(file);
         InputStream bis = new BufferedInputStream(fis);
         FileOutputStream fos = null;
         Image image = (Image) ImageIO.read(bis);
         int thumbWidth = 100;// Specify image width in px
         int thumbHeight = 100;// Specify image height in px
         
         int imageWidth = image.getWidth(null);// get image Width
         int imageHeight = image.getHeight(null);// get image Height
         
         double thumbRatio = (double) thumbWidth / (double) thumbHeight;
         double imageRatio = (double) imageWidth / (double) imageHeight;

         // This calculation is used to convert the image size according to the pixels mentioned above
         if (thumbRatio < imageRatio) {
            thumbHeight = (int) (thumbWidth / imageRatio);
         } else {
            thumbWidth = (int) (thumbHeight * imageRatio);
         }

         BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);

         Graphics2D graphics = thumbImage.createGraphics();
         graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
         graphics.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

         ByteArrayOutputStream out = new ByteArrayOutputStream();
         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
         System.out.println("Encoder" + encoder);
         JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);

         int quality = 300;
         quality = Math.max(0, Math.min(quality, 500));
         param.setQuality(0.75f, false);

         // output image type.
         String format = "jpg";

         encoder.setJPEGEncodeParam(param);
         encoder.encode(thumbImage);
         ImageIO.write(thumbImage, format, new       File("/home/devaraj/Image/outputimage/portraits_aspect.jpg"));
      } catch (IOException ioExcep) {
         ioExcep.printStackTrace();
      } catch (Exception excep) {
         excep.printStackTrace();
      }
   }

}

5 comments:

  1. I have no words for this great post such a awe-some information i got gathered. Thanks to Author.
    Vee Eee Technologies

    ReplyDelete
  2. Hello sir I am getting thumbnail image block image.
    how can i rectify that.please help me

    ReplyDelete
  3. I have gone through a blog according to which you have following options -

    For simple RGB files use ImageScalr . ImageIO class is used for reading files and ImageScalr to create thumbnails
    For supporting RGB + CYMK, use ImageIO and JAI (Java Advanced Imaging) API for reading files and ImageScalr to create thumbnail.
    In case you don’t know what file formats, color mode you are going to deal with, safest option is to use ImageMagick.
    Here is the link http://paxcel.net/blog/java-thumbnail-generator-imagescalar-vs-imagemagic/

    ReplyDelete
  4. How can i create a thumbnail for html files..Is this works for the html files also?

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete