«

»

Apr
30

How to Take Screenshots in Java

Have you ever wanted to grab a screenshot from your Java application? Here’s a quick tutorial on how to grab a screenshot and save it to a JPEG and PNG file. This shows how to use the Robot class to capture the screen image and the ImageIO API to save it as a JPG and PNG.

First, we need to determine the size of the screen and create a rectangle with the screen dimensions. We can query the toolkit to determine the size of the screen. In our example, we are grabbing the entire screen. You can also use this same method to grab the contents of a window or a specific screen rectangle.

//Get the screen size
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
Rectangle rect = new Rectangle(0, 0, screenSize.width, screenSize.height);

Next, you will need to create an instance of the Robot and capture the desired rectangle of the screen. The createScreenCapture() method takes a Rectangle and returns a BufferedImage.

Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(rectangle);

Finally, we are going to save the screenshot as a PNG and JPG. For this, we will use the ImageIO API to convert from a BufferedImage to a PNG and to a JPG.

//Save the screenshot as a png
file = new File(“screen.png”);
ImageIO.write(image, “png”, file);

//Save the screenshot as a jpg
file = new File(“screen.jpg”);
ImageIO.write(image, “jpg”, file);

The complete screenshot example is below:

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

try
{
//Get the screen size
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
Rectangle rect = new Rectangle(0, 0,
screenSize.width,
screenSize.height);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(rect);
File file;

//Save the screenshot as a png
file = new File(“screen.png”);
ImageIO.write(image, “png”, file);

//Save the screenshot as a jpg
file = new File(“screen.jpg”);
ImageIO.write(image, “jpg”, file);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}

VN:F [1.9.13_1145]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)
How to Take Screenshots in Java, 10.0 out of 10 based on 1 rating

About the author

Faisal Basra

Faisal Basra is an independent consultant, software developer, writer, blogger, speaker, architect and technology leader in Lahore, Pakistan. He has been a professional software developer since 2008, has been writing code since 2006. Having hands on experience of popular Java EE frameworks & technologies like JSF, Spring, Hibernate, Enverse, JPA, Richfaces, Primefaces, JSP/Servlet. I have taken many initiatives while working with teams. Some of includes Automated Build & Release Management system via Hudson, Maven, Archiva & SVN. Blogging is my hobby and I also initiated blog at corporate level from setting up complete blog for company, content generation strategy and visibility over the Internet by Internet Marketing. Framworks & Technologies: JSF, Richfaces, Primefaces, Openfaces, Struts, Hibernate, Spring, ORMLite Tools & Servers: jUnit, Log4j, Maven, Eclipse, MyEclipse, NetBeans, Tomcat, Jboss, WebLogic Mobile Development: Google Android Marketing: Internet Marketing, Mobile App Marketing

Permanent link to this article: http://www.javaplex.com/blog/how-to-take-screenshots-in-java/

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>