OCR Preprocessing API: How to Convert an Image of Text into a Binarized (light and dark) View

Images of documents are rarely taken in perfect circumstances. With that in mind, you’ll typically get the best results from an OCR (optical character recognition) operation by preprocessing images to improve their recognizability. Our binarization preprocessing API converts the image into a light and dark view, making it easier to perform accurate OCR conversions. You can easily incorporate this API using the code below (Java, with instructions to install using Maven).

Get started by adding a reference to the repository in pom.xml:

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

Then add one to the dependency:

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

One final step — include the import classes, then call the preprocessing function:

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

Now you’re all done — don’t forget to include your API key (available with a free account on www.cloudmersive.com) and inputs where indicated in the documentation.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store