How to generate a EAN-8 barcode as a PNG file in Java

Barcode generation in Java is a bit of a tricky task if gone about with a manual approach. Quite a bit of code is required and it’s probably going to take all day. As a nice alternative to this slog, this post is covering a much simpler means of getting the same results using an API.

Before we do anything else, we need to import our library. First add this repository reference:

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

Next, our dependency reference.

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>

And last, but not least, our function call for barcode generation.

// 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.GenerateBarcodeApi;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");GenerateBarcodeApi apiInstance = new GenerateBarcodeApi();String value = "value_example"; // String | Barcode value to generate fromtry {File result = apiInstance.generateBarcodeEAN8(value);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling GenerateBarcodeApi#generateBarcodeEAN8");e.printStackTrace();}

Already finished.

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