How to adaptively adjust the contrast of an image in Java

Adaptive contrast adjustment is a great way to enhance the visual appeal of a photo, and can be an excellent addition to any photo app or website. Accomplishing this will be surprisingly quick, as we will employ an API to do the heavy lifting for us. Total time, about 5 minutes.

We shall start by compiling our library with Jitpack. This will require references in our Maven POM file.

Repository snippet:


Dependency snippet:


And now we call editContrastAdaptive and input an imageFile:

// 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();Double gamma = 3.4D; // Double | Gamma value to adjust the contrast in the image.  Recommended value is 2.0.  Values between 0.0 and 1.0 will reduce contrast, while values above 1.0 will increase contrast.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.editContrastAdaptive(gamma, imageFile);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling EditApi#editContrastAdaptive");e.printStackTrace();}

Our image will now have its contrast adjusted automatically to make it more visually appealing. It’s really that easy.

Image for post

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