使用IronOCR在C#中提取護照數據
IronOCR的ReadPassport方法可以從護照影像中提取結構化數據,包括姓名、護照號碼、出生日期和有效期,僅需一行C#代碼,非常適合移民和安全應用。
在機場的登機櫃檯和安全移民等應用和系統中,代理人每天要處理大量護照,因此擁有一個準確提取關鍵旅行者信息的可靠系統對於確保高效、可靠的移民過程至關重要。 IronOCR程式庫提供專門為護照閱讀優化的先進OCR功能,底層使用Tesseract 5並進行了機器學習增強。
快速入門:一行代碼提取護照 MRZ 資訊此範例展示了如何使用OcrInput載入護照圖片,利用ReadPassport()提取資料,並從返回的PassportInfo中存取姓名、號碼和日期等結構化欄位。 不需要複雜的設置——僅需一行簡單的代碼。與傳統Tesseract實施不同,IronOCR提供了一個專門為文件提取設計的簡化API。
-
1使用 IronOCR 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var input = new IronOcr.OcrInput(); input.LoadImage("passport.jpg"); var passportInfo = new IronOcr.IronTesseract().ReadPassport(input).PassportInfo;Dim input As New IronOcr.OcrInput() input.LoadImage("passport.jpg") Dim passportInfo = (New IronOcr.IronTesseract()).ReadPassport(input).PassportInfoDim input As New IronOcr.OcrInput() input.LoadImage("passport.jpg") Dim passportInfo = (New IronOcr.IronTesseract()).ReadPassport(input).PassportInfo -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronOCR,並使用 免費試用
最小工作流程(5 個步驟)
- 下載C#程式庫以閱讀護照
- 導入護照圖像以供讀取
- 確保文件僅包含護照圖像,無標題或頁腳
- 使用
ReadPassport方法從映像中提取資料 - 存取OcrPassportResult屬性以查看和進一步操作提取的護照資料
如何在C#中提取護照數據?
例如,我們將使用護照影像作為輸入來展示 IronOCR 的功能。 使用OcrInput載入圖片後,您可以利用ReadPassport方法來識別和提取護照中的資訊。 此方法返回一個OcrPassportResult物件,其中包含GivenNames、Country、PassportNumber、Surname、DateOfBirth、DateOfExpiry、Gender、DocumentType、Nationality、NationalityCode、IssuingCountryCode、PersonalNumber和DateOfIssue等屬性。
ReadPassport方法是IronOCR專門文件閱讀功能的一部分,它還包括閱讀車牌、MICR支票和其他結構化文件的方法。 該方法使用先進的計算機視覺技術自動定位並提取MRZ(機器可讀區)區域。
ReadPassport方法當前不會自動旋轉輸入。 使用輸入時,請確保 MRZ 始終位於文件底部;否則,處理將失敗。我應使用何種護照影像格式?
IronOCR支持多種影像格式,包括JPG、PNG、TIFF和BMP。為獲得最佳結果,請確保您的護照影像具有足夠的分辨率(至少300 DPI)和適當的光線。 如果處理低質量掃描,可以調整DPI設置。

