Extract Driver's License AAMVA Barcode - C# .NET 6

This tutorial shows how to perform data extraction from a PDF417 barcode that follows the AAMVA specification in a C# .NET 6 Console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to use LEADTOOLS AAMVA PDF417 SDK technology in a C# .NET 6 Console application.
Completion Time 15 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or higher
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 this tutorial.

Create the Project and Add the 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.

This tutorial requires the following NuGet packages:

For a complete list of which DLLs are required for specific barcode features, refer to Barcode Support.

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:

Add the AAMVA PDF417 Extraction Code

With the project created, the references added, the license set, and the load image code added, coding can begin. The image save code is not necessary for this tutorial, so that code can be commented out or deleted.

In the Solution Explorer, open Program.cs and add the following statements to the using block at the top of Program.cs:

C#
using System; 
using System.IO; 
using System.Text; 
using Leadtools; 
using Leadtools.Barcode; 
using Leadtools.Codecs; 

Add a new method called ExtractPDF417Data(RasterImage image). Call the new method in the Main method below the LoadImage(@"C:\LEADTOOLS23\Resources\Images\license_sample_rear_aamva.png") method.

C#
static void Main(string[] args) 
{ 
   InitLEAD(); 
 
   RasterImage image = LoadImage(@"C:\LEADTOOLS23\Resources\Images\license_sample_rear_aamva.png"); 
   ExtractPDF417Data(image); 
   Console.WriteLine("Press any key to exit..."); 
   Console.ReadKey(true); 
} 

The method's parameter will be the RasterImage loaded in the LoadImage() method. This tutorial uses this AAMVA PDF417 sample image. Add the below PDF417 AAMVA extraction code inside the new method.

C#
static void ExtractPDF417Data(RasterImage image) 
{ 
   // Create a Barcode engine 
   BarcodeEngine engine = new BarcodeEngine(); 
   BarcodeData data = engine.Reader.ReadBarcode(image, LeadRect.Empty, BarcodeSymbology.PDF417); 
   if (data.Value != null && data.Symbology == BarcodeSymbology.PDF417) 
   { 
      AAMVAID id = BarcodeData.ParseAAMVAData(data.GetData(), false); 
      if (id != null) 
      { 
         Console.WriteLine("AAMVA PDF417 Barcode Found!\n" + 
               "==============================================="); 
         Console.WriteLine($"Issuer Identification Number: {id.IssuerIdentificationNumber}\n" + 
               $"First Name: {id.FirstName.Value}\n" + 
               $"Last Name: {id.LastName.Value}\n" + 
               $"Over 21? {id.Over21}\n"); 
      } 
      else 
      { 
         Console.WriteLine("Does not meet AAMVA specifications"); 
      } 
   } 
   else 
   { 
      Console.WriteLine("PDF417 Barcode Not Found!"); 
   } 
} 

Note: There are more properties inside the AAMVAID class, the snippet above showcases a few commonly used properties.

Handling Streams

To handle the file using MemoryStream, replace the existing code in the Main() method with the following:

C#
static void Main(string[] args) 
{ 
   InitLEAD(); 
 
   using (RasterCodecs codecs = new RasterCodecs()) 
   { 
      string filepath = @"C:\LEADTOOLS23\Resources\Images\license_sample_rear_aamva.png"; 
      byte[] data = File.ReadAllBytes(filepath); 
      using (MemoryStream ms = new MemoryStream(data)) 
      { 
         RasterImage image = codecs.Load(ms); 
         ExtractPDF417Data(image); 
      } 
   } 
   //SaveImage(image, @"C:\temp\output.jpg"); 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application runs and displays the data inside the PDF417 Barcode.

Extracted PDF417 barcode data displayed to the console.

Wrap-Up

This tutorial showed how to use the BarcodeData and AAMVAID classes.

See Also

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


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