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

This tutorial shows how to write barcodes onto an image and save the file in a Java application using the LEADTOOLS SDK.

Summary This tutorial covers how to use the [BarcodeEngine] and [BarcodeWriter] classes 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

Start with a copy of the project created in the Load and Save an Image tutorial. If that project is unavailable, follow the steps in that tutorial to create it.

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>\LEADTOOLS22\Bin\Java

For a complete list of which JAR files are required for your application, refer to Files to be Included with your Java Application.

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 run() 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 
	new _Main().run(args); 
private void run(String[] args) { 
	try { 
		RasterCodecs codecs = new RasterCodecs(); 
		String inputFile = "C:\\LEADTOOLS22\\Resources\\Images\\image1.cmp"; 
		String outputFile = "C:\\LEADTOOLS22\\Resources\\Images\\test.jpg"; 
		RasterImage image = LoadImage(inputFile, codecs); 
		BarcodeEngine bcEngine = new BarcodeEngine(); 
	    WriteUPCABarcode(image, bcEngine); 
		WriteQRBarcode(image, bcEngine); 
	    SaveImage(image, outputFile, codecs); 
	catch(Exception ex) { 

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

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.

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>\LEADTOOLS22\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 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.