How to convert a scanned image into text in Salesforce Apex using OCR
1 min readJul 11, 2020
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.