How to Invert, Negate the Colors in an Image using Java

Cloudmersive
2 min readSep 13, 2022

--

Each color within an image has an opposite value on the color wheel. So when we invert the colors within an image, we’re asking its pixels within it to jump to the other side of the color spectrum and produce a “negative” of the original image. Using our Image Inversion/Negation API, you can easily create this effect for any input image — not to mention, you can remove any transparency from the image in the process (this will be removed automatically prior to color inversion).

It’s easy to take advantage of this API for free using the ready-to-run Java code examples provided below to structure your API call. If you haven’t used a Cloudmersive API before, I have good news: you can use this API for free (up to 800 API calls per month) by registering a free account on our website. Your account will provide you with the API key you need to authenticate the service.

Our first step is to install the API client. We can do so by first adding a reference to the pom.xml repository:

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

Next, we can add a reference to the pom.xml dependency:

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

Then we can include our Imports at the top of our file:

// 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;

Lastly, we can call the image editing function, passing arguments through our input file path & API key parameters:

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");
EditApi apiInstance = new EditApi();
File imageFile = new File("/path/to/inputfile"); // 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();
}

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet