This tutorial shows how to create a Java application that utilizes writing PDF417 AAMVA standard barcodes using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to use LEADTOOLS AAMVA Builder SDK technology in a Java application. |
Completion Time | 30 minutes |
Project | Download tutorial project (3 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 AAMVA Driver's License Barcode - Java tutorial.
Start with a copy of the project created in the Load and Save an Image - Java tutorial. If the project is not available, create it by following the steps in that tutorial.
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
(Alraedy exists from previous tutorial)leadtools.codecs.jar
(Alraedy exists from previous tutorial)leadtools.barcode.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 and make sure the following import
statements are in the 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.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Add a new method to the _Main
class called SetAAMVAIDInformation()
. Modify the main()
method to call the new method so that it becomes as follows:
public static void main(String[] args) throws IOException
{
Platform.setLibPath("C:\\LEADTOOLS21\\Bin\\CDLL\\x64");
Platform.loadLibrary(LTLibrary.LEADTOOLS);
Platform.loadLibrary(LTLibrary.CODECS);
Platform.loadLibrary(LTLibrary.BARCODE);
SetLicense();
// Create the image and write barcode
AAMVAID id = SetAAMVAIDInformation();
RasterImage image = RasterImage.create(1700, 2200, 24, 200, RasterColor.WHITE);
WriteAAMVABarcode(image, id);
// Save the image to disk file
String outputFile = "C:\\LEADTOOLS21\\Resources\\Images\\testAAMVA.jpg";
RasterCodecs codecs = new RasterCodecs();
SaveImage(image, outputFile, codecs);
}
Add the below code to set the AAMVA ID information.
static AAMVAID SetAAMVAIDInformation()
{
int subFilesCount = 1;
LocalDateTime Now = LocalDateTime.now();
LocalDateTime birthday = Now.minusYears(16);
DateTimeFormatter formatterMMddyyyy = DateTimeFormatter.ofPattern("MMddyyyy");
String[][] driversLicenseData = new String[][] {{"DDF", "N"}, // First name truncation (N = not truncated)
{"DDE", "N"}, // Last name truncation (N = not truncated)
{"DCG", "USA"}, // Country identification
{"DCF", "NONE"}, // Document discriminator
{"DAQ", "1234567890"}, // ID Number
{"DAK", "123456 "}, // Postal code
{"DAJ", "PA"}, // Address Jurisdiction Code
{"DAI", "Any Town"}, // Address City
{"DAG", "123 Main Street"}, // Address Street 1
{"DAU", "072 in"}, // Height (inches or centimeters)
{"DAY", "BRO"}, // Eye color
{"DBC", "1"}, // Sex (Male = 1, Female = 2, 9 = Not specified)
{"DBB", birthday.format(formatterMMddyyyy)}, // Date of Birth
{"DBD", Now.format(formatterMMddyyyy)}, // Document Issue Date
{"DAD", "NONE"}, // Middle Name
{"DAC", "John"}, // First name
{"DCS", "Doe"}, // Last name
{"DBA", Now.plusYears(6).format(formatterMMddyyyy)},// Expiration date
{"DCD", "M"}, // Jurisdiction-specific endorsement codes
{"DCB", "NONE"}, // Jurisdiction-specific restriction codes
{"DCA", "C"}, // Jurisdiction-specific vehicle class
{"DDJ", birthday.plusYears(21).format(formatterMMddyyyy)}, // Under 21 until
{"DDI", birthday.plusYears(19).format(formatterMMddyyyy)}, // Under 19 until
{"DDH", birthday.plusYears(18).format(formatterMMddyyyy)}, // Under 18 until
{"DAZ", "BRO"}}; // Hair color
AAMVAIDBuilder builder = new AAMVAIDBuilder();
builder.setJurisdiction(AAMVAJurisdiction.NORTH_CAROLINA, AAMVAID.lookupIssuerIdentificationNumber(AAMVAJurisdiction.NORTH_CAROLINA));
builder.setVersion(AAMVAVersion.VERSION_2016);
builder.setJurisdictionVersion("00");
builder.setNumberOfEntries(subFilesCount);
builder.setSubfileType(subFilesCount - 1, AAMVASubfileType.DL, "DL");
for (int i = 0; i < driversLicenseData.length; i++)
builder.addDataElementToSubfile(0, driversLicenseData[i][0], driversLicenseData[i][1]);
return builder.build();
}
Once the above code has been added, create a new method WriteAAMVABarcode(RasterImage _image, AAMVAID _driversLicenseID
and call the method inside the main()
method. See above main()
method for where to place this new method. Add the below code to write the PDF417 barcode to a RasterImage
.
static void WriteAAMVABarcode(RasterImage _image, AAMVAID _driversLicenseID)
{
BarcodeEngine bcEngine = new BarcodeEngine();
PDF417BarcodeData _data = new PDF417BarcodeData();
_data.setSymbology(BarcodeSymbology.PDF417);
_data.setData(_driversLicenseID.getBytes());
PDF417BarcodeWriteOptions pdf417WriteOptions = (PDF417BarcodeWriteOptions)bcEngine.getWriter().getDefaultOptions(BarcodeSymbology.PDF417);
pdf417WriteOptions.setXModule(15);
pdf417WriteOptions.setXModuleAspectRatio(3);
pdf417WriteOptions.setECCLevel(PDF417BarcodeECCLevel.LEVEL5);
pdf417WriteOptions.setSymbolWidthAspectRatio(4);
bcEngine.getWriter().calculateBarcodeDataBounds(LeadRect.getEmpty(), _image.getXResolution(), _image.getYResolution(), _data, pdf417WriteOptions);
bcEngine.getWriter().writeBarcode(_image, _data, pdf417WriteOptions);
}
Run the project by selecting Run -> Run.
If the steps were followed correctly, the application will write a PDF417 barcode on the blank RasterImage
containing the specified AAMVA data, and will be saved to a file. The output result should look like this:
This tutorial showed how to use the BarcodeWriter
, AAMVAID
and AAMVAIDBuilder
classes.