How to invert or negate the colors in an image using Salesforce Apex

Color inversion can be a striking stylistic choice, however, this procedure can be difficult to pull off when using Apex. If you would rather skip a whole heap of suffering, then I have a nice shortcut that I can show you now.

We will be using an API to get our negation done quickly. We start by downloading its client files:

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

Location the client folder and copy it right into your project in Apex. Once that’s done, you will be able to use the library and call this function right here:

SwagEditApi api = new SwagEditApi();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')};try {// cross your fingersBlob result = api.editInvert(params);System.debug(result);} catch (Swagger.ApiException e) {// ...handle your exceptions}

And boom! You’re done! How easy was that!

Image for post
Image for post

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