How to Validate a PNG File with Java

Cloudmersive
2 min readJun 21, 2022

--

In another documentation article, we highlighted a Cloudmersive API which can validate dozens of unique image file types automatically. If you’re looking for image validation ONLY for PNG files, however, that API will exceed your application’s needs. Instead, you should take advantage of our PNG File Validation API, which (as the name hints) validates PNG files and identifies any errors found in those files exclusively. Below, we’ll walk through structuring your API call using complementary code examples provided in Java.

Let’s start by installing the Java SDK with Maven. First, include the following reference in the pom.xml repository:

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

After that, we can include the following reference in the pom.xml dependency:

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

Lastly, let’s add the import classes and call the API. Include your input file & provide your Cloudmersive API key (obtainable by registering a free account on our website) where indicated:

// 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.validateDocumentPngValidation(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ValidateDocumentApi#validateDocumentPngValidation");
e.printStackTrace();
}

If your file is found to be invalid, you’ll receive a Boolean determining “DocumentIsValid.” If false, you’ll get information on the error description, path, and URI.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet