如何在 C# 中使用 OCR 讀取特定文檔

如何使用 C# 和 IronOCR 讀取專業文檔

This article was translated from English: Does it need improvement?
Translated
View the article in English

用單一的通用方法準確解讀標準文本文件、車牌、護照和照片等特定文件是非常困難的。 這些挑戰源自於每種文件類型的格式、佈局和內容的多樣性,以及影像品質、失真和專業內容的差異。 此外,隨著文件類型範圍的擴大,實現上下文理解以及平衡效能和效率變得更加複雜。

IronOCR 引入了針對特定文件(例如標準文字檔案、車牌、護照和照片)執行 OCR 的具體方法,以實現最佳的準確性和性能。

快速入門:一行讀懂護照

使用 IronOCR 的ReadPassport擴充程序,只需極少的設定即可提取所有關鍵護照資訊。只需一行程式碼(假設您已安裝 IronOCR 和 AdvancedScan),即可快速輕鬆地獲得結構化的結果數據,例如姓名、護照號碼、國家/地區等。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronOCR

    PM > Install-Package IronOcr

  2. 複製並運行這段程式碼。

    var result = new IronTesseract().ReadPassport(new OcrInput().LoadImage("passport.jpg"));
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronOCR,免費試用!
    arrow pointer


關於包裹

ReadLicensePlateReadPassportReadPhotoReadScreenShot方法都是 IronOCR 基本套件的擴充方法,需要安裝IronOcr.Extensions.AdvancedScan套件。

這些方法可與 OCR 引擎配置(例如黑名單和白名單)搭配使用。除ReadPassport方法外,所有方法均支援多種語言,包括中文、日語、韓語和拉丁字母。 請注意,每種語言都需要額外的語言包IronOcr.Languages

在 .NET Framework 上使用進階掃描功能需要專案在 x64 架構上運作。 進入專案配置,取消選取"首選 32 位元"選項即可實現此目的。 請參閱以下故障排除指南以了解更多資訊:". NET Framework 進階掃描"。

閱讀文件範例

ReadDocument方法是一種強大的文件讀取方法,專門用於讀取包含大量文字的掃描文件或紙本文件的照片。 PageSegmentationMode配置對於讀取不同佈局的文字檔案非常重要。

例如, SingleBlockSparseText類型可以從表格佈局中檢索大量資訊。 這是因為SingleBlock假定文字保持為一個區塊,而SparseText假定文字分散在整個文件中。

:path=/static-assets/ocr/content-code-examples/how-to/read-specific-document-document.cs
using IronOcr;
using System;

// Instantiate OCR engine
var ocr = new IronTesseract();

// Configure OCR engine
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.SingleBlock;

using var input = new OcrInput();

input.LoadPdf("Five.pdf");

// Perform OCR
OcrResult result = ocr.ReadDocument(input);

Console.WriteLine(result.Text);
$vbLabelText   $csharpLabel

以下方法是基本 IronOCR 套件的擴充方法,需要安裝IronOcr.Extensions.AdvancedScan套件。

讀取車牌範例

ReadLicensePlate方法針對從照片中讀取車牌進行了最佳化。 此方法傳回的特殊資訊是Licenseplate屬性,其中包含所提供文件中車牌位置的資訊。

:path=/static-assets/ocr/content-code-examples/how-to/read-specific-document-license-plate.cs
using IronOcr;
using IronSoftware.Drawing;
using System;

// Instantiate OCR engine
var ocr = new IronTesseract();

using var inputLicensePlate = new OcrInput();

inputLicensePlate.LoadImage("LicensePlate.jpeg");

// Perform OCR
OcrLicensePlateResult result = ocr.ReadLicensePlate(inputLicensePlate);

// Retrieve license plate coordinates
Rectangle rectangle = result.Licenseplate;

// Retrieve license plate value
string output = result.Text;
$vbLabelText   $csharpLabel

閱讀護照範例

ReadPassport方法經過最佳化,可透過掃描機讀區 (MRZ) 內容,從護照照片中讀取和提取護照資訊。 MRZ 是護照、身分證和簽證等官方文件中特別定義的區域。 MRZ 通常包含重要的個人信息,例如持有人的姓名、出生日期、國籍和證件號碼。 目前,此方法僅支援英語。

