Saturday 2 April 2016

Java: Capture screenshot programmatically

In this post, I am going to show you how to take a screen shot and save it to your computer using Java API. java.awt.Robot class provides createScreenCapture method to capture a screen shot.

Capture full screen
Following statements are used to capture full screen.
Robot robot = new Robot();
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
ImageIO.write(screenFullImage, format, new File(destination));

‘format’ can be “jpg”, “png”, “bmp”, etc.

Capture portion of the screen
Following statements are used to capture the partial screen.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rect = new Rectangle(0, 0, screenSize.width / 2, screenSize.height / 2);
Robot robot = new Robot();
BufferedImage screenFullImage = robot.createScreenCapture(rect);
ImageIO.write(screenFullImage, format, new File(destination));


Following is the complete working application.

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Objects;

import javax.imageio.ImageIO;

public class Test {
   /**
    * Capture the full screen and save it to the destination.
    * 
    * @param destination
    * @throws AWTException
    * @throws IOException
    */
   public static void captureFullScreen(final String destination,
         final String format) throws AWTException, IOException {
      if (Objects.isNull(destination) || Objects.isNull(format))
         return;
      Robot robot = new Robot();
      Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit()
            .getScreenSize());
      BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
      ImageIO.write(screenFullImage, format, new File(destination));
   }

   /**
    * Capture rect portion of the screen.
    * 
    * @param rect
    * @param destination
    * @param format
    * @throws AWTException
    * @throws IOException
    */
   public static void capturePortion(final Rectangle rect,
         final String destination, String format) throws AWTException,
         IOException {
      if (Objects.isNull(rect) || Objects.isNull(destination)
            || Objects.isNull(format))
         return;
      Robot robot = new Robot();
      BufferedImage screenFullImage = robot.createScreenCapture(rect);
      ImageIO.write(screenFullImage, format, new File(destination));
   }

   public static void main(String args[]) throws AWTException, IOException {
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Rectangle rect = new Rectangle(0, 0, screenSize.width / 2,
            screenSize.height / 2);

      captureFullScreen("fullscreen", "png");
      capturePortion(rect, "halfScreen", "jpg");
   }
}


No comments:

Post a Comment