How to Create a Barcode Image in Java
The following five APIs will allow you to create barcode images in the three previously mentioned formats.
Join the DZone community and get the full member experience.
Join For FreeBarcodes are used universally in commerce and retail to aid in tracking, purchasing, and inventory. This allows organizations to keep accurate records of their supplies, products, and other items that are pivotal to their operations. For international and national use, there are different barcodes that are utilized for specific contexts. For example, UPC and EAN barcodes look similar in their formatting and can be used in similar ways, but UPC is a largely North American coding system; however, both UPC and EAN are used globally. QR Codes are also used globally but are usually employed for sharing complex sets of information like item details or website links.
The following five APIs will allow you to create barcode images in the three previously mentioned formats. This will allow you to print or create packaging with your personal barcodes attached, without needing to retrieve it from an outside source. This can be especially useful for small businesses as it provides increased independence as a company and improved visual, professional appeal for your products.
To use any of the following APIs, you will first need to install the SDK reference with Maven or Gradle. To install with Maven, add a Jitpack reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then, add a reference to the dependency:
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>
To install with Gradle, add the reference to your root build.gradle at the end of repositories:
xxxxxxxxxx
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Then, add the dependency in build.gradle:
xxxxxxxxxx
dependencies {
implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'
}
The first set of functions are centered on creating UPC barcodes in two formats: UPC-A and UPC-E. UPC-A is the standard UPC barcode type, containing 12 digits that encode the product’s information. The UPC-E acts as a compressed form of this code, as it can take the original 12-digit value and suppressing it to 6 digits with a seventh checksum digit. If your UPC value does not start with zero, however, you cannot compress it into a UPC-E code. These should be mainly used in retail and simple product encoding as they are not equipped to convey complex information.
To create a UPC-A barcode, install the SDK as shown above, and then call the function:
xxxxxxxxxx
// 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 | UPC-A barcode value to generate from
try {
byte[] result = apiInstance.generateBarcodeUPCA(value);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling GenerateBarcodeApi#generateBarcodeUPCA");
e.printStackTrace();
}
The only input necessary for this function is the UPC barcode value from which the image will be generated. This will return an output file containing the barcode image as a PNG. To ensure that this API works properly, you need to ensure certain requirements are met:
The UPC barcode value is valid and inputs correctly.
You have input your API Key. This can be retrieved at no cost on the Cloudmersive website, providing 800 monthly calls across our API library.
To create a UPC-E code, follow the same steps to install and call the following function:
xxxxxxxxxx
// 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 | UPC-E barcode value to generate from
try {
byte[] result = apiInstance.generateBarcodeUPCE(value);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling GenerateBarcodeApi#generateBarcodeUPCE");
e.printStackTrace();
}
EAN barcodes were created for use in Europe but are recognized internationally. The two types of EAN code are EAN-13 and EAN-8, with EAN-13 being the most widely used. Like the UPC format EAN codes can only encode numeric data, which allows it to be scanned quickly and easily. EAN-13 contains 13 digits that represent the country code, product and manufacturer information, and the checksum digits. EAN-8 contains 8 digits and is intended for use on products with packaging that is too small to feature an EAN-13 code.
To generate an EAN-13 code, install the SDK as shown above and then call the function:
xxxxxxxxxx
// 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 | Barcode value to generate from
try {
byte[] result = apiInstance.generateBarcodeEAN13(value);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling GenerateBarcodeApi#generateBarcodeEAN13");
e.printStackTrace();
}
For an EAN-8 code, perform the same initial steps and call the function below:
xxxxxxxxxx
// 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 | Barcode value to generate from
try {
byte[] result = apiInstance.generateBarcodeEAN8(value);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling GenerateBarcodeApi#generateBarcodeEAN8");
e.printStackTrace();
}
All these previous barcode formats are considered 1D types, as they can only encode simple, numerical data like product codes. However, if you wish to convey more complex information, a QR Code, which is a 2D format, is ideal. Short for Quick Return code, a QR code can be used to convey a wide variety of information by scanning with a QR code enabled device such as a smartphone. These can be used for product details, marketing purposes, and sharing service information such as menus.
To generate a QR Code, call the following function:
xxxxxxxxxx
// 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 {
byte[] result = apiInstance.generateBarcodeQRCode(value);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling GenerateBarcodeApi#generateBarcodeQRCode");
e.printStackTrace();
}
Input for this function can include any freeform text, and thus is flexible according to your requirements.
All these functions will return a PNG file containing your barcode image for use on any product or item. If you have any questions about using these APIs or inquiries concerning other API solutions, you can visit the Cloudmersive website, where our team is happy to help with anything you might need.
Opinions expressed by DZone contributors are their own.
Comments