Table of contents

User Guide for MRZ Scanner with C++

In this guide, you will learn step by step on how to build a MRZ Scanner solution with Dynamsoft Capture Vision SDK using c++.

Table of Contents

About the Solution

With the MRZ Scanner, you can extract the MRZ string from an image of a passport, visa, or ID card, and then parse it to retrieve data such as the first name, last name, document number, nationality, date of birth, expiration date, and personal number, converting them into human-readable fields.

System Requirements

To find out whether your environment is supported, please read the System Requirements.

Installation

If you haven’t downloaded the SDK yet, download the C/C++ Package now and unpack the package into a directory of your choice.

For this tutorial, we unpack it to a pseudo directory [INSTALLATION FOLDER], change it to your unpacking path for the following content.

Build Your Own Application

In this section, we’ll walk through the key steps needed to build an application that reads the machine-readable zone (MRZ) from an image file.

You can download the complete source code from here.

Create A New Project

For Windows

  1. Open Visual Studio. Go to File > New > Project, select Empty App and enter MRZScanner in the name text box.

  2. Add a new source file named MRZScanner.cpp into the project.

For Linux

  1. Create a new source file named MRZScanner.cpp and place it into the folder [INSTALLATION FOLDER]\Samples\MRZScanner.

  2. Create a file named Makefile and put it in the same directory as the file MRZScanner.cpp. The content of Makefile is as follows:

     CC=gcc
     CCFLAGS=-c -std=c++11
    
     DS_LIB_PATH=[INSTALLATION FOLDER]/Distributables/Lib/Linux/x64
     LDFLAGS=-L $(DS_LIB_PATH) -Wl,-rpath=$(DS_LIB_PATH) -Wl,-rpath=./
     DS_LIB=-lDynamsoftCaptureVisionRouter -lDynamsoftCore -lDynamsoftLicense -lDynamsoftUtility
     DS_JSON_PATH=[INSTALLATION FOLDER]/Distributables/Templates
     DLRMODEL_PATH=[INSTALLATION FOLDER]/Distributables/CharacterModel
     CONFUSABLE_CHARS_PATH=[INSTALLATION FOLDER]/Distributables/ConfusableChars.data
     DCPRES_PATH=[INSTALLATION FOLDER]/Distributables/ParserResources
    
     STDLIB=-lstdc++
    
     TARGET=MRZScanner
     OBJECT=MRZScanner.o
     SOURCE=MRZScanner.cpp
    
     # build rule for target.
     $(TARGET): $(OBJECT)
         $(CC) -o $(TARGET) $(OBJECT) $(STDLIB) $(DS_LIB) $(LDFLAGS)
         cp -r $(DS_JSON_PATH) $(DS_LIB_PATH)
         cp -r $(DLRMODEL_PATH) $(DS_LIB_PATH)
         cp  $(CONFUSABLE_CHARS_PATH) $(DS_LIB_PATH)
         cp  $(DCPRES_PATH) $(DS_LIB_PATH)
    
     # target to build an object file
     $(OBJECT): $(SOURCE)
         $(CC) $(CCFLAGS) $(SOURCE)
    
     # the clean target
     .PHONY : clean
     clean: 
         rm -f $(OBJECT) $(TARGET) $(DS_LIB_PATH)/ConfusableChars.data -r $(DS_LIB_PATH)/Templates -r $(DS_LIB_PATH)/CharacterModel -r $(DS_LIB_PATH)/ParserResources
    

Include the Library

Add headers and libs in MRZScanner.cpp.

