Bulk Emailing

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 25 March 2011

Aspect Ratio came to rescue

Posted on 02:10 by Unknown
In my present assignment I had to deal with large JPEGs (2000px:3000px) images and wanted to store their scaled version (preferably of size 400px:500px).
If we randomly scale an image without considering its Aspect Ratio, the resized image looses clarity very quickly (depends upon how smaller you are trying to make it).

The solution is Aspect Ratio. So, you have the original width and height ratio (2000px:3000px) and need to find out scaled width and height. We need to keep either width or height fix. In my case, I kept the scale width fixed to "400px". Now, if we apply a simple mathematic equation-

width_original : height_original = width_scale : height_scale
height_scale = (width_scale * height_original) / width_original

So, height_scale will be 600px.

Above calculation will determine the scale width and height which will make sure your scaled image is keeping original Aspect Ratio intact and thus you'll get the best quality resized image!

I am using Java ImageIO package to handle Image resizing part. Following is the code snippet (file is the source of the original image)-
final int SCALE_WIDTH = 400;
// Read the original Image
BufferedImage srcImage = ImageIO.read(file.getInputStream());


// Get original Width and Height
int originalWidth = srcImage.getWidth();
int originalHeight = srcImage.getHeight();
// Scale width and height
int newWidth = originalWidth;
int newHeight = 0;

if(newWidth GREATER_THAN SCALE_WIDTH)
    newWidth = SCALE_WIDTH;
//Find out scaled Height
newHeight = (newWidth * originalHeight)/originalWidth;
// Save resized image
BufferedImage dest = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);  
Graphics2D g = dest.createGraphics(); 
AffineTransform at = AffineTransform.getScaleInstance(
(double) newWidth / srcImage.getWidth(),
(double) newHeight / srcImage.getHeight());  
g.drawRenderedImage(srcImage, at); 
ImageIO.write(dest, format, file); 


That's it. Hope it helps.         
Email ThisBlogThis!Share to XShare to Facebook
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Weekend at Yeouido (Han River Cruise)
    Yeouido is one of the Han River Cruise boarding points. It has a wonderful river side park (Yeouido P...
  • CityWeather
    Update: Release 1.1 has been uploaded. It will now provide weekly forecast of your selected cities. Download   CityWeather is an Android...
  • Few Multi-Threading Implementations
    Sequential Producer & Consumer In my first example, I will demonstrate an implementation to solve ...
  • Accessing Yahoo Finance API
    Since last few days I was wondering the right set of Web Service to read Country wise Stock Exchange index information . I found a bunch of ...
  • Regular Expression in JavaScript
    I was struggling to get familiar with JavaScript's Regular expression handling and thought my learning might help others. Here I'll...
  • Alphanumeric Random Number
    Sometime basic things becomes tricky :-) Was trying to find out ways to generate some alphanumeric random number whose size (i.e. number of ...
  • Binary Search Tree in Java
    This post is a part of my continuous effort to just implement few of the important data structures in ...
  • QuickSort in Java
    This post is a part of my continuous effort to just implement few of the important data structures in Java. Please find my previous post on ...
  • InDroid- Access to Android's Hidden API
    I have started one project " InDroid " to expose and share Hidden and Internal methods and variables of Android platform which are...
  • Image Geo Tagging for Android
    " onMap " is an Image Geo Tagging application, which can read EXIF header of images stored in your Android device and Map them ov...

Blog Archive

  • ►  2013 (6)
    • ►  September (2)
    • ►  May (1)
    • ►  April (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2012 (4)
    • ►  July (2)
    • ►  March (1)
    • ►  January (1)
  • ▼  2011 (11)
    • ►  November (1)
    • ►  October (2)
    • ►  August (1)
    • ►  June (1)
    • ►  April (2)
    • ▼  March (3)
      • Aspect Ratio came to rescue
      • Regular Expression in JavaScript
      • JSON- Parsing Apache Lib
    • ►  January (1)
  • ►  2010 (27)
    • ►  December (2)
    • ►  November (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (4)
    • ►  June (7)
    • ►  May (5)
Powered by Blogger.

About Me

Unknown
View my complete profile