Monday, August 5, 2013

Resize image in Java


Java code for re-sizing all images in a folder


If you use digital camera then you may have noticed that the image format is jpeg(a highly compressed image format)
but still image size is very large.This is because the resolution of image is very high.
We generally do not need such high resolution images.Moreover if we want to store images
in cloud drive (like Google Drive,Dropbox,SkyDrive etc). So here is a java code that will resize
all images in the source directory and save them in the destination directory. User can specify the
ratio of resizing.

Resize.java

/*
 * @author: Arabinda Moni
 * This code resizes all images in a directory with a given ratio
 * You are free to use this code & edit this code.
 * */

package image.resize;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

public class Resize {
 
 private String source; //path of the source directory
 private String dest;   // path of the destination directory
 private ArrayList inclusion;  //this arraylist contains image foramats to be processed 
 private int count=0;
 
 public Resize(String source,String dest,ArrayList inclusion){
  this.source=source;
  this.dest=dest;
  this.inclusion=inclusion;    
 }
 
 
 /*
  * @param ratio is resize ratio
  * For example: if you want to reduce the image to half of its resolution then use ratio=0.5
  * */
 public void resizeAll(double ratio) throws IOException{
  long startTime=System.currentTimeMillis();
  scanDir(new File(source), ratio);
  System.out.println((System.currentTimeMillis()-startTime)+ "  "+count);
 }
 
 /*
  * This method resizes image f 
  * @param f is file to be resized
  * @param ratio is ratio of resizing
  * */
 public void resizeImage(File f,double ratio) throws IOException{
  count++;
  // use BufferedImage to read the image file
  BufferedImage img=ImageIO.read(f);
  //calculation of height & width of the new image
  int width=(int)(img.getWidth()*ratio);
  int height=(int)(img.getHeight()*ratio);
  //Scaling is done here with
  Image newimg=img.getScaledInstance(width, height, Image.SCALE_FAST); 
  BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
  
  // here the new image is created
  Graphics2D g2 = bi.createGraphics();
  g2.drawImage(newimg, 0, 0, null);
  g2.dispose();
  
  //write image to file
  ImageIO.write(bi, "jpg",new File(dest+"\\"+f.getName()));
    }
 
 /*
  * This method scans the source directory & sub-directories 
  * for images of the included format
  * */
 public void scanDir(File f,double ratio) throws IOException{
  System.out.println(f.getName());
  if(f.listFiles()!=null){
   for(File files : f.listFiles()){
    scanDir(files,ratio);
   }
  } 
  else if(inclusion.contains(f.getName().substring(f.getName().lastIndexOf('.')+1))){
   resizeImage(f,ratio);
  }
 }
    
}


Here goes the driver program
Run.java

package image.resize;

import java.io.IOException;
import java.util.ArrayList;

public class Run {
   public static void main(String args[]) throws IOException{
    ArrayList inc=new ArrayList<>();
    inc.add("jpg");
    inc.add("JPG");
    inc.add("jpeg");
    inc.add("JPEG");
    inc.add("png");    
    Resize res=new Resize(YourSourceDirectory, YourDestinationDirectory, inc);
    res.resizeAll(0.5);    
   }
}

:) Happy Coding.. :)

2 comments :