Detect and Extract MRTD - Console C#

This tutorial shows how to create a C# Windows Console application that will utilize MRTD extraction and processing using the LEADTOOLS SDK.

Summary This tutorial covers how to use LEADTOOLS MRTD SDK technology in a C# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform Windows Console C# Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Detect and Extract MRTD - Console C# tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License 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. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:

If using NuGet references, this tutorial requires the following NuGet packages:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64:

For a complete list of which DLL 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:


Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the MRTD Reader and Processing Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top.

// Using block at the top 
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Forms.Commands; 
using Leadtools.Ocr; 

Add a new method called ExtractMRTD(RasterImage image) then call it inside the Main method. Add the below MRTD detection and recognition code inside the new method. The method's parameter will be the RasterImage loaded in RasterImage image = LoadImage(@"C:\LEADTOOLS22\Resources\Images\mrz_sample.jpg");. For this tutorial use this MRTD sample image.

// Main method 
static void Main(string[] args) 
    RasterImage image = LoadImage(@"C:\LEADTOOLS22\Resources\Images\mrz_sample.jpg"); 
static RasterImage LoadImage(string filename) 
   using (RasterCodecs codecs = new RasterCodecs()) 
         return codecs.Load(filename); 
static void ExtractMRTD(RasterImage image) 
    MRTDReader mrtdReader = new MRTDReader(); 
    // Create the OCR Engine 
    using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
        ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime"); 
        mrtdReader.OcrEngine = ocrEngine; 
        // Process Image 
        // Output values 
        if (mrtdReader.Errors == MRTDErrors.NoError) 
            foreach (var value in mrtdReader.Results) 
                Console.WriteLine(string.Format("Data Element Field: {0}", value.Key.ToString())); 
                Console.WriteLine(string.Format("Data Element Value: {0}", value.Value.ReadableValue)); 
                Console.WriteLine(string.Format("Data Element Code : {0}", value.Value.MrzCharacters)); 
                Console.WriteLine(string.Format("Data Element Valid: {0}", value.Value.IsValid.ToString())); 

Handling Streams

To handle the use of MemoryStream, replace the code in the LoadImage() method with the following:

static RasterImage LoadImage(string filename) 
    using (RasterCodecs codecs = new RasterCodecs()) 
        byte[] data = File.ReadAllBytes(filename); 
        using (MemoryStream ms = new MemoryStream(data)) 
            return codecs.Load(ms); 

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 passport image's MRTD information.

Extracted MRTD information


This tutorial showed how to load an image and run MICR recognition. Also it covered how to use the MRTDReader class.

See Also

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

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