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.
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.
The first step is to import the Java classes we’ll need for our program to run. Here, we’ll use the following classes:
Here is the code to import all these classes:
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.
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.
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);
With our query parameters constructed, we can call the API.
InputStream inputStream = apiUrl.openStream();
OutputStream outputStream = new FileOutputStream("./screenshot.png");
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:
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!
Get started with ScreenshotAPI for free, or unlock 1,000 screenshots for just $9/month!