Main page > Java > Code examples > Imaging

Screenshot.java—Take a screenshot and save it to a PNG file using javax.imageio

This program creates a screenshot and saves it as a PNG file. It uses Java's image I/O package introduced with version 1.4 to write the image file. That's why this program needs Java 1.4 or higher to run.

To use this program do the following:

Again, make sure you have Java 1.4 or higher installed to successfully run this program.

Explanation

Here's what this program does. First it checks the number of program arguments and aborts if it didn't get exactly two. The first argument must be a non-negative number for the seconds the program is supposed to wait before actually taking the screenshot.

The second one is the name of the PNG file into which the capture is to be saved. Note that the format has been hard-coded to be PNG. JPEG as output format is not a good idea in most cases. JPEG is for photos only, and screenshots typically contain text and areas of one color. However, you could change the output format to JPEG by replacing png with jpg as second argument to ImageIO.write.

After getting and checking the parameters, the program waits for the number of seconds that was specified. If the user picks a large number, he can arrange the desktop in any way he wants before the capture is created. Good capture programs can react on a particular key stroke, but this one's only a small demo, so the time delay must suffice.

Next, the program determines the resolution of the screen in pixels and copies that information to a Rectangle object. Then a Robot object is created and a screen shot is made using the above mentioned Rectangle object. Note that the class java.awt.Robot has been introduced with Java 1.3. So if you replace the file saving code with something else, the minimum requirement for this program to work is still Java version 1.3 or higher.

Now the image (in form of a BufferedImage object) is used as parameter to ImageIO.write, and a PNG file is written.

Source code of Screenshot.java

/*
 * 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.exit(1);
		}
		// check if file name is valid
		String outFileName = args[1];
		if (!outFileName.toLowerCase().endsWith(".png")) {
			System.err.println("Error: output file name must " +
				"end with \".png\".");
			System.exit(1);
		}
		// 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, "png", 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); 
	}
}