我需要哪些代碼來提取護照數據?
以下範例展示了擷取護照資料的完整過程。 對於處理多本護照的應用,考慮實施多線程支援以提高性能。 您還可以追蹤長時間運行操作的OCR進度。
using IronOcr;
using System;
// Instantiate OCR engine
var ocr = new IronTesseract();
using var inputPassport = new OcrInput();
inputPassport.LoadImage("passport.jpg");
// Perform OCR
OcrPassportResult result = ocr.ReadPassport(inputPassport);
// Output passport information
Console.WriteLine(result.PassportInfo.GivenNames);
Console.WriteLine(result.PassportInfo.Country);
Console.WriteLine(result.PassportInfo.PassportNumber);
Console.WriteLine(result.PassportInfo.Surname);
Console.WriteLine(result.PassportInfo.DateOfBirth);
Console.WriteLine(result.PassportInfo.DateOfExpiry);
Console.WriteLine(result.PassportInfo.Gender);
Console.WriteLine(result.PassportInfo.DocumentType);
Console.WriteLine(result.PassportInfo.Nationality);
Console.WriteLine(result.PassportInfo.NationalityCode);
Console.WriteLine(result.PassportInfo.IssuingCountryCode);
Console.WriteLine(result.PassportInfo.PersonalNumber);
Console.WriteLine(result.PassportInfo.DateOfIssue);using IronOcr;
using System;
// Instantiate OCR engine
var ocr = new IronTesseract();
using var inputPassport = new OcrInput();
inputPassport.LoadImage("passport.jpg");
// Perform OCR
OcrPassportResult result = ocr.ReadPassport(inputPassport);
// Output passport information
Console.WriteLine(result.PassportInfo.GivenNames);
Console.WriteLine(result.PassportInfo.Country);
Console.WriteLine(result.PassportInfo.PassportNumber);
Console.WriteLine(result.PassportInfo.Surname);
Console.WriteLine(result.PassportInfo.DateOfBirth);
Console.WriteLine(result.PassportInfo.DateOfExpiry);
Console.WriteLine(result.PassportInfo.Gender);
Console.WriteLine(result.PassportInfo.DocumentType);
Console.WriteLine(result.PassportInfo.Nationality);
Console.WriteLine(result.PassportInfo.NationalityCode);
Console.WriteLine(result.PassportInfo.IssuingCountryCode);
Console.WriteLine(result.PassportInfo.PersonalNumber);
Console.WriteLine(result.PassportInfo.DateOfIssue);Imports IronOcr
' Instantiate OCR engine
Dim ocr As New IronTesseract()
Using inputPassport As New OcrInput()
inputPassport.LoadImage("passport.jpg")
' Perform OCR
Dim result As OcrPassportResult = ocr.ReadPassport(inputPassport)
' Output passport information
Console.WriteLine(result.PassportInfo.GivenNames)
Console.WriteLine(result.PassportInfo.Country)
Console.WriteLine(result.PassportInfo.PassportNumber)
Console.WriteLine(result.PassportInfo.Surname)
Console.WriteLine(result.PassportInfo.DateOfBirth)
Console.WriteLine(result.PassportInfo.DateOfExpiry)
Console.WriteLine(result.PassportInfo.Gender)
Console.WriteLine(result.PassportInfo.DocumentType)
Console.WriteLine(result.PassportInfo.Nationality)
Console.WriteLine(result.PassportInfo.NationalityCode)
Console.WriteLine(result.PassportInfo.IssuingCountryCode)
Console.WriteLine(result.PassportInfo.PersonalNumber)
Console.WriteLine(result.PassportInfo.DateOfIssue)
End UsingImports IronOcr
' Instantiate OCR engine
Dim ocr As New IronTesseract()
Using inputPassport As New OcrInput()
inputPassport.LoadImage("passport.jpg")
' Perform OCR
Dim result As OcrPassportResult = ocr.ReadPassport(inputPassport)
' Output passport information
Console.WriteLine(result.PassportInfo.GivenNames)
Console.WriteLine(result.PassportInfo.Country)
Console.WriteLine(result.PassportInfo.PassportNumber)
Console.WriteLine(result.PassportInfo.Surname)
Console.WriteLine(result.PassportInfo.DateOfBirth)
Console.WriteLine(result.PassportInfo.DateOfExpiry)
Console.WriteLine(result.PassportInfo.Gender)
Console.WriteLine(result.PassportInfo.DocumentType)
Console.WriteLine(result.PassportInfo.Nationality)
Console.WriteLine(result.PassportInfo.NationalityCode)
Console.WriteLine(result.PassportInfo.IssuingCountryCode)
Console.WriteLine(result.PassportInfo.PersonalNumber)
Console.WriteLine(result.PassportInfo.DateOfIssue)
End Using從ReadPassport中我可以預期哪些輸出?
擷取的資料會以結構化的格式傳回,方便與現有系統整合。 OcrResult類別提供對所有提取資訊的全面存取。

