# User Guide for MRZ Scanner with .NET

In this guide, you will learn step by step on how to build a MRZ scanner solution with Dynamsoft Capture Vision SDK using C#.

- [User Guide for MRZ Scanner with .NET](#user-guide-for-mrz-scanner-with-net)
  - [About the Solution](#about-the-solution)
  - [System Requirements](#system-requirements)
  - [Build Your Own Application](#build-your-own-application)
    - [Create a New Project](#create-a-new-project)
    - [Install the NuGet Package](#install-the-nuget-package)
    - [Import the Namespace](#import-the-namespace)
    - [Initialize the License Key](#initialize-the-license-key)
    - [Create a CaptureVisionRouter Instance](#create-a-capturevisionrouter-instance)
    - [Invoke the Capturing](#invoke-the-capturing)
    - [Filter and Get MRZ Details](#filter-and-get-mrz-details)
    - [Build and Run the Project](#build-and-run-the-project)

## 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](https://www.dynamsoft.com/capture-vision/docs/server/programming/dotnet/index.md#system-requirements).

## 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 <a href="https://github.com/Dynamsoft/capture-vision-dotnet-samples/tree/main/Samples/MRZScanner" target="_blank">download the entire source code from here</a>.

### Create a New Project

Start Visual Studio or your preferred C# IDE.

Create a new C# Console Application project. Let's name it `MRZScanner`.

### Install the NuGet Package

In the Solution Explorer, right-click on your project and select Manage NuGet Packages.

Search for and install the `Dynamsoft.DotNet.CaptureVision.Bundle` nuget package.

### Import the Namespace

Open the `Program.cs` file and add the following namespaces.

```csharp
using Dynamsoft.Core;
using Dynamsoft.CVR;
using Dynamsoft.License;
using Dynamsoft.DCP;
```

### Initialize the License Key

If this is your first time using the library, you will need to obtain a trial license key. We recommend getting your own 30-day trial license through the following modal:

<link href="/webres/wwwroot/css/intlTelInput.min.css" rel="stylesheet" />
<script src="/webres/wwwroot/js/intlTelInput.min.js"></script>
<script src="/webres/wwwroot/js/docs/dayjs.min.js"></script>
<link rel="stylesheet" href="/webres/wwwroot/fontawesome-free-5.13.0-web/css/all.min.css" />
<link rel="stylesheet" href="/webres/wwwroot/css/docs/trialLicense.css" />

<div class="trial-license-wrap" style="display: none;">
  <div class="not-logged-in hide">
    <p class="title">Request a Trial License</p>
    <div class="result">
      <div class="key">Demo: DLS2eyJvcmdhbml6YXeyJo34567AwMDAxLTEwM34XphdGlvbk812345AxIn0= </div>
      <div type="button" class="d-btn orange-btn reveal-btn">Reveal</div>
    </div>
  </div>
  <div class="had-license hide">
    <p class="title">Your Trial License <span class="expiredTime"></span></p>
    <div class="result">
      <div class="key">
        DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTEwMTIwMDkzNiIsIm9yZ2FuaXphdGlvbklEIjoiMjAwMDAxIn0=
      </div>
      <i class="fa fa-copy"></i>
      <div type="button" class="d-btn orange-btn copy-btn">Copy</div>
      <div type="button" class="d-btn orange-btn extend-btn" style="display: none;">Extend</div>
    </div>
    <p class="note" style="display: none;color: #ec403d;">You've reached the limit for trial requests. Please <a href="/contact/">contact our support team</a>, and we will be happy to extend additional time for you.​</p>
    <span class="copyied">Copied!</span>
  </div>
  <div class="request-success hide">
    <p class="title">Request a Trial License - Successful</p>
    <div class="desc">
      <i class="icon-send"></i>
      <p>The license will be sent to your email immediately. If you don't see it in your inbox, please check your
        junk/spam folder as well.</p>
    </div>
  </div>
</div>

<div class="modal trial-license-request-modal" tabindex="-1">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <i class="icon-close"></i>
      <div class="modal-body">
        <div class="loading" id="loadingLogin" style="display: none;">
          <div class="lds-ring">
            <div></div>
            <div></div>
            <div></div>
            <div></div>
          </div>
        </div>
        <div class="emailErrorAlert off">
          <div class="ds-alert error">
            <p>We are unable to deliver to the email address provided. Please try a different one.</p>
            <div class="ds-alert-close-btn"></div>
          </div>
        </div>
        <div class="third-party-request">
          <p>One Click to Request a 30-Day Trial License</p>
          <div class="login-btns">
            <div type="button" class="third-party-btn github-btn"><i class="fab fa-github"></i>Github</div>
            <div type="button" class="third-party-btn microsoft-btn"><i class="fab fa-microsoft"></i>Microsoft</div>
            <div type="button" class="third-party-btn google-btn">
              <div class="gsi-material-button-icon">
                  <svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" xmlns:xlink="http://www.w3.org/1999/xlink" style="display: block;">
                      <path fill="#EA4335" d="M24 9.5c3.54 0 6.71 1.22 9.21 3.6l6.85-6.85C35.9 2.38 30.47 0 24 0 14.62 0 6.51 5.38 2.56 13.22l7.98 6.19C12.43 13.72 17.74 9.5 24 9.5z"></path>
                      <path fill="#4285F4" d="M46.98 24.55c0-1.57-.15-3.09-.38-4.55H24v9.02h12.94c-.58 2.96-2.26 5.48-4.78 7.18l7.73 6c4.51-4.18 7.09-10.36 7.09-17.65z"></path>
                      <path fill="#FBBC05" d="M10.53 28.59c-.48-1.45-.76-2.99-.76-4.59s.27-3.14.76-4.59l-7.98-6.19C.92 16.46 0 20.12 0 24c0 3.88.92 7.54 2.56 10.78l7.97-6.19z"></path>
                      <path fill="#34A853" d="M24 48c6.48 0 11.93-2.13 15.89-5.81l-7.73-6c-2.15 1.45-4.92 2.3-8.16 2.3-6.26 0-11.57-4.22-13.47-9.91l-7.98 6.19C6.51 42.62 14.62 48 24 48z"></path>
                      <path fill="none" d="M0 0h48v48H0z"></path>
                  </svg>
              </div>
              Google
            </div>
          </div>
        </div>
        <p class="devide">or continue with a work email</p>
        <div class="dynamsoft-request">
          <div class="basicForm">
            <div class="basicFormGroup" style="grid-area: a">
              <input type="email" id="Email" name="email" class="basicFormInput" autofocus placeholder=" " />
              <div class="placeholder"><span>*</span> Business Email Address</div>
            </div>
            <div class="clearfix" style="grid-area: b">
              <div class="basicFormGroup fl" style="width: 49.5%">
                <input type="text" id="FirstName" class="basicFormInput" placeholder=" ">
                <div class="placeholder"><span>*</span> First Name</div>
              </div>
              <div class="basicFormGroup fr" style="width: 49.5%">
                <input type="text" id="LastName" class="basicFormInput" placeholder=" ">
                <div class="placeholder"><span>*</span> Last Name</div>
              </div>
            </div>
            <div class="basicFormGroup companyField" style="grid-area: c">
              <input type="text" id="CompanyName" class="basicFormInput" placeholder=" ">
              <div class="placeholder"><span>*</span> Company Name</div>
            </div>
            <div class="basicFormGroup phoneNumberField" style="grid-area: d">
              <input type="tel" id="PhoneNumber" class="basicFormInput" autocomplete="new-password" placeholder=" ">
              <div class="placeholder"><span>*</span> Phone Number</div>
            </div>
          </div>
          <div class="generateLine">
            <div type="button" class="generateLicense" value="Generate a License">Generate a License</div>
            <p>The license will be sent to your email immediately.</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="modal trial-license-extend-modal" tabindex="-1">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <i class="icon-close"></i>
      <div class="modal-body">
        <div class="extensionSurvey">
          <div class="survey survey1">
            <div class="form-group">
              <label class="control-label">Are you building an app for your own company or for a client? <span
                  class="fontOrange">*</span></label>
              <div class="radio-group">
                <div class="radio">
                  <label class="radio">
                    <input name="BuildingAppUser" id="BuildingAppUser1" type="radio" value="Own Company" required>
                    <label for="BuildingAppUser1"><span>Own Company</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="BuildingAppUser" id="BuildingAppUser2" type="radio" value="A Client">
                    <label for="BuildingAppUser2"><span>A Client</span></label>
                  </label>
                </div>
              </div>
            </div>
            <div class="form-group">
              <label class="control-label">Which one of the following deployment types best describes your <span
                  class="showQ1A2" style="display: none;">client's</span> usage scenario? <span
                  class="fontOrange">*</span></label>
              <div class="radio-group">
                <div class="radio">
                  <label class="radio">
                    <input name="UsageScenario" data-businessType="100000000" id="UsageScenario1" type="radio"
                      value="Internal use by employees and contractors of my company only" required>
                    <label for="UsageScenario1"><span>Internal use by employees and contractors of my <span
                          class="showQ1A2" style="display: none;">client's</span> company only</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="UsageScenario" data-businessType="100000002" id="UsageScenario2" type="radio"
                      value="Hosted software/service for consumption by customers (e.g., SaaS)">
                    <label for="UsageScenario2"><span>Hosted software/service for consumption by customers (e.g.,
                        SaaS)</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="UsageScenario" data-businessType="100000001" id="UsageScenario3" type="radio"
                      value="Product for sale to customers for on-premises use">
                    <label for="UsageScenario3"><span>Product for sale to customers for on-premises use</span></label>
                  </label>
                </div>
              </div>
            </div>
            <div class="form-group">
              <label class="control-label">When do you need to release your application with the SDK feature integrated?
                <span class="fontOrange">*</span></label>
              <div class="radio-group">
                <div class="radio">
                  <label class="radio">
                    <input name="ReleaseTime" id="ReleaseTime1" type="radio" value="Less than a week" required>
                    <label for="ReleaseTime1"><span>Less than a week</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="ReleaseTime" id="ReleaseTime2" type="radio" value="Less than a month">
                    <label for="ReleaseTime2"><span>Less than a month</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="ReleaseTime" id="ReleaseTime3" type="radio" value="1 to 3 months">
                    <label for="ReleaseTime3"><span>1 to 3 months</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="ReleaseTime" id="ReleaseTime4" type="radio" value="3 to 6 months">
                    <label for="ReleaseTime4"><span>3 to 6 months</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="ReleaseTime" id="ReleaseTime5" type="radio" value="More than 6 months">
                    <label for="ReleaseTime5"><span>More than 6 months</span></label>
                  </label>
                </div>
              </div>
            </div>
            <div class="form-group">
              <label class="control-label">How many end users (persons) will need the SDK feature in your application?
                <span class="fontOrange">*</span></label>
              <div class="radio-group">
                <div class="radio">
                  <label class="radio">
                    <input name="CompanySize" id="CompanySize1" type="radio" value="Less than 30" required>
                    <label for="CompanySize1"><span>Less than 30</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="CompanySize" id="CompanySize2" type="radio" value="31 - 100">
                    <label for="CompanySize2"><span>31 - 100</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="CompanySize" id="CompanySize3" type="radio" value="101 - 500">
                    <label for="CompanySize3"><span>101 - 500</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="CompanySize" id="CompanySize4" type="radio" value="501 - 1000">
                    <label for="CompanySize4"><span>501 - 1000</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="CompanySize" id="CompanySize5" type="radio" value="More than 1000">
                    <label for="CompanySize5"><span>More than 1000</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="CompanySize" id="CompanySize6" type="radio" value="I don't know">
                    <label for="CompanySize6"><span>I don't know</span></label>
                  </label>
                </div>
              </div>
            </div>
            <div class="form-group">
              <label class="control-label">So far, does our SDK meet your need well? <span
                  class="fontOrange">*</span></label>
              <div class="radio-group">
                <div class="radio">
                  <label class="radio">
                    <input name="CustomerReviews" id="CustomerReviews1" type="radio" value="Yes, I am satisfied."
                      required>
                    <label for="CustomerReviews1"><span>Yes, I am satisfied.</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="CustomerReviews" id="CustomerReviews2" type="radio"
                      value="Yes, I just need more time to complete the development.">
                    <label for="CustomerReviews2"><span>Yes, I just need more time to complete the
                        development.</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="CustomerReviews" id="CustomerReviews3" type="radio"
                      value="I haven't tried the SDK yet.">
                    <label for="CustomerReviews3"><span>I haven't tried the SDK yet.</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="CustomerReviews" id="CustomerReviews4" type="radio"
                      value="No, I need some technical help.">
                    <label for="CustomerReviews4"><span>No, I need some technical help.</span></label>
                  </label>
                  <textarea id="MyTechnicalHelp" rows="2" name="MyTechnicalHelp" placeholder=""></textarea>
                </div>
              </div>
            </div>
          </div>
          <div class="survey survey2">
            <div class="form-group">
              <label class="control-label">Based on your current estimation, how soon will you release the application
                with the SDK features? <span class="fontOrange">*</span></label>
              <div class="radio-group">
                <div class="radio">
                  <label class="radio">
                    <input name="ReleaseTimeB" id="ReleaseTimeB1" type="radio" value="Less than a week" required>
                    <label for="ReleaseTimeB1"><span>Less than a week</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="ReleaseTimeB" id="ReleaseTimeB2" type="radio" value="Less than a month">
                    <label for="ReleaseTimeB2"><span>Less than a month</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="ReleaseTimeB" id="ReleaseTimeB3" type="radio" value="1 to 3 months">
                    <label for="ReleaseTimeB3"><span>1 to 3 months</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="ReleaseTimeB" id="ReleaseTimeB4" type="radio" value="3 to 6 months">
                    <label for="ReleaseTimeB4"><span>3 to 6 months</span></label>
                  </label>
                </div>
                <div class="radio">
                  <label class="radio">
                    <input name="ReleaseTimeB" id="ReleaseTimeB5" type="radio" value="More than 6 months">
                    <label for="ReleaseTimeB5"><span>More than 6 months</span></label>
                  </label>
                </div>
              </div>
            </div>
            <div class="form-group">
              <label class="control-label">How has your experience been with the SDK during your trial period? <span
                  class="fontOrange">*</span></label>
              <div class="textarea-group">
                <textarea id="OtherInfo" rows="2" name="OtherRequiredOrSuggestion" placeholder=""></textarea>
              </div>
            </div>
          </div>
          <div class="submitSurvyLine">
            <span class="d-btn submit-btn fontOswald">Submit</span>
            <p>The license will be sent to your email immediately.</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<script src="/webres/wwwroot/js/docs/trialLicense.js"></script>

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

```csharp
int errorCode = 1;
string errorMsg;
errorCode = LicenseManager.InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", out errorMsg);
if (errorCode != (int)EnumErrorCode.EC_OK && errorCode != (int)EnumErrorCode.EC_LICENSE_CACHE_USED)
    Console.WriteLine("License initialization error: " + errorMsg);
```

> The string "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9" here is a free public trial license. Note that network connection is required for this license to work. Please replace it with your own 30-day trial license.

### Create a CaptureVisionRouter Instance

```csharp
using (CaptureVisionRouter cvRouter = new CaptureVisionRouter())
{
    //code for invoking the capturing
}
```

### Invoke the Capturing

```csharp
using (CaptureVisionRouter cvRouter = new CaptureVisionRouter())
{
    string imageFile = "[PATH-TO-THE-IMAGE-FILE]";
    using (CapturedResult? result = cvRouter.Capture(imageFile, "ReadPassportAndId"))
    {
        //code for filtering and getting MRZ details
    }
}
```

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

### Filter and Get MRZ Details

```csharp
if (result == null)
{
    Console.WriteLine("No MRZ detected.");
}
else
{
    if (result.GetErrorCode() != 0)
    {
        Console.WriteLine("Error: " + result.GetErrorCode() + ", " + result.GetErrorString());
    }
    ParsedResult? parsedResult = result.GetParsedResult();
    if (parsedResult == null || parsedResult.GetItems().Length == 0)
    {
        Console.WriteLine("No MRZ results.");
    }
    else
    {
        ParsedResultItem[] items = parsedResult.GetItems();
        Console.WriteLine("Detected " + items.Length + " MRZ Result(s).");
        foreach (var item in items)
        {
            string? docType = item.GetCodeType();
            Console.WriteLine("DocumentType:" + docType);
            if (docType == "MRTD_TD3_PASSPORT")
            {
                Console.WriteLine("DocumentID:" + item.GetFieldValue("passportNumber"));
            }
            else
            {
                Console.WriteLine("DocumentID:" + item.GetFieldValue("documentNumber"));
            }
            //get more field values
            //Console.WriteLine(item.GetFieldValue("[FIELD-NAME]"));
        }
    }
}
```

### Build and Run the Project

Save the `Program.cs` file and then compile and run the program using your IDE (such as Visual Studio). You will see the output message in the console like

```
Detected 1 MRZ Result(s).
DocumentType: XXX
DocumentID: XXX
```
