Wednesday, 24 August 2011

10 useful Eclipse Short cuts for easy Java programming

1) ctrl + 1
2) ctrl + space bar
3) ctrl + f6
4) ctrl + f7
5) ctrl + shift + R
6) ctrl + shift + T
7) Alt + up arrow
8) Alt + down arrow
9) Alt + left arrow
10) Alt + right arrow

Monday, 4 July 2011

What's Up in Java 1.7

Here's some code examples for the new features in Java 1.7
Support for Collections
Let's write less code when we create a List / Set / Map
List< String> list = ["item"];
String item = list[0];
Set< String > set = {"item"};
Map< String,Integer > map = {"key" : 1};
int value = map["key"];
Automatic Resource Mgt
Below code look's to be verbose for you.. right
BufferedReader br = new BufferedReader(new FileReader(path));
try {
   return br.readLine();
} finally {
   br.close();
}
But you love this below one!!!!
try (BufferedReader br = new BufferedReader(new FileReader(path)) {
   return br.readLine();
}
Improved Type Inference for Generic Instance Creation (diamond)
when you declare the interface of your object, You don't want to repeat when you instantiate the object.
Look's to be great na.....
Map<String,String> hello = new Map<>();
Underscores in numeric literals
int billion = 1_000_000_000;
I am not sure how many ppl it will be useful one?
String's in switch case
String availability = "available";
switch(availability) {
 case "available":
    //code
    break;

  case "unavailable":
    //code
    break;

  case "merged":
    //code

  default:
    //code
    break;
}
Note : Check for the Null in the String before you passing the String into Switch case.
Binary literals
You can create binary literals using the 0b prefix
int binary = 0b1001_1001;
Catching More Than One Type of Exception with One Exception Handler
try {
Here comes your code....
}
catch(IOException | NullPointerException | ..........) {
}

 It would be great if someone can point me on more things. I am sure there are plenty of other cool stuff.

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

}

Wednesday, 29 June 2011

Java EE7 - JSR 342 Approved

JCP has approved the following :-
  • JSR 342 – the next release of Java platform, Enterprise Edition.Java EE 7 has enhances cloud environments.
  • JSR 340 – Java Servlet 3.3
  • JSR 341 – EL 3.0
  • JSR 343 – Java Message Service 2.0
  • JSR 344 – JSF 2.2
  • JSR 338 – JPA API 2.1
  • JSR 339 – Java API for RESTful Webservices.


    Read More...