How to Read Specialized Documents with C# and IronOCR
用单一的通用方法准确解读标准文本文件、车牌、护照和照片等特定文件是非常困难的。 这些挑战源于每种文档类型的格式、布局和内容的多样性,以及图像质量、失真和专业内容的差异。 此外,随着文档类型范围的扩大,实现上下文理解以及平衡性能和效率变得更加复杂。
IronOCR 引入了针对特定文档(例如标准文本文件、车牌、护照和照片)执行 OCR 的具体方法,以实现最佳的准确性和性能。
快速开始:一行代码读取护照使用IronOCR的ReadPassport扩展以最小设置提取所有关键护照详细信息。只需一行代码—假设您已经安装了IronOCR和AdvancedScan—您将快速轻松地获得结构化结果数据,例如名称、护照号码、国家等。
-
1Install IronOCR with NuGet Package Manager
-
2复制并运行这段代码。
var result = new IronTesseract().ReadPassport(new OcrInput().LoadImage("passport.jpg"));C# -
3部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronOCR
最小工作流程(5 个步骤)
- 下载一个用于读取车牌、护照和照片的 C# 库
- 为OCR准备图像和PDF文档
- 设置
ReadLicensePlate方法以读取车牌 - 设置
ReadPassport方法以从护照中检索信息。 - Leverage the
ReadPhotoandReadScreenShotmethods to read images that contain hard-to-read text
关于包裹
ReadScreenShot方法是IronOCR基本包的扩展方法,需要安装IronOcr.Extensions.AdvancedScan包。
这些方法适用于OCR引擎配置,如黑名单和白名单。支持多种语言,包括中文、日文、韩文和拉丁字母表,但ReadPassport方法例外。 请注意,每种语言都需要额外的语言包IronOcr.Languages 。
在 .NET Framework 上使用高级扫描功能需要项目在 x64 架构上运行。 进入项目配置,取消选中"首选 32 位"选项即可实现此目的。 请参阅以下故障排除指南了解更多信息:". NET Framework 高级扫描"。
阅读文档示例
ReadDocument方法是一个强大的文档阅读方法,专注于扫描文档或包含大量文字的纸质文档照片。 PageSegmentationMode配置对于读取不同布局的文本文件非常重要。
例如, SingleBlock和SparseText类型可以从表格布局中检索大量信息。 这是因为SingleBlock假定文本保持为一个块,而SparseText假定文本分散在整个文档中。
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属性,其中包含所提供文档中车牌位置的信息。
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 通常包含重要的个人信息,例如持有人的姓名、出生日期、国籍和证件号码。 目前,此方法仅支持英语。
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 。
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属性。
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 架构上运行,方法是取消选中项目设置中的“首选 32 位”选项,以解决 IronOCR 在 .NET Framework 上使用高级扫描功能的问题。
IronOCR 有语言支持限制吗?
IronOCR 支持多种语言,包括中文、日语、韩语和拉丁字母。不过,ReadPassport 方法目前仅支持英语文件。
using IronOCR 的高级扫描功能需要什么?
要使用 IronOCR 的高级扫描功能,需要 IronOcr.Extensions.AdvancedScan 包,该包仅在 Windows 上可用。
What precautions should be taken when using the `ReadPassport` method?
When using the `ReadPassport` method, ensure that the image only contains the passport photo to avoid confusion and unexpected output. Any additional text like headers and footers should be excluded.
How can IronOCR be installed to perform document reading in C#?
IronOCR can be installed via the NuGet package manager. Ensure that both IronOCR and IronOcr.Extensions.AdvancedScan are installed to utilize methods for reading specialized document types like license plates and passports.

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。