How to Detect and Un-Skew a Photo of a Document using Java

Cloudmersive
2 min readOct 4, 2022

While many of us like to think of ourselves as casually talented photographers, our photos often tell a different story. Many of the photos we take on our phone are subtly (or, at times, not so subtly) skewed at odd angles, which does more than just hurt the aesthetic: it makes subsequent file conversion and/or OCR operations more difficult to perform. Thankfully, our Detect & Unskew Photo API can be used directly before file conversion (such as converting JPG/PNG to PDF) and OCR services to ensure the highest quality output. This API detects the degree to which a photo is skewed and automatically corrects it, creating & supplying the encoding for a perfectly square version of the image in return. For those specifically looking to employ this API before an OCR operation, you’ll have the option to convert the image to Black & White grayscale by configuring an optional PostProcessingEffect parameter (values are “None” and “BlackAndWhite”).

Below, I’ll demonstrate how you can use this API for free with ready-to-run Java code examples from the Cloudmersive API Console. All you’ll need in addition to the below examples is a Cloudmersive API key, which you can get by registering a free account on our website (free accounts come with a limit of 800 API calls per month — perfect for smaller scale projects & startups).

Let’s get started by installing the API client. First add a reference to the repository in pom.xml:

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

Now let’s wrap up installation by adding an additional reference to the pom.xml dependency:

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

Last but not least, let’s call the below function, starting with adding our imports to the top of our file. As a reminder, the only mandatory inputs are your API key and your image file path; the PostProcessingEffect parameter is optional, set to “None” by default:

// 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.RecognizeApi;
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");
RecognizeApi apiInstance = new RecognizeApi();
File imageFile = new File("/path/to/inputfile"); // File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
String postProcessingEffect = "postProcessingEffect_example"; // String | Optional, post-processing effects to apply to the email, default is None. Possible values are None and BlackAndWhite (force the image into a black and white view to aid in OCR operations).
try {
byte[] result = apiInstance.recognizeDetectAndUnskewDocument(imageFile, postProcessingEffect);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling RecognizeApi#recognizeDetectAndUnskewDocument");
e.printStackTrace();
}

--

--

Cloudmersive

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