This tutorial shows how to perform data extraction from a PDF417 barcode that follows the AAMVA specification in a macOS Swift Console application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to perform data extraction from a PDF417 that follows the AAMVA specification in a macOS Swift Console application. |
Completion Time | 20 minutes |
Visual Studio Project | Download tutorial project (5 KB) |
Platform | macOS Swift Console Application |
IDE | Xcode |
Development License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project and loading an image by reviewing the Add References and Set a License and Load and Save Images tutorials, before working on the Extract Driver's License AAMVA Barcode - macOS Swift Console tutorial.
Start with a copy of the project created in the Load and Save Images tutorial. If you do not have that project, follow the steps in that tutorial to create it.
The references needed depend upon the purpose of the project. This tutorial requires the following Framework references located at <INSTALL_DIR>\LEADTOOLS23\Bin\Xcode\Frameworks\macOS
:
Leadtools.Barcode.PdfRead.framework
Leadtools.Barcode.framework
Leadtools.Codecs.Png.framework
Leadtools.Codecs.framework
Leadtools.framework
Leadtools.ImageProcessing.Color.framework
Leadtools.ImageProcessing.Core.framework
Leadtools.ImageProcessing.Utilities.framework
Edit the Leadtools-Bridging-Header.h
file to add the following imports:
#import <Leadtools.Barcode/Leadtools.Barcode.h>
#import <Leadtools.Codecs/Leadtools.Codecs.h>
For a complete list of which files are required for your application, refer to Files to be Included in your 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, and the license set, coding can begin.
Using the Project Navigator, open main.swift
. Add a new function named ExtractPDF417Data(image: LTRasterImage)
. Call this function below the call to the LoadImage()
function, passing in the RasterImage
object gathered from that function, as shown below.
SetLicense()
let codecs = LTRasterCodecs()
let inputFile = "<INSTALL_DIR>/LEADTOOLS23/Resources/Images/license_sample_rear_aamva.png"
guard let image = LoadImage(file: inputFile, codecs: codecs) else { fatalError("Failed to load image") }
ExtractPDF417Data(image: image)
Note: Creation of the
LoadImage()
function is covered in the Load and Save Images tutorial.
Add the following code to the ExtractPDF417Data()
function to detect the PDF417 barcode and output the extracted data to the console. For the purposes of this tutorial, this sample PNG is used.
func ExtractPDF417Data(image: LTRasterImage) {
let engine = LTBarcodeEngine()
var error: NSError?
if let data: LTBarcodeData = engine.reader.readBarcode(image, searchBounds: LeadRect.zero, symbology: LTBarcodeSymbology.PDF417, error: &error) {
if let id = try? LTBarcodeData.parseAAMVAData(data.data!, strictMode: false) {
print("AAMVA PDF417 Barcode Found!\n" +
"==========================================")
print("Issuer Identification Number: \(id.issuerIdentificationNumber ?? "")" +
"\nFirst Name: \(id.firstName?.value ?? "")" +
"\nLast Name: \(id.lastName?.value ?? "")" +
"\nOver 21? \(id.over21)")
} else {
print("Does not meet AAMVA specifications")
}
} else {
print("PDF417 Barcode Not Found!\n\(error!.localizedDescription)")
}
}
Note: There are many more properties inside the
AAMVAID
class, the snippet above showcases a few commonly used properties.
Clean the project to clear any errors by selecting Product -> Clean Build Folder or Shift + Command + K.
Run the project by selecting Product -> Run or Command + R.
If the steps were followed correctly, the application runs and if a PDF417 barcode that meets the AAMVA specifications is detected, the extracted data is then displayed to the console.
This tutorial showed how to use the BarcodeData
and AAMVAID
classes to detect and extract data from a PDF417 barcode that meets the AAMVA specifications.