This tutorial shows how to create a Java application that will utilize the BarcodeEngine and BarcodeWriter classes to write barcode information to an image.
Overview | |
---|---|
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 |
|
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.
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
leadtools.jar
leadtools.barcode.jar
leadtools.codecs.jar
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 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
{
Platform.setLibPath("C:\\LEADTOOLS21\\Bin\\CDLL\\x64");
Platform.loadLibrary(LTLibrary.LEADTOOLS);
Platform.loadLibrary(LTLibrary.BARCODE);
Platform.loadLibrary(LTLibrary.CODECS);
SetLicense();
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.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.
static 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>\LEADTOOLS21\Resources\Images
directory. If it does not exist, change the path to a valid folder.
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.