然後我們存取從OcrPassportResult物件中獲取的PassportInfo資料成員。 擷取過程會自動處理各種護照版面與格式,在不同簽發國家提供一致的結果。
GivenNames:PassportInfo的屬性返回護照輸入的給定名稱作為字符串。 這對應於第一行MRZ數據行,位置4到44。Country:PassportInfo的屬性返回護照輸入的國家作為字符串。 這對應到 MRZ 資料的第一行,位置 2 到 3。 傳回的字串拼出簽發國家的全名,而不是縮寫。 在我們的範例中,USA 返回「美利堅合眾國」。PassportNumber:PassportInfo的屬性返回護照輸入的護照號碼作為字符串。 這對應於第二行MRZ數據行,位置1到9。Surname:PassportInfo的屬性返回護照輸入的姓氏作為字符串。 這對應於第一行MRZ數據行,位置4到44。DateOfBirth:PassportInfo的屬性返回護照輸入的出生日期作為字符串,格式為YYYY-MM-DD。 這對應於第二行 MRZ 數據,位置 14 到 19。DateOfExpiry:PassportInfo的屬性成員返回護照輸入的有效期作為字符串,格式為YYYY-MM-DD。 這相對應於第二個 MRZ 資料列,位置 22 到 27。Gender: 從MRZ位置21返回性別作為Gender.Male,Gender.Female, 或Gender.Unspecified。DocumentType: 從MRZ位置1-2返回文件類型,例如來自於護照的"P"、來自於服務的"PS"或外交的"PD"。Nationality: 從MRZ位置11-13返回英文完整國家名稱。NationalityCode: 從MRZ位置11-13返回原始ISO 3166-1 alpha-3代碼。IssuingCountryCode: 從MRZ位置3-5返回發行國的原始ISO 3166-1 alpha-3代碼。PersonalNumber: 從MRZ位置29-42返回可選的國家ID號碼。DateOfIssue: 通過在27種語言中進行最佳匹配的標籤匹配,從視覺檢查區(VIZ)提取發行日期。
我可以從護照中提取什麼MRZ信息?
IronOCR讀取每本符合(國際民用航空組織)ICAO標準的護照底部兩行的MRZ信息。 MRZ 資料由兩行資料組成,每組位置都包含獨特的資訊。 以下是一個簡表,顯示哪些資訊與該行的索引相對應。
IronOCR的MRZ解析功能旨在處理印刷質量和影像方向的變化。 對於困難的文件,您可以應用影像校正濾波器來提高識別準確性。
MRZ區域看起來如何?
MRZ 通常位於護照頁面的底部,由兩行標準文字組成。 瞭解 MRZ 結構有助於排除萃取問題和驗證結果。

