Java - Take screenshots of the full screen

By xngo on February 27, 2019

The Java program below will take screenshot of your full screen. Here is how to use it:

java PrintScreen <wait_in_milliseconds> <save_to_directory> <filename_prefix>

Full code

import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
 
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import javax.imageio.ImageIO;
 
public class PrintScreen
{
  public static void main(String[] args)
  {
    // Print help how to use this application.
    if (args.length != 3)
    {
      System.out.println("Usage:\n java PrintScreen <wait_in_milliseconds> <save_to_directory> <filename_prefix>\n");
      System.out.println("Examples:");
      System.out.println(" In MS Windows: java PrintScreen 1000 c:\\myPictureDirectory myScreenshotPrefix");
      System.out.println(" In Linux: java PrintScreen 1000 /myPictureDirectory myScreenshotPrefix");
      System.exit(0);
    }
 
    // Get arguments.
    long lWaitTime = Long.parseLong(args[0]);
    File oDir = new File(args[1]);
    String sPrefix = args[2];
 
    String sDirectory = "";
    try
    {
      sDirectory = oDir.getCanonicalPath();
    }
    catch (IOException ex)
    {
      System.out.println(ex.getMessage());
    }
 
    // Take screenshots of the full screen forever.
    PrintScreen oPrintScreen = new PrintScreen();
    while (true)
    {
      String sPathSeparator = "";
      if (!sDirectory.endsWith(File.separator))
      {
        sPathSeparator = File.separator;
      }
      final String sCurrentDateTime = oPrintScreen.getCurrentDataTime();
      final String sFilename = sDirectory + sPathSeparator + sPrefix + "_" + sCurrentDateTime + ".png";
      oPrintScreen.captureScreen(sFilename);
      try
      {
        // Waiting time between each screenshot.
        Thread.sleep(lWaitTime);
      }
      catch (Exception ex)
      {
        System.out.println(ex.getMessage());
      }
    }
 
  }
 
  public void captureScreen(final String sFilename)
  {
    try
    {
      // Determine current screen size
      Toolkit toolkit = Toolkit.getDefaultToolkit();
      Dimension oScreenSize = toolkit.getScreenSize();
      Rectangle oScreen = new Rectangle(oScreenSize);
 
      // Create screen shot
      Robot robot = new Robot();
      BufferedImage oImage = robot.createScreenCapture(oScreen);
 
      // Save captured image to PNG file
      ImageIO.write(oImage, "png", new File(sFilename));
 
      // Display info of image saved.
      String sMsg = String.format("Screenshot(%s x %s pixels) is saved to %s.", oImage.getWidth(), oImage.getHeight(), sFilename);
      System.out.println(sMsg);
    }
    catch (Exception ex)
    {
      System.out.println(ex.getMessage());
      System.exit(0); // Shutdown the application completely. Need this because of the infinite loop.
    }
  }
 
  public final String getCurrentDataTime()
  {
    Date oCurrentDate = new Date();
    SimpleDateFormat oSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss.SSSS");
    return oSimpleDateFormat.format(oCurrentDate);
  }
}

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.