Sunday, May 4, 2014
To day in this article I will tell you how to resize or scale an image in Java. Many times while developing an application you maya need this. Most often you may require to create a thumbnail of an image in most web applications. This tutorial will guide you describing each steps in detail.

  • First read an image from file or URL using ImageIO
  • Now create a BufferedImage with specified width, height and image type
  • Obtain a Graphics2D object from the destination object create in earlier step
  • Draw the original image using the Graphics object
  • Dispose the Graphics object
  • Return or save the final image
Here in our example we will have a class with 3 methods.
Original image (930 x 520)  to be resized or scaled

  • resizeImage() : This method takes in a source image, the width and height of destination image. This is most simple method and follows the steps mentioned above
    Image resized (100 x 100) without hints
  • BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = dest.createGraphics();
    g2d.drawImage(src, 0, 0, width, height, null);
    g2d.dispose();
    return dest;
  • resizeImageWithHints() : This is a slightly modified version of the above method. Here rendering hints and alpha composite are added to the destination image as shown below
    Image resized (100 x 100) with hints
  • g2d.setComposite(AlphaComposite.Src);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  • scaleImage() : This method takes input a source image and the scale factor varying from 0 to 1. The most important feature of this method is that it reserves i.e. maintains the aspect ratio of original image. Here an AffineTransform instance is created and the image is drwan with that instance
    Image scaled = 0.3 (279 x 156)
  • AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
    g2d.drawRenderedImage(src, at);
-------------------------------------------------------------------------------------------------------------------------
Java Source Code
-------------------------------------------------------------------------------------------------------------------------
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class ImageResizer {
 
 /**
  * Resizes an image without rendering hints depending on input width, height
  * @param src source image to be resized
  * @param width width of destination image
  * @param height height of destination image
  * @return resized image
  */
 public BufferedImage resizeImage(BufferedImage src,int width,int height){
  BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  Graphics2D g2d = dest.createGraphics();
  g2d.drawImage(src, 0, 0, width, height, null);
  g2d.dispose();
  
  return dest;
 }
 
 /**
  * Resizes an image with rendering hints depending on input width, height
  * @param src source image to be resized
  * @param width width of destination image
  * @param height height of destination image
  * @return resized image
  */
 public BufferedImage resizeImageWithHints(BufferedImage src,int width,int height){
  BufferedImage dest = new BufferedImage(width, height, src.getType());
  Graphics2D g2d = dest.createGraphics();
  
  g2d.setComposite(AlphaComposite.Src);
  g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  
  g2d.drawImage(src, 0, 0, width, height, null);
  g2d.dispose();
  
  return dest;
 }
 
 /**
  * Scales an image depending on sclae factor and maintains
  * the aspect ratio of original image
  * @param src source image to be scaled
  * @param scale scale factor xscale = yscale
  * @return scaled image
  */
 public BufferedImage scaleImage(BufferedImage src, double scale){
  int w = (int)(src.getWidth()*scale);
  int h = (int)(src.getHeight()*scale);
  BufferedImage dest = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  Graphics2D g2d = dest.createGraphics();
  AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
  g2d.drawRenderedImage(src, at);
  
  return dest;
 }
 
 public static void main(String... args) throws Exception {
  ImageResizer resizer = new ImageResizer();
  BufferedImage src = ImageIO.read(new File("original.jpg"));

  BufferedImage dest = resizer.resizeImage(src, 100, 100);
  ImageIO.write(dest, "jpeg", new File("dest-resize.jpg"));
  
  dest = resizer.resizeImageWithHints(src, 100, 100);
  ImageIO.write(dest, "jpeg", new File("dest-resize-hints.jpg"));
  
  dest = resizer.scaleImage(src, 0.3);
  ImageIO.write(dest, "jpeg", new File("dest-scale.jpg"));
 }
}
-------------------------------------------------------------------------------------------------------------------------
Download Links
-------------------------------------------------------------------------------------------------------------------------
DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared

0 comments:

Post a Comment

Total Pageviews

Followers


Labels

Popular Posts

free counters