第一排
| 位置 | 領域 | 描述 |
|---|---|---|
| 1 | 文件類型 | 通常“P”代表護照。 |
| 2-3 | 簽發國 | 三字母國家代碼(ISO 3166-1 alpha-3) |
| 4-44 | 姓氏和名字 | Surname followed by '<<' and then given names separated by '<' |
第二排
| 位置 | 領域 | 描述 |
|---|---|---|
| 1-9 | 護照號碼 | 唯一護照號碼 |
| 10 | 校驗位(護照號碼) | 護照號碼校驗位 |
| 11-13 | 國籍 | 三字母國籍代碼(ISO 3166-1 alpha-3) |
| 14-19 | 出生日期 | 出生日期(YYMMDD格式) |
| 20 | 校驗位(出生日期) | 出生日期校驗位 |
| 21 | 性別 | 性別('M' 代表男性,'F' 代表女性,'X' 代表未指定) |
| 22-27 | 到期日期 | 到期日(YYMMDD 格式) |
| 28 | 校驗位(到期日) | 有效期校驗位 |
| 29-42 | 個人號碼 | 可選的個人號碼(通常為國民身分證號碼) |
| 43 | 校驗位(個人號碼) | 個人號碼的校驗位 |
| 44 | 校驗位(複合) | 整體校驗位 |
我如何調試和驗證護照提取結果?
我們也可以從 IronOCR 中驗證結果,從護照影像中取得原始抽取的文字以及置信度,確認抽取的資訊是否正確。 使用上述範例,我們可以存取Confidence和Text的OcrPassportResult屬性。
為調試目的,您可能需要突出顯示文本區域以視覺上驗證哪些區域被識別出來。 此功能在排除抽取問題或最佳化掃描區域時尤其有用。
using IronOcr;
using System;
// Instantiate OCR engine
var ocr = new IronTesseract();
using var inputPassport = new OcrInput();
inputPassport.LoadImage("passport.jpg");
// Perform OCR
OcrPassportResult result = ocr.ReadPassport(inputPassport);
// Output Confidence level and raw extracted text
Console.WriteLine(result.Confidence);
Console.WriteLine(result.Text);using IronOcr;
using System;
// Instantiate OCR engine
var ocr = new IronTesseract();
using var inputPassport = new OcrInput();
inputPassport.LoadImage("passport.jpg");
// Perform OCR
OcrPassportResult result = ocr.ReadPassport(inputPassport);
// Output Confidence level and raw extracted text
Console.WriteLine(result.Confidence);
Console.WriteLine(result.Text);Imports IronOcr
Imports System
' Instantiate OCR engine
Private ocr = New IronTesseract()
Private inputPassport = New OcrInput()
inputPassport.LoadImage("passport.jpg")
' Perform OCR
Dim result As OcrPassportResult = ocr.ReadPassport(inputPassport)
' Output Confidence level and raw extracted text
Console.WriteLine(result.Confidence)
Console.WriteLine(result.Text)Imports IronOcr
Imports System
' Instantiate OCR engine
Private ocr = New IronTesseract()
Private inputPassport = New OcrInput()
inputPassport.LoadImage("passport.jpg")
' Perform OCR
Dim result As OcrPassportResult = ocr.ReadPassport(inputPassport)
' Output Confidence level and raw extracted text
Console.WriteLine(result.Confidence)
Console.WriteLine(result.Text)
Confidence:來自OcrPassportResult的Confidence屬性是一個浮點數,表示OCR統計精確度的信心,為每個字元的平均值。 如果護照影像模糊或包含其他資訊,則此浮動值較低。 1 代表最高和最有信心,0 代表最低和最沒信心。 對於生產應用程序,請考慮實施結果信心水準閾值以確保數據質量。Text:來自OcrPassportResult的Text屬性包含從護照圖片中提取的原始、未解析的文本。 開發人員可在單元測試中使用,以驗證擷取的護照影像文字。 對於高級場景,可以以hOCR格式導出結果以進行進一步分析。
護照掃描應用的最佳實踐
在生產環境中實施護照掃描時,請考慮這些額外因素:
- 影像質量:確保輸入圖像符合最低質量標準。 濾波向導可以幫助優化影像以提高識別率。
- 性能:對於高容量處理,實施異步支援並考慮批量處理多本護照。
- 安全:由於護照數據較為敏感,確保正確的數據處理,並考慮與安全的文件管理系統整合。
- 驗證:實施檢核位驗證,以確保提取的MRZ數據的準確性。 MRZ 格式包含多個校驗碼,可用於驗證擷取資訊的完整性。
- 錯誤處理:對於護照圖像可能損壞、照明差或含有非標準格式的情況,實施可靠的錯誤處理。
常見問題
IronOCR的ReadPassport方法用於什麼?
IronOCR的ReadPassport方法用於從護照圖像中提取結構化數據,如姓名、護照號碼、出生日期和到期日期,使其成為移民和安全應用的理想選擇。
IronOCR如何幫助使用C#提取護照數據?
IronOCR通過使用ReadPassport方法簡化護照數據提取,該方法提供了一個簡單的API,用於在一行C#代碼中提取MRZ數據、姓名、護照號碼和日期。
使用IronOCR從護照中可以提取什麼樣的信息?
使用IronOCR,您可以從護照中提取信息,如名字、護照號碼、國家、出生日期、國碼以及其他MRZ數據字段。
IronOCR是否特定於任何護照語言?
目前,IronOCR的ReadPassport方法針對基於英語的護照進行了優化,但包括了處理打印質量和圖像方向變化的先進計算機視覺技術。
使用IronOCR進行護照掃描時的一些最佳實踐是什麼?
最佳實踐包括確保高質量圖像(300 DPI)、使用檢查數字驗證提取的MRZ數據,實施可靠的錯誤處理,並對敏感的護照數據使用安全的數據管理。
在Mac上使用ReadPassport方法的要求是什麼?
在Mac上使用ReadPassport時,確保MRZ位於輸入圖像的底部,因為不支持自動旋轉,項目需要在先進的.NET Framework的x64架構上運行。
IronOCR支持護照OCR的哪些圖像格式?
IronOCR支持多種圖像格式,包括JPG、PNG、TIFF和BMP,用於護照OCR並建議解析度至少為300 DPI的圖像。
IronOCR能處理非標準護照格式嗎?
IronOCR的MRZ解析設計能夠處理護照印刷質量和圖像方向的變化,使其適應不同的護照佈局和格式。
IronOCR如何確保提取護照數據的準確性?
IronOCR在OcrPassportResult中提供了Confidence屬性,表示OCR流程的統計準確性,開發者可以實施質量保證的門檻。
IronOCR為MRZ提取驗證提供了哪些其他功能?
IronOCR包括對MRZ數據字段進行檢查數字驗證的功能,以確保提取信息的準確性和完整性。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。