Scan and Extract MRZ Data from ID Cards in C#
This guide demonstrates how to extract machine-readable zone (MRZ) data from ID cards using Nutrient .NET SDK. The MRZ, typically found at the bottom of personal identification documents, is readable via optical character recognition (OCR). Nutrient .NET SDK’s advanced OCR engine enables recognition of MRZ data in machine-readable travel documents (MRTD), including passports, visas, and ID cards.
Extract MRZ information
To extract MRZ information from an ID card, follow the steps below:
-
Create a
GdPictureImaging
object and aGdPictureOCR
object. -
Select the image by passing its path to the
CreateGdPictureImageFromFile
method of theGdPictureImaging
object. -
Set the image with the
SetImage
method of theGdPictureOCR
object. -
Run the OCR process by passing
OCRSpecialContext.MRZ
to theRunOCR
method of theGdPictureOCR
object. -
Get the result of the OCR process as text with the
GetOCRResultText
method of theGdPictureOCR
object. -
Write the output to the console.
-
Release unnecessary resources.
The following example 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