How to convert a scanned image into text in Salesforce Apex using OCR

Optical character recognition (OCR) is an amazing labor-saving technology, but a difficult one to setup on the fly, often taking days or even weeks to fine tune to the level of desired accuracy. Instead of dealing with all that, we will be using a Cloudmersive API that will not only get us quality results, but also allow us to be finished implementing it in less than 10 minutes. Let’s get started!

First download and extract this archive:

https://github.com/Cloudmersive/Cloudmersive.APIClient.Apex.OCR/archive/master.zip

Copy the /client folder into your project and we will then be able to call this function here:

SwagImageOcrApi api = new SwagImageOcrApi();SwagClient client = api.getClient();// Configure API key authorization: ApikeyApiKeyAuth Apikey = (ApiKeyAuth) client.getAuthentication('Apikey');Apikey.setApiKey('YOUR API KEY');Map<String, Object> params = new Map<String, Object>{'imageFile' => Blob.valueOf('Sample text file\nContents'),'recognitionMode' => 'recognitionMode_example','language' => 'language_example','preprocessing' => 'preprocessing_example'};try {// cross your fingersSwagImageToTextResponse result = api.imageOcrPost(params);System.debug(result);} catch (Swagger.ApiException e) {// ...handle your exceptions}

And voila! You now have OCR at your fingertips.

Image for post
Image for post

Written by

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

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