Showing posts with label Java. Java2D. Show all posts
Showing posts with label Java. Java2D. Show all posts

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

}