In this tutorial, we are going to show you how easy it is to capture a screenshot (either whole or partial) programmatically, and then saves the screenshot to an image file using Java API. The technique can be used to create screen video capture software.

 

1. About the Robot.createScreenCapture() and ImageIO.write() methods

The java.awt.Robot class provides a useful method for capturing a screenshot. Here’s the method signature:

BufferedImage createScreenCapture(Rectangle screenRect)

We pass a screen region (in rectangle) to be captured as this method’s parameter. The captured graphic is returned as a BufferedImageobject which can be drawn onto a GUI component or saved to a file. And we use the ImageIO.write() static method to save the BufferedImage object to a file:

boolean write(RenderedImage im, String formatName, File output)

where the formatName can be “jpg”, “png”, “bmp”, etc.

NOTE: The captured image does not include the mouse cursor.

 

2. Capture full screen Java example

To capture a screenshot of the whole screen, we need to know the screen size. The following statement obtains the screen size as a Rectangle object:

Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

And the following program captures a full screenshot and saves it to a JPG image file:

package net.codejava.graphics;

import java.awt.AWTException;
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 javax.imageio.ImageIO;

/**
 * This program demonstrates how to capture a screenshot (full screen)
 * as an image which will be saved into a file.
 * @author www.codejava.net
 *
 */
public class FullScreenCaptureExample {

	public static void main(String[] args) {
		try {
			Robot robot = new Robot(); 
			String format = "jpg";
			String fileName = "FullScreenshot." + format;
			
			Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
			BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
			ImageIO.write(screenFullImage, format, new File(fileName));
			
			System.out.println("A full screenshot saved!");
		} catch (AWTException | IOException ex) {
			System.err.println(ex);
		}
	}
}

The image file (FullScreenshot.jpg) is stored under the program’s current directory.

 

3. Capture a portion of the screen Java example

To capture screenshot of a portion of the screen, we need to specify a rectangle region to be captured. For example, the following statements create a capture region which is the first quarter of the screen (suppose that the screen is divided into four parts):

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle captureRect = new Rectangle(0, 0, screenSize.width / 2, screenSize.height / 2);

And following code is for a complete program:

package net.codejava.graphics;

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 javax.imageio.ImageIO;

/**
 * This program demonstrates how to capture screenshot of a portion of screen. 
 * @author www.codejava.net
 *
 */
public class PartialScreenCaptureExample {

	public static void main(String[] args) {
		try {
			Robot robot = new Robot(); 
			String format = "jpg";
			String fileName = "PartialScreenshot." + format;
			
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			Rectangle captureRect = new Rectangle(0, 0, screenSize.width / 2, screenSize.height / 2);
			BufferedImage screenFullImage = robot.createScreenCapture(captureRect);
			ImageIO.write(screenFullImage, format, new File(fileName));
			
			System.out.println("A partial screenshot saved!");
		} catch (AWTException | IOException ex) {
			System.err.println(ex);
		}
	}
}

The image file (PartialScreenshot.jpg) is stored under the program’s current directory.

 

4. Capture a GUI component Java example

Capturing screenshot of a GUI component (e.g. buttons, text fields, windows, etc) is even easier as we don’t have to use the Robot class. Each Swing component has a paint() method which can be used to paint a Graphics object. The following method will capture image of an arbitrary component and save it to an image file:

void captureComponent(Component component) {
	Rectangle rect = component.getBounds();

	try {
		String format = "png";
		String fileName = component.getName() + "." + format;
		BufferedImage captureImage =
				new BufferedImage(rect.width, rect.height,
									BufferedImage.TYPE_INT_ARGB);
		component.paint(captureImage.getGraphics());

		ImageIO.write(captureImage, format, new File(fileName));

		System.out.printf("The screenshot of %s was saved!", component.getName());
	} catch (IOException ex) {
		System.err.println(ex);
	}
}

Run the demo program (ComponentGUICaptureExample.java in the attachments section):

Component GUI capture demo

Click the Capture Me! button, its image will be saved as the button1.png file which looks like this:

button1

And click the Capture This Frame! button, image of the frame’s content will be saved as the frame0.png file which looks like this:

frame0

API References:

 

Other Java Graphics Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.

Attachments:
Download this file (ScreenCaptureExamples.zip)ScreenCaptureExamples.zip[Java source files]3 kB

Add comment

   


Comments 

#10Alex De Lara2020-01-03 09:36
This is great Nam Ha !!!
Works like a charm, but I found that "partial screen capture" should state the number of pixels to be captured. No big deal though the example may become confusing on a first pass.
I have a question though: does this somehow messes with the mouse positioning ? This question is because I am moving the mouse to a particular point on the screen, actually on a browser app and want to capture the tooltip with the screenshot but it fails to bring the tooltip.
Quote
#9ber9992019-01-26 12:24
Great idia.
Could you please write the code for a Midlet that could be started on a pre-Android softphone via a key combination?
Quote
#8Joel2018-10-04 19:27
Very good, Thank you! :)
Quote
#7kishore2018-08-09 20:05
Hi, Thanks for the code. I did try this approach on a remote machine executing the scripts thru jenkins, but the screenshots captured are blank with black screen. I am trying to use the scripts executing thru selenium webdriver. Any idea what could be the issue.
Quote
#6Akashic2017-12-23 09:46
Works perfectly. Thanks
Quote