JAVATM MASTER

Screen Shot in Java

Back to Table of Contents

Last modification by Marco Schmidt 2004-02-03
© 2003, 2004
Marco Schmidt

Modified by Eric R. Dunstan to include support for jpg 2004-10-20

This simple command line application requires javaTM 1.4 or higher. The application takes two parameters: an integer of indicating the number of seconds to wait before the screen capture is executed, followed by the name of the image file to save the screen capture to. The image file name must end with an .jpg, .jpeg, or .png extension.

First you copy the program below into a new file called Screenshot.java. Next you must compile the program like this:

javac Screenshot.java

Next you run the program like this:

java Screenshot 3 myscreen.jpg

That will save to myscreen.jpg in the local directory whatever is displayed on the screen of the computer 3 seconds after the java command is executed.

To use in an applet, you would need to sign the applet and then it would only work where users trusted the signature. Another possible use for this functionality could be in writing a PC Anywhere type program in java where the user can access a remote computer visually. Such a program would use java.awt.Robot extensively, invoking the methods: keypress, keyrelease, mouseMove, mousePress, and so on across a network connection to the remote machine.

/* Start of program
 * Screenshot.java (requires Java 1.4+)
 */

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class Screenshot {
	public static void main(String[] args) throws Exception {
		// make sure we have exactly two arguments, 
		// a waiting period and a file name
		if (args.length != 2) {
			System.err.println("Usage: java Screenshot " +
				"WAITSECONDS OUTFILE.png");
			System.err.println("Or: java Screenshot " +
				"WAITSECONDS OUTFILE.jpg");
			System.exit(1);
		}
		// check if file name is valid
		String outFileName = args[1];
		if (! (outFileName.toLowerCase().endsWith(".png")
			|| outFileName.toLowerCase().endsWith(".jpg")
			|| outFileName.toLowerCase().endsWith(".jpeg"))) {
			System.err.println("Error: output file name must " +
				"end with \".png\" or \".jpg\" or \".jpeg\".");
			System.exit(1);
		}
		String ImageType = 
			(outFileName.toLowerCase().endsWith(".png") ?
			"png" : "jpg" ); 
		// wait for a user-specified time
		try {
			long time = Long.parseLong(args[0]) * 1000L;
			System.out.println("Waiting " + (time / 1000L) + 
				" second(s)...");
			Thread.sleep(time);
		} catch(NumberFormatException nfe) {
			System.err.println(args[0] + " does not seem to be a " +
				"valid number of seconds.");
			System.exit(1);
		}
		// determine current screen size
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension screenSize = toolkit.getScreenSize();
		Rectangle screenRect = new Rectangle(screenSize);
		// create screen shot
		Robot robot = new Robot();
		BufferedImage image = robot.createScreenCapture(screenRect);
		// save captured image to PNG file
		ImageIO.write(image, ImageType, new File(outFileName));
		// give feedback
		System.out.println("Saved screen shot (" + image.getWidth() +
			" x " + image.getHeight() + " pixels) to file \"" +
			outFileName + "\".");
		// use System.exit if the program hangs after writing the file;
		// that's an old bug which got fixed only recently
		// System.exit(0); 
	}
}
//End of Program

*OracleTM and JavaTM are registered trademarks of Oracle and or its affiliates. Other names may be trademarks of their respective owners.*