tag:blogger.com,1999:blog-84581833281613706672024-02-20T18:35:15.128-08:00Javaget2javahttp://www.blogger.com/profile/11598550318785017000noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-8458183328161370667.post-30998432368846073462011-08-24T22:29:00.000-07:002011-08-24T22:29:20.945-07:0010 useful Eclipse Short cuts for easy Java programming1) ctrl + 1 <br />
2) ctrl + space bar<br />
3) ctrl + f6 <br />
4) ctrl + f7<br />
5) ctrl + shift + R<br />
6) ctrl + shift + T<br />
7) Alt + up arrow<br />
8) Alt + down arrow<br />
9) Alt + left arrow<br />
10) Alt + right arrowget2javahttp://www.blogger.com/profile/11598550318785017000noreply@blogger.com1tag:blogger.com,1999:blog-8458183328161370667.post-60542519056750637202011-07-04T02:31:00.000-07:002011-07-05T04:32:04.114-07:00What's Up in Java 1.7<div dir="ltr" style="text-align: left;" trbidi="on"><div dir="ltr" style="text-align: left;" trbidi="on">Here's some code examples for the new features in Java 1.7<br />
<b><i>Support for Collections</i></b><br />
Let's write less code when we create a List / Set / Map<br />
<pre class="java" name="code">List< String> list = ["item"];
String item = list[0];
Set< String > set = {"item"};
Map< String,Integer > map = {"key" : 1};
int value = map["key"];
</pre><i><b>Automatic Resource Mgt</b></i><br />
Below code look's to be verbose for you.. right<br />
<pre class="java" name="code">BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
</pre>But you love this below one!!!!<br />
<pre class="java" name="code">try (BufferedReader br = new BufferedReader(new FileReader(path)) {
return br.readLine();
}
</pre><b><i>Improved Type Inference for Generic Instance Creation (diamond)</i></b><br />
when you declare the interface of your object, You don't want to repeat when you instantiate the object.<br />
Look's to be great na.....<br />
<pre class="java" name="code">Map<String,String> hello = new Map<>();
</pre><b><i>Underscores in numeric literals</i></b><br />
int billion = 1_000_000_000; <br />
I am not sure how many ppl it will be useful one?<br />
<b><i>String's in switch case</i></b><br />
<pre class="java" name="code">String availability = "available";
switch(availability) {
case "available":
//code
break;
case "unavailable":
//code
break;
case "merged":
//code
default:
//code
break;
}
</pre>Note : Check for the Null in the String before you passing the String into Switch case.<br />
<b><i>Binary literals</i></b><br />
You can create binary literals using the 0b prefix<br />
int binary = 0b1001_1001;<br />
<b><i>Catching More Than One Type of Exception with One Exception Handler</i></b><br />
<pre class="java" name="code">try {
Here comes your code....
}
catch(IOException | NullPointerException | ..........) {
}
</pre><br />
It would be great if someone can point me on more things. I am sure there are plenty of other cool stuff. </div><br />
</div>get2javahttp://www.blogger.com/profile/11598550318785017000noreply@blogger.com5tag:blogger.com,1999:blog-8458183328161370667.post-10625106270573851312011-07-01T05:35:00.000-07:002011-07-03T22:33:30.690-07:00Thumbnail Generator using Java<div dir="ltr" style="text-align: left;" trbidi="on"><div dir="ltr" style="text-align: left;" trbidi="on">What is Aspect Ratio ?<br />
<div dir="ltr" style="text-align: left;" trbidi="on"> The ratio of a picture's width-to-height is known as its aspect ratio.<br />
Landscape Image ?<br />
An image with an aspect ratio greater than 1, is called a "<b>Landscape</b>" image.<br />
Portrait Image ?<br />
An image with an aspect ratio lesser than 1, is called a "<b>Portrait</b>" image.<br />
Square Image ?<br />
An image with an aspect ratio equal to 1, is called a "S<b>quare</b>" image.<br />
<br />
Retain the aspect ration is very important to keep the image with good resolution even it after resize has been done.<br />
<br />
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.<br />
<br />
<b><span class="Apple-style-span" style="color: #741b47;">A) Landscape Image</span></b><br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-vXs11nG2jEw/Tg2oSYMAreI/AAAAAAAAABo/eURhXZsZwZo/s1600/colorful_landscapes_screensaver-54675-1.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="240" src="http://4.bp.blogspot.com/-vXs11nG2jEw/Tg2oSYMAreI/AAAAAAAAABo/eURhXZsZwZo/s320/colorful_landscapes_screensaver-54675-1.jpeg" width="320" /></a></div><br />
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).<br />
<br />
Let's do some mathematical calculation to find the ratio of an image<br />
<span class="Apple-style-span" style="color: #3d85c6;"><b>thumb_image_ratio = boundingbox_width / boundingbox_height</b></span><span class="Apple-style-span" style="color: #3d85c6;"><b> </b></span><br />
<span class="Apple-style-span" style="color: #3d85c6;"><b> = 100 / 100</b></span><br />
<span class="Apple-style-span" style="color: #3d85c6;"><b> = 1</b></span><br />
<div><b><span class="Apple-style-span" style="color: #3d85c6;">original_image_ratio = original_image_width / original_image_height</span></b></div><div><b><span class="Apple-style-span" style="color: #3d85c6;"> = 1024 / 768</span></b></div><div><b><span class="Apple-style-span" style="color: #3d85c6;"> = 1.33</span></b><b><span class="Apple-style-span" style="color: #3d85c6;"> </span></b><br />
<br />
That's show the thumb_image_ratio < original_image_ratio. So we can fix the resized_image_width to be boundingbox_width(100).<br />
Now we have calculate the resized_image_height ?<br />
<br />
<span class="Apple-style-span" style="color: #3d85c6;"><b>resized_image_height = resized_image_width / original_image_ratio</b></span><br />
<span class="Apple-style-span" style="color: #3d85c6;"><b> = 100/1.33 </b></span><br />
<span class="Apple-style-span" style="color: #3d85c6;"><b> = 75.1879</b></span><br />
<br />
Finally the outcome of the resized_image_width = 100 and resized_image_height = 75 and it will retain the same Aspect ratio.<br />
<br />
<b><span class="Apple-style-span" style="color: #741b47;">B) Portrait Image</span></b><br />
<div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-Byurr_2pLAc/Tg2sYY-cAFI/AAAAAAAAABs/fXDCzRTPLPk/s1600/photography_tutorial_portraits_image-2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://4.bp.blogspot.com/-Byurr_2pLAc/Tg2sYY-cAFI/AAAAAAAAABs/fXDCzRTPLPk/s320/photography_tutorial_portraits_image-2.jpg" width="219" /></a></div><br />
<br />
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">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).</div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><br />
</div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">Let's do some mathematical calculation to find the ratio of an image</div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><b><span class="Apple-style-span" style="color: #3d85c6;">thumb_image_ratio = boundingbox_width / boundingbox_height<br />
= 100 / 100<br />
= 1</span></b><br />
<b><span class="Apple-style-span" style="color: #3d85c6;"><span class="Apple-style-span" style="color: black; font-weight: normal;"></span></span></b><br />
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><b><span class="Apple-style-span" style="color: #3d85c6;"> <b><span class="Apple-style-span" style="color: #3d85c6;">original_image_ratio = original_image_width / original_image_height </span></b></span></b></div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><b><span class="Apple-style-span" style="color: #3d85c6;"><b><span class="Apple-style-span" style="color: #3d85c6;"> = 294 / 429</span></b></span></b></div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><b><span class="Apple-style-span" style="color: #3d85c6;"><b><span class="Apple-style-span" style="color: #3d85c6;"> = 0.6853</span></b></span></b></div></div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><br />
</div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">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 ?</div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><br />
<b><span class="Apple-style-span" style="color: #3d85c6;">resized_image_width = resized_image_height / original_image_ratio</span></b><br />
<b><span class="Apple-style-span" style="color: #3d85c6;"> = 100/0.6853</span></b><br />
<b><span class="Apple-style-span" style="color: #3d85c6;"> = 68</span></b></div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><br />
</div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">Finally the outcome of the resized_image_width = 68 and resized_image_height = 100 and it will retain the same Aspect ratio.</div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><br />
</div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><pre name="code" class="java">/**
* @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();
}
}
}
</pre>
</div></div></div></div></div>get2javahttp://www.blogger.com/profile/11598550318785017000noreply@blogger.com4Coimbatore, Tamil Nadu, India11.0183333 76.97472219999997410.9172508 76.88420469999997 11.1194158 77.065239699999978tag:blogger.com,1999:blog-8458183328161370667.post-3107438136804911042011-06-29T10:45:00.000-07:002011-06-29T11:21:45.325-07:00Java EE7 - JSR 342 Approved<div dir="ltr" style="text-align: left;" trbidi="on"><div style="border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 13px; margin-bottom: 14px; margin-left: 0px; margin-right: 0px; margin-top: 14px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><b><span style="border-width: 0px; font-size: 13px; margin: 0px; outline-width: 0px; padding: 0px; text-decoration: underline;">JCP has approved the following :-</span></b></div><ul style="text-align: left;"><li><b><span style="border-width: 0px; color: green; font-size: 13px; margin: 0px; outline-width: 0px; padding: 0px;">JSR 342</span></b><span class="Apple-converted-space"> </span>– the next release of Java platform, Enterprise Edition.Java EE 7 has enhances cloud environments.</li>
<li><b><span style="border-width: 0px; color: green; font-size: 13px; margin: 0px; outline-width: 0px; padding: 0px;">JSR 340</span></b><span class="Apple-converted-space"> </span>– Java Servlet 3.3</li>
<li><b><span style="border-width: 0px; color: green; font-size: 13px; margin: 0px; outline-width: 0px; padding: 0px;">JSR 341</span></b><span class="Apple-converted-space"> </span>– EL 3.0</li>
<li><b><span style="border-width: 0px; color: green; font-size: 13px; margin: 0px; outline-width: 0px; padding: 0px;">JSR 343</span></b><span class="Apple-converted-space"> </span>– Java Message Service 2.0</li>
<li><b><span style="border-width: 0px; color: green; font-size: 13px; margin: 0px; outline-width: 0px; padding: 0px;">JSR 344</span></b><span class="Apple-converted-space"> </span>– JSF 2.2</li>
<li><b><span style="border-width: 0px; color: green; font-size: 13px; margin: 0px; outline-width: 0px; padding: 0px;">JSR 338</span></b><span class="Apple-converted-space"> </span>– JPA API 2.1</li>
<li><b><span style="border-width: 0px; color: green; font-size: 13px; margin: 0px; outline-width: 0px; padding: 0px;">JSR 339</span></b><span class="Apple-converted-space"> </span>– Java API for RESTful Webservices.</li>
</ul><ul style="border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 13px; list-style-image: initial; list-style-position: initial; list-style-type: circle; margin-bottom: 0px; margin-left: 25px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></ul><a href="http://www.blogger.com/goog_1147231323"><br />
</a><br />
<a href="http://jcp.org/en/jsr/summary?id=342" style="float: right;" target="_blank">Read More...</a> </div>get2javahttp://www.blogger.com/profile/11598550318785017000noreply@blogger.com2