:path=/static-assets/ocr/content-code-examples/how-to/read-specific-document-passport.cs
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);
$vbLabelText   $csharpLabel

結果

讀取護照

請確保文件中僅包含護照照片。 任何頁首和頁尾文字都可能幹擾該方法,並導致意外的輸出。

查看照片範例

ReadPhoto方法針對讀取包含難以辨識文字的影像進行了最佳化。 此方法傳回TextRegions屬性,其中包含有關檢測到的文字的有用信息,例如RegionTextInRegionFrameNumber

:path=/static-assets/ocr/content-code-examples/how-to/read-specific-document-photo.cs
using IronOcr;
using IronSoftware.Drawing;

// Instantiate OCR engine
var ocr = new IronTesseract();

using var inputPhoto = new OcrInput();
inputPhoto.LoadImageFrame("photo.tif", 2);

// Perform OCR
OcrPhotoResult result = ocr.ReadPhoto(inputPhoto);

// index number refer to region order in the page
int number = result.TextRegions[0].PageNumber;
string textinregion = result.TextRegions[0].TextInRegion;
Rectangle region = result.TextRegions[0].Region;
$vbLabelText   $csharpLabel

閱讀螢幕截圖範例

ReadScreenShot方法針對讀取包含難以閱讀的文字的螢幕截圖進行了最佳化。 與 ReadPhoto 方法類似,它也傳回TextRegions屬性。

:path=/static-assets/ocr/content-code-examples/how-to/read-specific-document-screenshot.cs
}
using IronOcr;
using System;
using System.Linq;

// Instantiate OCR engine
var ocr = new IronTesseract();

using var inputScreenshot = new OcrInput();
inputScreenshot.LoadImage("screenshot.png");

// Perform OCR
OcrPhotoResult result = ocr.ReadScreenShot(inputScreenshot);

// Output screenshoot information
Console.WriteLine(result.Text);
Console.WriteLine(result.TextRegions.First().Region.X);
Console.WriteLine(result.TextRegions.Last().Region.Width);
Console.WriteLine(result.Confidence);
}
$vbLabelText   $csharpLabel

常見問題解答

如何使用C#中的OCR技術讀取車牌號碼?

您可以使用 IronOCR 提供的ReadLicensePlate方法從照片中準確讀取車牌號碼。此方法會傳回車牌號碼文字及其位置資訊。

從護照照片中提取資訊的最佳方法是什麼?

IronOCR 的ReadPassport方法旨在掃描護照照片中的機讀區 (MRZ),提取姓名、出生日期和證件號碼等重要資訊。

IronOCR 能辨識出照片中帶有複雜文字的文字嗎?

是的,IronOCR 中的ReadPhoto方法針對讀取難以辨認的文字影像進行了最佳化,提供了有關偵測到的文字及其區域的詳細資料。

是否可以使用 IronOCR 讀取螢幕截圖中的文字?

沒錯,IronOCR 的ReadScreenShot方法專門針對處理螢幕截圖中的文字進行了最佳化,並提供了詳細的文字區域資訊。

如何提高複雜版面文件的OCR辨識準確率?

為了提高複雜文件佈局的 OCR 識別準確率,請在 IronOCR 中設定PageSegmentationMode 。諸如 SingleBlock 和 SparseText 之類的選項對於從表格佈局中提取資訊尤其有用。

如果 IronOCR 的進階掃描功能在我的 .NET Framework 專案中無法正常運作,我該怎麼辦?

為了解決 IronOCR 在 .NET Framework 上的進階掃描功能出現問題,請確保您的專案設定為在 x64 架構上執行,方法是在專案設定中取消選取「首選 32 位元」選項。

IronOCR在語言支援方面是否有任何限制?

IronOCR 支援多種語言,包括中文、日文、韓文和拉丁字母。但是, ReadPassport方法目前僅支援英文文件。

我需要哪些條件才能使用 IronOCR 的進階掃描功能?

若要使用 IronOCR 中的進階掃描功能,需要 IronOcr.Extensions.AdvancedScan 軟體包,此軟體包僅適用於 Windows 系統。

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。

準備好開始了嗎?
Nuget 下載 5,299,091 | 版本: 2025.12 剛剛發布