Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday, August 8, 2013

Take Screen Shot in Java





The following code shows you how to take a screenshot using java.The heart of this code is the Robot class. This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.
To use this class we need to import java.awt.Robot. In eclipse shift+ctrl+o will do all this import stuffs for you.

Here is the method to capture screen shot

/*
 * This method takes screen shot
 * @param filename is the file name of the screenshot
 */
 public static void captureScreen(String fileName) throws Exception {
   
                   //get the dimention (i.e. width & height in pixels) of the screen
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                   //Create a rectangular shape of size of the screen
     Rectangle screenRectangle = new Rectangle(screenSize);
                   //create a new robot object
     Robot robot = new Robot();
                   //take a snapshot of the screen
     BufferedImage image = robot.createScreenCapture(screenRectangle);
                   //write the image to file
     ImageIO.write(image, "jpg", new File(fileName));
   
 }

createScreenCapture(Rectangle screenRect)creates an image containing pixels read from the screen & returns it as a BufferedImage. You can change the image format from jpg to any other format you like.


For more info on Robot class visit oracle docs here


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.. :)