如何使用 C# 的 OCR 技術讀取特定文件

How to Read Specialized Documents with C# and IronOCR

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

要透過單一通用方法精準辨識標準文字文件、車牌、護照及照片等特定文件,實屬極為困難。 這些挑戰源於各類文件格式、版面配置與內容的多元性,以及影像品質、變形程度及專業內容的差異。 此外,隨著文件類型的範圍擴大,要掌握語境並在效能與效率之間取得平衡,將變得更加複雜。

IronOCR 提供針對特定文件(例如標準文字文件、車牌、護照及照片)執行 OCR 的專用方法,以實現最佳的準確度與效能。

快速入門:一行代碼讀取 Passport using IronOCR 的 ReadPassport 擴充功能,只需最少的設定即可擷取所有關鍵護照資訊。只要一行程式碼——假設您已安裝 IronOCR 和 AdvancedScan——您就能快速且輕鬆地獲得結構化結果資料,例如姓名、護照號碼、國籍等。

  1. using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronOcr

    PM > Install-Package IronOcr
  2. 請複製並執行此程式碼片段。

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

    立即透過免費試用,在您的專案中開始使用 IronOCR

    arrow pointer


關於此套件

方法 ReadPhotoReadScreenShot 屬於 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);
Imports IronOcr
Imports System

' Instantiate OCR engine
Private ocr = New IronTesseract()

' Configure OCR engine
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.SingleBlock

Dim input = New OcrInput()

input.LoadPdf("Five.pdf")

' Perform OCR
Dim result As OcrResult = 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;
Imports IronOcr
Imports IronSoftware.Drawing
Imports System

' Instantiate OCR engine
Private ocr = New IronTesseract()

Private inputLicensePlate = New OcrInput()

inputLicensePlate.LoadImage("LicensePlate.jpeg")

' Perform OCR
Dim result As OcrLicensePlateResult = ocr.ReadLicensePlate(inputLicensePlate)

' Retrieve license plate coordinates
Dim rectangle As Rectangle = result.Licenseplate

' Retrieve license plate value
Dim output As String = result.Text
$vbLabelText   $csharpLabel

閱讀 Passport 範例

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);
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 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

結果

閱讀 Passport

請確保文件中僅包含護照照片。 任何頁首或頁尾文字都可能混淆方法,導致產生意外的輸出結果。

查看圖片範例

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;
Imports IronOcr
Imports IronSoftware.Drawing

' Instantiate OCR engine
Private ocr = New IronTesseract()

Private inputPhoto = New OcrInput()
inputPhoto.LoadImageFrame("photo.tif", 2)

' Perform OCR
Dim result As OcrPhotoResult = ocr.ReadPhoto(inputPhoto)

' index number refer to region order in the page
Dim number As Integer = result.TextRegions(0).PageNumber
Dim textinregion As String = result.TextRegions(0).TextInRegion
Dim region As Rectangle = 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);
}
Imports IronOcr
Imports System
Imports System.Linq

' Instantiate OCR engine
Private ocr = New IronTesseract()

Private inputScreenshot = New OcrInput()
inputScreenshot.LoadImage("screenshot.png")

' Perform OCR
Dim result As OcrPhotoResult = 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 專案中無法運作,我該怎麼辦?

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

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

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

我需要什麼才能使用 IronOCR 的進階掃描功能?

若要使用 IronOCR 的進階掃描功能,需安裝 IronOcr.Extensions.AdvancedScan 套件,此套件僅限於 Windows 系統使用。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

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

準備開始了嗎?
Nuget 下載 5,888,303 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronOcr
執行範例 觀看您的圖片轉為可搜尋文字。