Write 1D and 2D Barcodes to an Image - Java

This tutorial shows how to create a Java application that will utilize the BarcodeEngine and BarcodeWriter classes to write barcode information to an image.

Summary This tutorial covers how to use the BarcodeWriter Class in a Java application.
Completion Time 30 minutes
Project Download tutorial project (2 KB)
Platform Java Application
IDE Eclipse
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License and Load and Save an Image tutorials, before working on the Write 1D and 2D Barcodes to an Image - Java tutorial.

Create the Project and Add LEADTOOLS References

In Eclipse, create a new Java project, and add the necessary LEADTOOLS references.

The references needed depend upon the purpose of the project. The following JAR files are needed for this tutorial:

The JAR files are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Java

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:


Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Write Barcode Code

With the project created, the references added, the license set, and the load and save code added, coding can begin.

In the Package Explorer, open the _Main.java class. Add the following import statements to the import block at the top.

import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import leadtools.*; 
import leadtools.barcode.*; 
import leadtools.codecs.*; 

Add two new methods to the _Main class called WriteUPCABarcode(RasterImage _image, BarcodeEngine _bcEngine) and WriteQRBarcode(RasterImage _image, BarcodeEngine _bcEngine). Call these methods inside the main() method after the LoadImage(inputFile, codecs) call. Also, after the load image call add a new instance of BarcodeEngine.

public static void main(String[] args) throws IOException 
	RasterCodecs codecs = new RasterCodecs(); 
	String inputFile = "C:\\LEADTOOLS21\\Resources\\Images\\image1.cmp"; 
	String outputFile = "C:\\LEADTOOLS21\\Resources\\Images\\test.jpg"; 
	RasterImage image = LoadImage(inputFile, codecs); 
	BarcodeEngine bcEngine = new BarcodeEngine(); 
    WriteUPCABarcode(image, bcEngine); 
    WriteQRBarcode(image, bcEngine); 
    SaveImage(image, outputFile, codecs); 

Add the below code to write a 1D UPCA barcode to the loaded image.

static void WriteUPCABarcode(RasterImage _image, BarcodeEngine _bcEngine) 
	BarcodeData _data = new BarcodeData(); 
	_data.setBounds(new LeadRect(10, 10, 600, 200)); 
	OneDBarcodeWriteOptions _options = new OneDBarcodeWriteOptions(); 
	_bcEngine.getWriter().writeBarcode(_image, _data, _options); 

Add the below code to write a 2D QR barcode to the loaded image.

static void WriteQRBarcode(RasterImage _image, BarcodeEngine _bcEngine) 
	QRBarcodeData _data = new QRBarcodeData(); 
	_data.setValue("QR Data Value"); 
	_data.setBounds(new LeadRect(10, 250, _image.getImageWidth(), _image.getImageHeight())); 
	QRBarcodeWriteOptions _options = new QRBarcodeWriteOptions(); 
	_bcEngine.getWriter().writeBarcode(_image, _data, _options); 


This code saves the file to the <INSTALL_DIR>\LEADTOOLS21\Resources\Images directory. If it does not exist, change the path to a valid folder.

Run the Project

Run the project by selecting Run -> Run.

If the steps were followed correctly, the application will write the UPCA and QR barcodes to the loaded RasterImage and save the image to the specified output file. The resulting RasterImage should look like this sample image.


This tutorial showed how to write a 1D and 2D barcode to a given image using the BarcodeWriter class.

See Also

Help Version 21.0.2021.5.11
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.