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

Generating barcodes is a crucial part of many businesses, but is not all that easy to pull off programmatically with Java. Today, we will be rectifying that situation, setting up easy barcode generation with just one function call. To accomplish this so quickly, we are going to install and use a barcode API.

First we should add this repositories reference.

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

Then another reference in dependencies.

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

And now we are able to call our function using this bit of sample code here:

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

And you are officially done! Easy-peasy.

Image for post
Image for post

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