开始使用 C# 和 VB.NET 进行 OCR

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

IronOCR 是一个 C# 软件库,允许 .NET 平台软件开发人员从图像和 PDF 文档中识别和读取文本。 这是一个纯粹的 .NET OCR 库,使用了目前已知的最先进的 Tesseract 引擎。

安装

使用 NuGet 包管理器进行安装

在 Visual Studio 中安装 IronOcr,或使用 NuGet 软件包管理器在命令行中安装。 在 Visual Studio 中,用以下命令导航到控制台:

  • 工具 ->
  • NuGet 包管理器 ->
  • 软件包管理器控制台
Install-Package IronOcr

并查看 IronOCR on NuGet 了解有关版本更新和安装的更多信息。

还有其他适用于不同平台的 IronOCR NuGet 包

IronOcr.Extensions.AdvancedScan(适用于 Linux 和 macOS)

本软件包适用于使用 Linux 和 Mac 的用户,他们也希望获得 IronOcr 的高级功能。

故障排除

在此软件包的新版本更新中,IronOCR 将 OpenCV 依赖项合并到软件包内,以简化软件包,因此,如果当前导入 OpenCV 依赖项的开发人员会出现以下错误。

The type of namespace name `OpenCvSharp` could not be found(are you missing a using directive or an assembly reference)

您可以放心地删除 OpenCV 命名空间,问题就会得到解决。

下载 IronOCR .ZIP 文件

您也可以选择通过.ZIP文件下载IronOCR。 点击直接下载 DLL。 下载 .zip 文件后,您可以

.NET Framework 4.0+ 安装说明:

  • 将 IronOcr.dll 加入 net40 文件夹
  • 然后添加装配参考:

    • 系统配置
    • 系统绘图
    • 系统网络

.NET Standard 和 .NET Core 2.0+ 以及 .NET 5 的说明

  • 将 netstandard2.0 文件夹中的 IronOcr.dll 包含到您的项目中
  • 然后添加一个 NuGet 包引用到:

    • 系统绘图.Common 4.7 或更高版本

下载IronOCR安装程序(仅限Windows)

另一种方法是下载我们的 IronOCR 安装程序,该程序将安装 IronOCR 开箱即用所需的所有资源。 请注意,此选项仅适用于 Windows 系统。 要下载安装程序,请 点击这里。 下载 .zip 文件后,您可以

.NET Framework 4.0+ 安装说明:

  • 将 IronOcr.dll 加入 net40 文件夹
  • 然后添加装配参考:

    • 系统配置
    • 系统绘图
    • 系统网络

.NET Standard 和 .NET Core 2.0+ 以及 .NET 5 的说明

  • 将 netstandard2.0 文件夹中的 IronOcr.dll 包含到您的项目中
  • 然后添加一个 NuGet 包引用到:

    • 系统绘图.Common 4.7 或更高版本

为什么选择 IronOCR?

IronOCR for .NET 是一个易于安装、功能完备且文档齐全的 .NET 软件库。

选择 IronOCR 可实现 99.8%+ OCR 精确度,而无需使用任何外部网络服务、持续收费或通过互联网发送机密文件。

为什么 C# 开发人员选择 IronOCR 而不是 Vanilla Tesseract:

  • 以单个 DLL 或 NuGet 的形式安装
  • 包括开箱即用的 Tesseract 5、4 和 3 引擎。
  • 准确度99.8%明显优于普通 Tesseract。
  • 超快速度和多线程
  • 兼容 MVC、WebApp、桌面、控制台和服务器应用程序
  • 无需使用 Exes 或 C# 代码
  • 完全支持 PDF OCR
  • 在几乎所有图像文件或 PDF 上执行 OCR
  • 完全支持 .NET Core、Standard 和 Framework
  • 在 Windows、Mac、Linux、Azure、Docker、Lambda、AWS 上部署
  • 读取条形码和二维码
  • 将 OCR 结果导出为 XHTML
  • 将 OCR 输出为可搜索的 PDF 文档
  • 多线程支持
  • 125 种国际语言,全部通过 NuGet 或 OcrData 文件管理
  • 提取图像、坐标、统计数据和字体。 不仅仅是文字。
  • 可用于在商业和专有应用程序中重新发布 Tesseract OCR。

在处理真实世界的图像和不完美的文档(如照片或可能存在数字噪声或瑕疵的低分辨率扫描件)时,_IronOCR 可大显身手。

其他.NET平台的免费OCR库,如其他.NET Tesseract API和网络服务在这些实际使用案例中的表现并不理想。

使用 Tesseract 5 的OCR - 用 C# 开始编码;

下面的代码示例显示了使用 C# 或 VB .NET 从图像中读取文本是多么容易。

一行代码

:path=/static-assets/ocr/content-code-examples/get-started/get-started-1.cs
string Text = new IronTesseract().Read(@"img\Screenshot.png").Text;
Dim Text As String = (New IronTesseract()).Read("img\Screenshot.png").Text
$vbLabelText   $csharpLabel

可配置的 Hello World.

:path=/static-assets/ocr/content-code-examples/get-started/get-started-2.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// Add multiple images
input.LoadImage("images/sample.jpeg");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using

' Add multiple images
input.LoadImage("images/sample.jpeg")

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

C# PDF OCR

相同的方法也可以用来从任何PDF文档中提取文本。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-3.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// We can also select specific PDF page numbers to OCR
input.LoadPdf("example.pdf", Password: "password");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);

// 1 page for every page of the PDF
Console.WriteLine($"{result.Pages.Length} Pages");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using

' We can also select specific PDF page numbers to OCR
input.LoadPdf("example.pdf", Password:= "password")

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)

' 1 page for every page of the PDF
Console.WriteLine($"{result.Pages.Length} Pages")
$vbLabelText   $csharpLabel

用于多页 TIFF 的OCR

:path=/static-assets/ocr/content-code-examples/get-started/get-started-4.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("multi-frame.tiff", pageindices);
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("multi-frame.tiff", pageindices)
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

条形码和 QR.

IronOCR 的一个独特功能是它可以在扫描文本的同时读取文档中的条形码和二维码。 OcrResult.OcrBarcode 类的实例可为开发人员提供有关每个扫描条形码的详细信息。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-5.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
ocr.Configuration.ReadBarCodes = true;

using OcrInput input = new OcrInput();
input.LoadImage("img/Barcode.png");

OcrResult Result = ocr.Read(input);
foreach (var Barcode in Result.Barcodes)
{
    // type and location properties also exposed
    Console.WriteLine(Barcode.Value);
}
Imports IronOcr

Private ocr As New IronTesseract()
ocr.Configuration.ReadBarCodes = True

Using input As New OcrInput()
	input.LoadImage("img/Barcode.png")
	
	Dim Result As OcrResult = ocr.Read(input)
	For Each Barcode In Result.Barcodes
		' type and location properties also exposed
		Console.WriteLine(Barcode.Value)
	Next Barcode
End Using
$vbLabelText   $csharpLabel

对图像特定区域进行OCR

IronOCR 的所有扫描和阅读方法都能准确指定我们希望从页面的哪个或哪些部分读取文本。 当我们查看标准化表单时,这非常有用,可以节省大量时间并提高效率。

要使用裁剪区域,我们需要添加对System.Drawing的系统引用,以便我们可以使用System.Drawing.Rectangle对象。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-6.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// Dimensions are in pixel
var contentArea = new System.Drawing.Rectangle() { X = 215, Y = 1250, Height = 280, Width = 1335 };

input.LoadImage("document.png", contentArea);

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using

' Dimensions are in pixel
Private contentArea = New System.Drawing.Rectangle() With {
	.X = 215,
	.Y = 1250,
	.Height = 280,
	.Width = 1335
}

input.LoadImage("document.png", contentArea)

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

用于低质量扫描的OCR

IronOCR OcrInput类可以修复正常的Tesseract无法读取的扫描件。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-7.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\Potter.tiff", pageindices);

// fixes digital noise and poor scanning
input.DeNoise();

// fixes rotation and perspective
input.Deskew();

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("img\Potter.tiff", pageindices)

' fixes digital noise and poor scanning
input.DeNoise()

' fixes rotation and perspective
input.Deskew()

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

将 OCR 结果导出为可搜索的 PDF 文件

:path=/static-assets/ocr/content-code-examples/get-started/get-started-8.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Quarterly Report";
input.LoadImage("image1.jpeg");
input.LoadImage("image2.png");
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("image3.gif", pageindices);

OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable.pdf");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Quarterly Report"
input.LoadImage("image1.jpeg")
input.LoadImage("image2.png")
Dim pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("image3.gif", pageindices)

Dim result As OcrResult = ocr.Read(input)
result.SaveAsSearchablePdf("searchable.pdf")
$vbLabelText   $csharpLabel

TIFF转可搜索PDF转换

:path=/static-assets/ocr/content-code-examples/get-started/get-started-9.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("example.tiff", pageindices);
ocr.Read(input).SaveAsSearchablePdf("searchable.pdf");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("example.tiff", pageindices)
ocr.Read(input).SaveAsSearchablePdf("searchable.pdf")
$vbLabelText   $csharpLabel

将 OCR 结果导出为 HTML.

:path=/static-assets/ocr/content-code-examples/get-started/get-started-10.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Html Title";
input.LoadImage("image1.jpeg");

OcrResult Result = ocr.Read(input);
Result.SaveAsHocrFile("results.html");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Html Title"
input.LoadImage("image1.jpeg")

Dim Result As OcrResult = ocr.Read(input)
Result.SaveAsHocrFile("results.html")
$vbLabelText   $csharpLabel

OCR 图像增强过滤器

IronOCR 为 OcrInput 对象提供独特的过滤器,以提高 OCR 性能。

图像增强代码示例

:path=/static-assets/ocr/content-code-examples/get-started/get-started-11.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.LoadImage("LowQuality.jpeg");

// fixes digital noise and poor scanning
input.DeNoise();

// fixes rotation and perspective
input.Deskew();

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
input.LoadImage("LowQuality.jpeg")

' fixes digital noise and poor scanning
input.DeNoise()

' fixes rotation and perspective
input.Deskew()

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

OCR 图像过滤器列表

IronOCR 内置的用于提高 OCR 性能的输入过滤器包括

  • OcrInput.Rotate(double degrees) - 顺时针旋转图像若干度。对于逆时针旋转,请使用负数。
  • OcrInput.Binarize() - 此过滤器将每个像素转换为黑色或白色,没有中间状态,可能改善非常低对比度图像中的OCR性能。
  • OcrInput.ToGrayScale() - 将每个像素转换为灰度。 这可能不会提高准确性,但可以提高速度。
  • OcrInput.Contrast() - 自动增加对比度,通常可提高低对比度扫描的速度和准确性。
  • OcrInput.DeNoise()--去除数字噪声,仅在预期会产生噪声时推荐使用。
  • OcrInput.Invert() - 反转每种颜色(白色变为黑色,反之亦然)。
  • OcrInput.Dilate()--推进形态学,为对象边界添加像素,与 Erode 相反。
  • OcrInput.Erode()--推进形态学,从对象边界移除像素,与 Dilate 相反。
  • OcrInput.Deskew() - 旋转图像使其方向正确。 这很有用,因为Tesseract的偏斜容忍度是有限的。
  • OcrInput.EnhanceResolution - 增强低质量图像的分辨率。 该设置通常用于自动管理低 DPI 输入。
  • EnhanceResolution可检测低分辨率图像(低于 275 dpi),提高图像分辨率并锐化文本以获得更好的 OCR 结果。 虽然耗时,但通常可以缩短整个 OCR 操作时间。
  • Language - 支持从 22 种国际语言包中进行选择。
  • Strategy - 允许根据单词的统计关系在快速、不太准确或高级(使用人工智能提高准确性)策略之间进行选择。
  • ColorSpace - 选择灰度或彩色 OCR; 通常,灰度最佳,但某些对比度情况下彩色更优.
  • DetectWhiteTextOnDarkBackgrounds - 调整负图像,自动检测和读取深色背景上的白色文本。
  • InputImageType - 引导 OCR 库,指定它是处理完整文档还是处理片段。
  • RotateAndStraighten - 允许 IronOCR 正确处理旋转或受透视扭曲影响的文档。
  • ReadBarcodes - 在文本扫描的同时自动读取条形码和 QR 码,无需大量增加时间。
  • ColorDepth - 确定 OCR 过程中颜色深度的每像素位数。 更高的深度可以提高质量,但也会增加处理时间。

125 种语言包

IronOCR 通过语言包支持 125 种国际语言,语言包以 DLL 形式发布,可从本网站下载,或从 NuGet 包管理器下载。

语言包括德语、法语、英语、中文、日语等。 专业语言包适用于 MRZ、MICR 支票、金融数据、车牌等。此外,还可以使用自定义的 tesseract".traineddata "文件。

语言示例

// Reference to the path of the source file that demonstrates setting language packs for OCR
:path=/static-assets/ocr/content-code-examples/get-started/get-started-12.cs
// Reference to the path of the source file that demonstrates setting language packs for OCR
using IronOcr;

// PM> Install IronOcr.Languages.Arabic
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.Arabic;

using OcrInput input = new OcrInput();

var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("img/arabic.gif", pageindices);
// Add image filters if needed
// In this case, even thought input is very low quality
// IronTesseract can read what conventional Tesseract cannot.
OcrResult result = ocr.Read(input);
// Console can't print Arabic on Windows easily.
// Let's save to disk instead.
result.SaveAsTextFile("arabic.txt");
' Reference to the path of the source file that demonstrates setting language packs for OCR
Imports IronOcr

' PM> Install IronOcr.Languages.Arabic
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.Arabic

Using input As New OcrInput()
	
	Dim pageindices = New Integer() { 1, 2 }
	input.LoadImageFrames("img/arabic.gif", pageindices)
	' Add image filters if needed
	' In this case, even thought input is very low quality
	' IronTesseract can read what conventional Tesseract cannot.
	Dim result As OcrResult = ocr.Read(input)
	' Console can't print Arabic on Windows easily.
	' Let's save to disk instead.
	result.SaveAsTextFile("arabic.txt")
End Using
$vbLabelText   $csharpLabel

多语言示例

还可以同时使用多种语言进行 OCR。这可以增强对 Unicode 文档中的英文元数据和 URL 的 OCR 识别。

// Reference to the path of the source file that demonstrates multi-language OCR
:path=/static-assets/ocr/content-code-examples/get-started/get-started-13.cs
// Reference to the path of the source file that demonstrates multi-language OCR
using IronOcr;

// PM> Install IronOcr.Languages.ChineseSimplified
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.ChineseSimplified;

// We can add any number of languages
ocr.AddSecondaryLanguage(OcrLanguage.English);

using OcrInput input = new OcrInput();
input.LoadPdf("multi-language.pdf");
OcrResult result = ocr.Read(input);
result.SaveAsTextFile("results.txt");
' Reference to the path of the source file that demonstrates multi-language OCR
Imports IronOcr

' PM> Install IronOcr.Languages.ChineseSimplified
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.ChineseSimplified

' We can add any number of languages
ocr.AddSecondaryLanguage(OcrLanguage.English)

Using input As New OcrInput()
	input.LoadPdf("multi-language.pdf")
	Dim result As OcrResult = ocr.Read(input)
	result.SaveAsTextFile("results.txt")
End Using
$vbLabelText   $csharpLabel

详细的 OCR 结果对象

IronOCR 会为每次操作返回一个 OCR 结果对象。 一般来说,开发人员会访问 Text 属性来获取扫描文本。 不过,结果对象包含的信息要详细得多。

// Reference to the path of the source file demonstrating detailed OCR result object usage
:path=/static-assets/ocr/content-code-examples/get-started/get-started-14.cs
// Reference to the path of the source file demonstrating detailed OCR result object usage
using IronOcr;

IronTesseract ocr = new IronTesseract();

// Must be set to true to read barcode
ocr.Configuration.ReadBarCodes = true;
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\sample.tiff", pageindices);

OcrResult result = ocr.Read(input);
var pages = result.Pages;
var words = pages[0].Words;
var barcodes = result.Barcodes;
// Explore here to find a massive, detailed API:
// - Pages, Blocks, Paraphaphs, Lines, Words, Chars
// - Image Export, Fonts Coordinates, Statistical Data, Tables
' Reference to the path of the source file demonstrating detailed OCR result object usage
Imports IronOcr

Private ocr As New IronTesseract()

' Must be set to true to read barcode
ocr.Configuration.ReadBarCodes = True
Using input As New OcrInput()
	Dim pageindices = New Integer() { 1, 2 }
	input.LoadImageFrames("img\sample.tiff", pageindices)
	
	Dim result As OcrResult = ocr.Read(input)
	Dim pages = result.Pages
	Dim words = pages(0).Words
	Dim barcodes = result.Barcodes
	' Explore here to find a massive, detailed API:
	' - Pages, Blocks, Paraphaphs, Lines, Words, Chars
	' - Image Export, Fonts Coordinates, Statistical Data, Tables
End Using
$vbLabelText   $csharpLabel

性能

IronOCR 开箱即用,无需调整性能或修改图像。

速度惊人:IronOcr.2020+快了多达10倍,并比以前的版本错误减少了超过250%。

了解更多

要了解有关 C#、VB、F# 或任何其他 .NET 语言中 OCR 的更多信息,请阅读我们的社区教程,其中给出了使用 IronOCR 的实际示例,并展示了优化库的细微差别。

完整的.NET开发人员API参考也可用。

Curtis Chau
技术作家

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

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

审核者
Jeff Fritz
Jeffrey T. Fritz
首席项目经理 - .NET 社区团队
Jeff 也是 .NET 和 Visual Studio 团队的首席项目经理。他是 .NET Conf 虚拟会议系列的执行制片人,并主持“Fritz and Friends”直播节目,每周两次与观众一起谈论技术并编写代码。Jeff 撰写研讨会、演示文稿并计划包括 Microsoft Build、Microsoft Ignite、.NET Conf 和 Microsoft MVP 峰会在内的最大型微软开发者活动的内容。
准备开始了吗?
Nuget 下载 5,167,857 | Version: 2025.11 刚刚发布