How to generate a QR code barcode as a PNG file in Java

QR codes offer a ton of versatility, whether it be for a mobile app, point of sale system, or marketing campaign. Their 2D format provides the capacity for relatively large quantities of data to be stored and accessed at will. Such a useful tool should have an easy method of implementation, and that’s why we are here today. Let’s dive right in.

The first thing we need to do is set up our references for the repository in our Maven POM file, like so:

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

And the same for our dependency.

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

Finally, we call generateBarcodeQRCode and provide it with a String value to be encoded:

// 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: 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");
GenerateBarcodeApi apiInstance = new GenerateBarcodeApi();
String value = "value_example"; // String | QR code text to convert into the QR code barcode
try {
File result = apiInstance.generateBarcodeQRCode(value);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling GenerateBarcodeApi#generateBarcodeQRCode");
e.printStackTrace();
}

And we are done. Pretty easy, right? If you have other barcode needs, such as UPC-E or EAN-8, we have a variety of other APIs that cover each format.

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