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;

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
  Process process = rt.exec(cmd);
  // Sleep for some time so that page can be loaded fully by browser
  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


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: