Capture Screenshot in Java

I was trying to generate the thumbnail of an HTML page through Java. I could not find a great way, but in the process I found how to capture the screen in a Java program. It is pretty easy. The following program opens the IE in a full screen mode and then captures the screen. This image can be used later on to get the smaller sized image.  

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.io.File;


public class ScreenCapture {
  public static void main(String[] args) throws Exception {
  Runtime rt = Runtime.getRuntime();
  String[] cmd = new String[3];
  cmd[0]="C:/Program Files/Internet Explorer/iexplore.exe";
  cmd[1]="-k"; // Open the browser in full screen mode
  cmd[2]="java.sun.com";
  Process process = rt.exec(cmd);
  // Sleep for some time so that page can be loaded fully by browser
  Thread.currentThread().sleep(20*1000);
  Robot robot = new Robot();
  // Capture the screen shot
  BufferedImage bi=robot.createScreenCapture(
  new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
  ImageIO.write(bi, "jpg", new File("C:/imageTest.jpg"));
  // Kill the browser
  process.destroy();
  }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: