Extract Driver's License AAMVA Barcode - macOS Swift Console

This tutorial shows how to utilize 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 use LEADTOOLS AAMVA PDF417 SDK technology 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

Required Knowledge

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.

Create the Project and Add LEADTOOLS References

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>\LEADTOOLS22\Bin\Xcode\Frameworks\macOS:

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.

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 AAMVA PDF417 Extraction Code

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>/LEADTOOLS22/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.

Run the Project

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.

Console display of extracted information.

Wrap-Up

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.

See Also

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

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