Scan and Extract MRZ Data from ID Cards in C#

This guide explains how to extract Machine-Readable Zone (MRZ) information from ID cards. The MRZ is usually the last few lines in a personal identification document and can be read by devices using OCR. GdPicture.NET’s optical character recognition (OCR) engine allows you to recognize the MRZ information in Machine-Readable Travel Documents (MRTD) like passports, visas, and ID cards.

Extract MRZ Information

To extract MRZ information from an ID card, follow these steps:

  1. Create a GdPictureImaging object and a GdPictureOCR object.

  2. Select the image by passing its path to the CreateGdPictureImageFromFile method of the GdPictureImaging object.

  3. Set the image with the SetImage method of the GdPictureOCR object.

  4. Run the OCR process by passing OCRSpecialContext.MRZ to the RunOCR method of the GdPictureOCR object.

  5. Get the result of the OCR process as text with the GetOCRResultText method of the GdPictureOCR object.

  6. Write the output to the console.

  7. Release unnecessary resources.

The example below extracts MRZ information from an ID card:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
using GdPictureOCR gdpictureOCR = new GdPictureOCR();
// Select the image to process.
int imageId = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.png");
// Set the image.
gdpictureOCR.SetImage(imageId);
// Run the OCR process.
string resultId = gdpictureOCR.RunOCR(OCRSpecialContext.MRZ);
// Get the result of the OCR process as text.
string mrzData = gdpictureOCR.GetOCRResultText(resultId);
// Write the output to the console.
Console.WriteLine($"MRZ information: {mrzData}");
// Release unnecessary resources.
gdpictureImaging.ReleaseGdPictureImage(imageId);
gdpictureOCR.ReleaseOCRResults();
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging()
Using gdpictureOCR As GdPictureOCR = New GdPictureOCR()
    ' Select the image to process.
    Dim imageId As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.png")
    ' Set the image.
    gdpictureOCR.SetImage(imageId)
    ' Run the OCR process.
    Dim resultId As String = gdpictureOCR.RunOCR(OCRSpecialContext.MRZ)
    ' Get the result of the OCR process as text.
    Dim mrzData As String = gdpictureOCR.GetOCRResultText(resultId)
    ' Write the output to the console.
    Console.WriteLine($"MRZ information: {mrzData}")
    ' Release unnecessary resources.
    gdpictureImaging.ReleaseGdPictureImage(imageId)
    gdpictureOCR.ReleaseOCRResults()
End Using
End Using
Used Methods and Properties

Related Topics