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:
-
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 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