```cpp
#include <iostream>
#include <string>
#include "[INSTALLATION FOLDER]/Include/DynamsoftCaptureVisionRouter.h"
#include "[INSTALLATION FOLDER]/Include/DynamsoftUtility.h"

using namespace std;
using namespace dynamsoft::cvr;
using namespace dynamsoft::dlr;
using namespace dynamsoft::dcp;
using namespace dynamsoft::license;
using namespace dynamsoft::utility;

// The following code only applies to Windows.
#if defined(_WIN64) || defined(_WIN32)
    #ifdef _WIN64
        #pragma comment(lib, "[INSTALLATION FOLDER]/Distributables/Lib/Windows/x64/DynamsoftCaptureVisionRouterx64.lib")
        #pragma comment(lib, "[INSTALLATION FOLDER]/Distributables/Lib/Windows/x64/DynamsoftCorex64.lib")
        #pragma comment(lib, "[INSTALLATION FOLDER]/Distributables/Lib/Windows/x64/DynamsoftLicensex64.lib")
        #pragma comment(lib, "[INSTALLATION FOLDER]/Distributables/Lib/Windows/x64/DynamsoftUtilityx64.lib")
    #else
        #pragma comment(lib, "[INSTALLATION FOLDER]/Distributables/Lib/Windows/x86/DynamsoftCaptureVisionRouterx86.lib")
        #pragma comment(lib, "[INSTALLATION FOLDER]/Distributables/Lib/Windows/x86/DynamsoftCorex86.lib")
        #pragma comment(lib, "[INSTALLATION FOLDER]/Distributables/Lib/Windows/x86/DynamsoftLicensex86.lib")
        #pragma comment(lib, "[INSTALLATION FOLDER]/Distributables/Lib/Windows/x86/DynamsoftUtilityx86.lib")
    #endif
#endif
```

Initialize the License Key

Open the MRZScanner.cpp file and add the following code inside the Main method to initialize the license for using the SDK in the application:

```cpp
int errorcode = 0;
char error[512];
errorcode = CLicenseManager::InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", error, 512);
if (errorcode != ErrorCode::EC_OK && errorcode != ErrorCode::EC_LICENSE_CACHE_USED)
{
    cout << "License initialization failed: ErrorCode: " << errorcode << ", ErrorString: " << error << endl;
}
else
{
    // codes from following steps...
}
```

>Note:
> The string "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9" here is a free public trial license. Note that network connection is required for this license to work. When it expires, you can request a 30-day free trial license from the <a href="https://www.dynamsoft.com/customer/license/trialLicense?utm_source=guide&product=mrz&package=c_cpp" target="_blank">Customer Portal</a>.

Create a CaptureVisionRouter Instance

```cpp
CCaptureVisionRouter* router = new CCaptureVisionRouter();
```

Invoke the Capturing

```cpp
string imageFile = "[PATH-TO-THE-IMAGE-FILE]";
CCapturedResult* result = router->Capture(imageFile.c_str(), "ReadPassportAndId");
```

> Please change the `[PATH-TO-THE-IMAGE-FILE]` to a real image file path.

Filter and Get MRZ Details

```cpp
cout << "File: " << imageFile << endl;
if (result->GetErrorCode() != 0) {
    cout << "Error: " << result->GetErrorCode() << "," << result->GetErrorString() << endl;
}
CParsedResult* dcpResult = result->GetParsedResult();
if (dcpResult == NULL || dcpResult->GetItemsCount() == 0)
{
    cout << "No MRZ results." << endl;
}
else
{
    int lCount = dcpResult->GetItemsCount();
    cout << "Detected " << lCount << " MRZ Result(s)." << endl;
    for (int i = 0; i < lCount; i++)
    {
        const CParsedResultItem *item = dcpResult->GetItem(i);
        string docType = item->GetCodeType();
        cout << "DocumentType: " << docType << endl;
        if (docType == "MRTD_TD3_PASSPORT")
        {
            cout << "DocumentID: " << item->GetFieldValue("passportNumber") << endl;
        }
        else
        {
            cout << "DocumentID: " << item->GetFieldValue("documentNumber") << endl;
        }
        //get more field values
        //cout << "DocumentID: " << item->GetFieldValue("[FIELD-NAME]") << endl;
    }
}
```

Release the Allocated Memory

if (dcpResult)
    dcpResult->Release();
if (result)
    result->Release();
delete router, router = NULL;   

Build and Run the Project

On Windows

  1. Build the application through Visual Studio.

  2. Copy the following items from [INSTALLATION FOLDER]\Distributables to the same folder as the EXE file.

    • All the DLL files from .\Lib\Windows\[PLATFORMS]
    • File ConfusableChars.data
    • Folders CharacterModel, ParserResources and Templates
  3. Run the program MRZScanner.exe.

On Linux

  1. Open a terminal and change to the target directory where Makefile located in. Build the sample:

     >make
    
  2. Run the program MRZScanner.

     >./MRZScanner
    

Note:

Please change all [INSTALLATION FOLDER] in above code snippet to your unpacking path.

This page is compatible for:

Is this page helpful?

YesYes NoNo

In this article: