如何使用 OCR 在 C# 中读取特定的文件

如何使用 C# 和 IronOCR 读取专业文档

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

用单一的通用方法准确解读标准文本文件、车牌、护照和照片等特定文件是非常困难的。 这些挑战源于每种文档类型的格式、布局和内容的多样性,以及图像质量、失真和专业内容的差异。 此外,随着文档类型范围的扩大,实现上下文理解以及平衡性能和效率变得更加复杂。

IronOCR 引入了针对特定文档(例如标准文本文件、车牌、护照和照片)执行 OCR 的具体方法,以实现最佳的准确性和性能。

快速入门:一行读懂护照

使用 IronOCR 的ReadPassport扩展程序,只需极少的设置即可提取所有关键护照信息。只需一行代码(假设您已安装 IronOCR 和 AdvancedScan),即可快速轻松地获得结构化的结果数据,例如姓名、护照号码、国家/地区等。

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 IronOCR

    PM > Install-Package IronOcr

  2. 复制并运行这段代码。

    var result = new IronTesseract().ReadPassport(new OcrInput().LoadImage("passport.jpg"));
  3. 部署到您的生产环境中进行测试

    立即开始在您的项目中使用 IronOCR,免费试用!
    arrow pointer


关于包裹

ReadLicensePlateReadPassportReadPhotoReadScreenShot方法都是 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

阅读护照示例

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

结果

读取护照

请确保文件中仅包含护照照片。 任何页眉和页脚文本都可能干扰该方法,并导致意外的输出。

查看照片示例

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

常见问题解答

如何使用 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 上可用。

Curtis Chau
技术作家

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

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 5,167,857 | Version: 2025.11 刚刚发布