如何使用 C# 和 IronOCR 讀取專業文檔
用單一的通用方法準確解讀標準文本文件、車牌、護照和照片等特定文件是非常困難的。 這些挑戰源自於每種文件類型的格式、佈局和內容的多樣性,以及影像品質、失真和專業內容的差異。 此外,隨著文件類型範圍的擴大,實現上下文理解以及平衡效能和效率變得更加複雜。
IronOCR 引入了針對特定文件(例如標準文字檔案、車牌、護照和照片)執行 OCR 的具體方法,以實現最佳的準確性和性能。
快速入門:一行讀懂護照
使用 IronOCR 的ReadPassport擴充程序,只需極少的設定即可提取所有關鍵護照資訊。只需一行程式碼(假設您已安裝 IronOCR 和 AdvancedScan),即可快速輕鬆地獲得結構化的結果數據,例如姓名、護照號碼、國家/地區等。
立即開始使用 NuGet 建立 PDF 檔案:
使用 NuGet 套件管理器安裝 IronOCR
複製並運行這段程式碼。
var result = new IronTesseract().ReadPassport(new OcrInput().LoadImage("passport.jpg"));部署到您的生產環境進行測試
最小工作流程(5 個步驟)
- 下載一個用於讀取車牌、護照和照片的 C# 庫。
- 準備圖像和 PDF 文件以進行 OCR
- 設定
ReadLicensePlate方法以讀取車牌 - 設定
ReadPassport方法以從護照中檢索資訊。 - Leverage the
ReadPhotoand `ReadScreenShot` methods to read images that contain hard-to-read text
關於包裹
ReadLicensePlate 、 ReadPassport 、 ReadPhoto和ReadScreenShot方法都是 IronOCR 基本套件的擴充方法,需要安裝IronOcr.Extensions.AdvancedScan套件。
這些方法可與 OCR 引擎配置(例如黑名單和白名單)搭配使用。除ReadPassport方法外,所有方法均支援多種語言,包括中文、日語、韓語和拉丁字母。 請注意,每種語言都需要額外的語言包IronOcr.Languages 。
在 .NET Framework 上使用進階掃描功能需要專案在 x64 架構上運作。 進入專案配置,取消選取"首選 32 位元"選項即可實現此目的。 請參閱以下故障排除指南以了解更多資訊:". NET Framework 進階掃描"。
閱讀文件範例
ReadDocument方法是一種強大的文件讀取方法,專門用於讀取包含大量文字的掃描文件或紙本文件的照片。 PageSegmentationMode配置對於讀取不同佈局的文字檔案非常重要。
例如, SingleBlock和SparseText類型可以從表格佈局中檢索大量資訊。 這是因為SingleBlock假定文字保持為一個區塊,而SparseText假定文字分散在整個文件中。
:path=/static-assets/ocr/content-code-examples/how-to/read-specific-document-document.csusing 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)以下方法是基本 IronOCR 套件的擴充方法,需要安裝IronOcr.Extensions.AdvancedScan套件。
讀取車牌範例
ReadLicensePlate方法針對從照片中讀取車牌進行了最佳化。 此方法傳回的特殊資訊是Licenseplate屬性,其中包含所提供文件中車牌位置的資訊。
:path=/static-assets/ocr/content-code-examples/how-to/read-specific-document-license-plate.csusing 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閱讀護照範例
ReadPassport方法經過最佳化,可透過掃描機讀區 (MRZ) 內容,從護照照片中讀取和提取護照資訊。 MRZ 是護照、身分證和簽證等官方文件中特別定義的區域。 MRZ 通常包含重要的個人信息,例如持有人的姓名、出生日期、國籍和證件號碼。 目前,此方法僅支援英語。
:path=/static-assets/ocr/content-code-examples/how-to/read-specific-document-passport.csusing 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)結果

請確保文件中僅包含護照照片。 任何頁首和頁尾文字都可能幹擾該方法,並導致意外的輸出。
查看照片範例
ReadPhoto方法針對讀取包含難以辨識文字的影像進行了最佳化。 此方法傳回TextRegions屬性,其中包含有關檢測到的文字的有用信息,例如Region 、 TextInRegion和FrameNumber 。
:path=/static-assets/ocr/content-code-examples/how-to/read-specific-document-photo.csusing 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閱讀螢幕截圖範例
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)
}常見問題解答
我如何在 C# 中使用 OCR 讀取車牌?
您可以使用 IronOCR 提供的 ReadLicensePlate 方法準確地從照片中讀取車牌。此方法返回車牌文本及其位置詳情。
從護照照片中提取信息的最佳方式是什麼?
IronOCR 的 ReadPassport 方法專為掃描護照照片中的機器可讀區域(MRZ)而設計,提取重要信息如姓名、出生日期和文件號。
IronOCR 能夠從具有困難文本的照片中讀取文本嗎?
是的,IronOCR 中的 ReadPhoto 方法優化以讀取包含難以閱讀文本的圖像,提供有關檢測到的文本及其區域的詳細數據。
能否使用 IronOCR 從截圖中讀取文本?
當然可以,IronOCR 的 ReadScreenShot 方法專為處理截圖中的文本而優化,並提供詳細的文本區域資料。
如何提高具有複雜佈局文件的 OCR 準確性?
要提高複雜文件佈局的 OCR 準確性,可在 IronOCR 中配置 PageSegmentationMode。單塊和稀疏文本等選項對於從表格佈局中提取信息特別有用。
如果 IronOCR 的高級掃描功能在我的 .NET Framework 項目中無法使用,我該怎麼做?
確保您的項目設置為在 x64 架構上運行,通過在項目設置中取消選中‘首選 32 位’選項,以解決 .NET Framework 中 IronOCR 的高級掃描功能問題。
IronOCR 有語言支持限制嗎?
IronOCR 支持多種語言,包括中文、日文、韓文和拉丁字母。不過,ReadPassport 方法目前僅支持英文文檔。
使用 IronOCR 的高級掃描功能需要什麼?
要使用 IronOCR 的高級掃描功能,需要 IronOcr.Extensions.AdvancedScan 包,該包僅在 Windows 上可用。






