How to fix a skewed document photo in Java

Cloudmersive
2 min readJan 25, 2020

--

With the enormous popularity of smart phones, they are quickly replacing conventional scanners as a method for digitizing documents. Smart phone cameras are perfectly fine in and of themselves, but they invariably result in skewed photos when used for documents. Any good scanning or check depositing app needs to have a method for dealing with this problem. If you are reading this, I am guessing you are looking for a quick solution to this problem. You came to the right place; we will be done in about 5 minutes.

First of all, we must set up our library. We will do this using Jitpack so we need to add references to our Maven POM file.

Repository

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

Dependency

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v2.75</version>
</dependency>
</dependencies>

Now, we will invoke preprocessingUnskew, like so:

// 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.PreprocessingApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: ApikeyApiKeyAuth 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");PreprocessingApi apiInstance = new PreprocessingApi();File imageFile = new File("/path/to/file"); // File | Image file to perform OCR on.  Common file formats such as PNG, JPEG are supported.try {byte[] result = apiInstance.preprocessingUnskew(imageFile);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling PreprocessingApi#preprocessingUnskew");e.printStackTrace();}

And the API will do the rest for us. It’s really as simple as that. Your document will now be ready for conversion to PDF or Optical Character Recognition, both of which can be accomplished with our APIs.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet