Marketing

How to Take Screenshots with Java

Post by
Andrew Pierno
How to Take Screenshots with Java

We’ve already shown you how you can take programmatic screenshots with Python, Ruby, PHP, and JavaScript. Considering that Java still remains one of the most popular programming languages today, it’s time to look at how you can take screenshots with Java!

Taking Screenshots with Java

Before we look at how simple it is to take timed, programmatic, and HD screenshots with ScreenshotAPI, let’s first consider the more traditional methods of taking screenshots with Java. 

Firstly, you can use the createScreenCapture method from Java’s Robot class. 

With this class, you can take screenshots of the current open screen on a machine. You can also use it to take screenshots of multiple displays or of specific components on the screen. 

Another option would be to use the Selenium Webdriver to take screenshots and store them.

While both these methods can be effective and suitable for some use cases, they fall short in many instances. For instance, you’ll struggle to take bulk screenshots. 

Also, these tools are often quite limited in the types of screenshots you’ll be able to take. This is where a tool like ScreenshotAPI comes in. It lets you take tons of screenshots in almost any format and for any use case you need. 

Using ScreenshotAPI to Take Screenshots with Java

Considering the above, let’s look at how you would use ScreenshotAPI to take screenshots with Java. To do this, we’ll write a short Java program that you can use wherever you need to take screenshots.

Step 1: Import Packages

The first step is to import the Java classes we’ll need for our program to run. Here, we’ll use the following classes:

  • FileOutputStream. A file output stream is for writing data to a file. Because this class is specifically meant for writing streams of raw data like image files, we’ll use it to write the screenshot’s data to an image file.  
  • InputStream. This class is used to read data. It’s important to note that it’s not useful on its own, so the program will use its subclasses to read the data. 
  • OutputStream. Conversely to the InputStream class, the OutputStream class is used to write data. Like the InputStream class, it can’t be used independently, and our program will use its subclasses to write the data. 
  • URL. We’ll use the URL class to enable our program to access resources on the internet. So, we’ll use it to access the website we’d like to take a screenshot of. 
  • URLEncoder. Finally, we’ll use the URLencoder class to translate the information our program obtains from the internet into a form that can be understood. 

Here is the code to import all these classes:

package main;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLEncoder;

You’ll also notice that the code contains the package main statement. This is because, unlike the other programming languages we’ve dealt with, Java is a compiled language. As a result, the Java compiler will produce a program from the code we write.  In other languages, an interpreter runs the code from top to bottom. package main contains all the code that needs to be compiled. 

Step 2: Define the Variables

Later in our program, we’ll use query parameters to call the API. To do this, we’ll first need to define the variables that we’ll use.

String token = "Your API Key";
String url = URLEncoder.encode("https://google.com");
int width = 1920;
int height = 1080;
String output = "image";

With this code, we defined the variables that provide our API key, what website we’d like a screenshot of, set the size of the screenshot, and determined the output of the screenshot. 

Step 3: Construct the query parameters

Once we’ve defined our variables, we can construct the query parameters. Here, we’ll use this code:

String query = "https://shot.screenshotapi.net/screenshot";
query += String.format("?token=%s&url=%s&width=%d&height=%d&output=%s",
token, url, width, height, output);
URL apiUrl = new URL(query);

Step 4: Call the API

With our query parameters constructed, we can call the API. 

InputStream inputStream = apiUrl.openStream();
OutputStream outputStream = new FileOutputStream("./screenshot.png");
inputStream.transferTo(outputStream);

inputStream.close();
outputStream.close();

Step 5: Final steps

At this stage, our code is almost complete; there are only a few final steps we need to complete. 

Firstly, we’ll need to place the code that defines the variables, constructs the parameters, and calls the API in a try code block.

This means that when the code runs, the program will only run completely if it doesn’t encounter any errors. If it does, we’ll write the code of what should happen in the catch code block. 

Once done, all our code will look like this: 

java code for taking screenshots

Start Taking Screenshots with Java Now

With ScreenshotAPI, you can easily plug your API into your preferred programming language to take timed, bulk, and programmatic screenshots. No matter if you need screenshots to track competitors, keep tabs on your listings, or ensure website compliance, ScreenshotAPI has got your back!

screenshotapi reviews

Get started with ScreenshotAPI for free, or unlock 1,000 screenshots for just $9/month!