How to Read Specialized Documents with C# and IronOCR
準確地以一般單一方法來閱讀特定文件,如標準文字文件、車牌、護照和照片,這是非常困難的。 這些挑戰來自每種文件型別的多樣格式、版面和內容,並且影像質量、失真和特殊內容的變異也會影響。 此外,隨著文件型別範圍的擴大,要實現上下文的理解和平衡性能與效率就變得更加複雜。
IronOCR為特定文件(如標準文字文件、車牌、護照和照片)引入了特定的OCR方法,以實現最佳的準確性和性能。
快速開始:一行程式碼閱讀護照
使用IronOCR的ReadPassport擴展來提取所有關鍵的護照詳細資訊,只需少量設定。只需一行程式碼(假設您已安裝IronOCR和AdvancedScan),您將能夠快速、輕鬆地獲得結構化的結果資料,如姓名、護照號碼、國家等。
最小化工作流程(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.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)
下列方法是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
讀取護照範例
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)
結果
請確保文件中僅包含護照圖像。 任何頁眉和頁尾文字都可能讓該方法產生意外輸出。
讀取照片範例
ReadPhoto方法優化於閱讀包含難以閱讀文字的圖像。 此方法返回TextRegions屬性,包含有關檢測文字的有用資訊,例如Region,TextInRegion,和FrameNumber。
: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
讀取截圖範例
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)
}
常見問題
如何使用OCR在C#中讀取車牌?
您可以使用IronOCR提供的ReadLicensePlate方法從照片中準確讀取車牌。此方法返回車牌文字及其位置詳細資訊。
從護照照片中提取資訊的最佳方法是什麼?
IronOCR的ReadPassport方法專為掃描護照照片中的機械可讀區(MRZ)而設計,提取姓名、出生日期和文件號碼等重要資訊。
IronOCR能否從文字困難的照片中讀取文字?
能,IronOCR中的ReadPhoto方法被優化以讀取難以閱讀的文字圖像,提供檢測到的文字及其區域的詳細資料。
是否可以使用IronOCR從截圖中讀取文字?
當然,IronOCR的ReadScreenShot方法專為處理截圖中的文字而優化,並提供詳細的文字區域資訊。
我如何提高復雜佈局文件的OCR準確性?
要提高復雜文件佈局的OCR準確性,請在IronOCR中配置PageSegmentationMode。SingleBlock和SparseText等選項對於從表格佈局中提取資訊特別有用。
如果IronOCR的高級掃描功能在我的.NET Framework專案中無法工作,我應該怎麼做?
確保您的項目設定為在x64架構下運行,通過取消勾選專案設定中的"Prefer 32-bit"選項,以解決IronOCR在.NET Framework上高級掃描功能的問題。
IronOCR在語言支援方面有任何限制嗎?
IronOCR支援包括中文、日文、韓文和拉丁字母在內的多種語言。然而,ReadPassport方法目前僅支援英語文件。
我需要什麼來使用IronOCR的高級掃描功能?
要使用IronOCR的高級掃描功能,需要IronOcr.Extensions.AdvancedScan套件,該套件僅在Windows上可用。

