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.

Overview  
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>\LEADTOOLS23\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:

Note: 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.

Java
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.

Java
public static void main(String[] args) throws IOException { 
	new _Main().run(args); 
} 
 
private void run(String[] args) { 
	try { 
		Platform.setLibPath("C:\\LEADTOOLS23\\Bin\\CDLL\\x64"); 
		Platform.loadLibrary(LTLibrary.LEADTOOLS); 
		Platform.loadLibrary(LTLibrary.BARCODE); 
		Platform.loadLibrary(LTLibrary.CODECS); 
		 
		SetLicense(); 
		 
		RasterCodecs codecs = new RasterCodecs(); 
		String inputFile = "C:\\LEADTOOLS23\\Resources\\Images\\image1.cmp"; 
		String outputFile = "C:\\LEADTOOLS23\\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) { 
		System.err.println(ex.getMessage()); 
		ex.printStackTrace(); 
	} 
} 

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

Java
void WriteUPCABarcode(RasterImage _image, BarcodeEngine _bcEngine) 
{	 
	BarcodeData _data = new BarcodeData(); 
	_data.setSymbology(BarcodeSymbology.UPC_A); 
	_data.setValue("01234567890"); 
	_data.setBounds(new LeadRect(10, 10, 600, 200)); 
		 
	OneDBarcodeWriteOptions _options = new OneDBarcodeWriteOptions(); 
	_options.setEnableErrorCheck(true); 
	_options.setTextPosition(BarcodeOutputTextPosition.DEFAULT); 
		 
	_bcEngine.getWriter().writeBarcode(_image, _data, _options); 
} 

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

Java
void WriteQRBarcode(RasterImage _image, BarcodeEngine _bcEngine) 
{ 
	QRBarcodeData _data = new QRBarcodeData(); 
	_data.setSymbolModel(QRBarcodeSymbolModel.MODEL1_AUTOSIZE); 
	_data.setSymbology(BarcodeSymbology.QR); 
	_data.setValue("QR Data Value"); 
	_data.setBounds(new LeadRect(10, 250, _image.getImageWidth(), _image.getImageHeight())); 
		 
	QRBarcodeWriteOptions _options = new QRBarcodeWriteOptions(); 
	_options.setGroupNumber(0); 
	_options.setGroupTotal(0); 
	_options.setXModule(30); 
	_options.setECCLevel(QRBarcodeECCLevel.LEVEL_L); 
	_options.setHorizontalAlignment(BarcodeAlignment.NEAR); 
	_options.setVerticalAlignment(BarcodeAlignment.NEAR); 
		 
	_bcEngine.getWriter().writeBarcode(_image, _data, _options); 
} 

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

Create the SaveImage(RasterImage _image, String outputFile, RasterCodecs _codecs) method below then add it to run() after the barcode methods.

Java
void SaveImage(RasterImage _image, String outputFile, RasterCodecs _codecs) { 
	_codecs.save(_image, outputFile, RasterImageFormat.JPEG, _image.getBitsPerPixel()); 
	System.out.println("Image saved successfully."); 
} 

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.

Wrap-up

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

See Also

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


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