如何使用 C# 和 IronOCR 读取专业文档
用单一的通用方法准确解读标准文本文件、车牌、护照和照片等特定文件是非常困难的。 这些挑战源于每种文档类型的格式、布局和内容的多样性,以及图像质量、失真和专业内容的差异。 此外,随着文档类型范围的扩大,实现上下文理解以及平衡性能和效率变得更加复杂。
IronOCR 引入了针对特定文档(例如标准文本文件、车牌、护照和照片)执行 OCR 的具体方法,以实现最佳的准确性和性能。
快速入门:一行读懂护照
使用 IronOCR 的ReadPassport扩展程序,只需极少的设置即可提取所有关键护照信息。只需一行代码(假设您已安装 IronOCR 和 AdvancedScan),即可快速轻松地获得结构化的结果数据,例如姓名、护照号码、国家/地区等。
立即开始使用 NuGet 创建 PDF 文件:
使用 NuGet 包管理器安装 IronOCR
复制并运行这段代码。
var result = new IronTesseract().ReadPassport(new OcrInput().LoadImage("passport.jpg"));部署到您的生产环境中进行测试
最小工作流程(5 个步骤)
- 下载一个用于读取车牌、护照和照片的 C# 库
- 为OCR准备图像和PDF文档
- 设置
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)
}常见问题解答
如何使用 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 方法目前仅支持英语文件。
使用 IronOCR 的高级扫描功能需要什么?
要使用 IronOCR 的高级扫描功能,需要 IronOcr.Extensions.AdvancedScan 包,该包仅在 Windows 上可用。






