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 |
|
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.
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
leadtools.jar
leadtools.barcode.jar
leadtools.codecs.jar
For a complete list of which JAR files are required for your application, refer to Files to be Included with your Java Application.
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.
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 {
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.
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.
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.
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 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.