How to invert or negate the colors in an image using Java

Cloudmersive
2 min readJan 26, 2020

--

Color inversion is a good tool for any photo app, so how can we implement it without wasting a bunch of time? The answer is simple: we shall employ an API and let it do the work for us!

Let us begin by installing our library using Jitpack. This will necessitate the following references in pom.xml

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, simply call editInvert, as you can see demonstrated below:

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

You’re done! No seriously, that’s all there is to it. If you appreciate how easy this is to implement, you may wish to look at our other image processing APIs, which cover everything from file format conversion to cropping.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet