How to remove transparency from an image in Java

This tutorial will cover a very simple method to remove image transparency. Whether you are using it in a photo app, for printer compatibility, or web display, today’s post will have you ready to go in just a few minutes.

First on our list, add two references to our pom.xml file, so that we can use Jitpack to install our library dynamically.

Repository reference:

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

Dependency reference:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v2.75</version>
</dependency>
</dependencies>

Now simply call editRemoveTransparency using this sample code:

// 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.editRemoveTransparency(imageFile);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling EditApi#editRemoveTransparency");e.printStackTrace();}

And that’s all that needs to be done. Our image will be rendered over a white background, thus eliminating all effects of transparency. You may wish to look at our other related APIs, which cover everything from cropping to contrast, and are just as easy to set up.

Image for post
Image for post

Written by

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