How to Validate an Image File using Java

Cloudmersive
2 min readJun 21, 2022

--

Images get uploaded to websites for a lot of reasons, all the time. That can include creating a profile picture, submitting an insurance claim, posting a review, and so much more. Those images tend to be super useful for any business, offering an element of UGC (User-Generated Content) that can measurably boost a website’s popularity and relatability among its audience. Those images are useless, however, if they’re invalid. The key is to ensure that invalid image files don’t make it past the client side and into a database without a strict validation service deep-verifying their contents. With the help of our Image File Validation API, you can ensure your website automatically checks the validity of new image files and reports a quick result regarding that validity. This API supports dozens of image file formats and can be incorporated into your application using the ready-to-run code snippets provided below to structure an API call.

Let’s begin by installing the Maven SDK. First, include a reference in the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next, let’s add a reference to the dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

One more step left to go. Include the below code block with your import classes up top, and ensure your parameters are ready to go. Those should include:

  1. Your input file.
  2. Your Cloudmersive API key (which you can get by registering a free account on our website).
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ValidateDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ValidateDocumentApi apiInstance = new ValidateDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
DocumentValidationResult result = apiInstance.validateDocumentImageValidation(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ValidateDocumentApi#validateDocumentImageValidation");
e.printStackTrace();
}

With that, you’re all done. Your API key will give you access to dozens of other Cloudmersive APIs, including a suite of additional Document Validation APIs similar to the above (for different file types/formats).

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.