使用 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包管理器安装

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

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

并查看NuGet 上的 IronOcr有关版本更新和安装的更多信息。

其他平台也有可用的 IronOCR NuGet 包

下载 IronOCR .ZIP

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

.NET Framework 4.0+ 安装说明:

  • 将 net40 文件夹中的 IronOcr.dll 包含到您的项目中。
  • 然后添加程序集引用到:
  • System.Configuration
  • System.Drawing
  • System.Web

.NET Standard与.NET Core 2.0+和.NET 5的说明

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

    * System.Drawing.Common 4.7 或更高版本

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

另一个选择是下载我们的IronOCR安装程序,它会安装IronOCR正常工作所需的所有资源。 请记住,此选项仅适用于Windows系统。 要下载安装程序,请 点击这里. 下载.zip文件后:

.NET Framework 4.0+ 安装说明:

  • 将 net40 文件夹中的 IronOcr.dll 包含到您的项目中。
  • 然后添加程序集引用到:
  • System.Configuration
  • System.Drawing
  • System.Web

.NET Standard与.NET Core 2.0+和.NET 5的说明

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

    * System.Drawing.Common 4.7 或更高版本

为什么选择IronOCR?

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

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

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

  • 安装为单个 DLL 或 NuGet
  • 包括开箱即用的Tesseract 5、4和3引擎。
  • 精确度99.8%显著优于常规的Tesseract。
  • 极速与多线程处理
  • MVC、WebApp、桌面、控制台和服务器应用程序兼容
  • 无需 Exe 文件或 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 在处理真实世界的图像和不完美文件(如照片或低分辨率扫描件,可能包含数字噪声或缺陷)时表现出色。

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

    使用 Tesseract 5 进行 OCR - 在 C# 中开始编程

    以下代码示例显示了使用 C# 或 VB .NET 从图像中读取文本的简便性。

OneLiner

: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
VB   C#

可配置的 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)
VB   C#

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")
VB   C#

多页 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)
VB   C#

条形码和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
VB   C#

图像特定区域的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)
VB   C#

低质量扫描的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)
VB   C#

将 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")
VB   C#

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")
VB   C#

将 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")
VB   C#

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)
VB   C#

OCR 图像过滤器列表

用于提高IronOCR性能的输入过滤器包括:

  • OcrInput.Rotate(双学位)** - 将图像顺时针旋转若干度。要逆时针旋转,请使用负数。
  • OcrInput.Binarize()** - 这个图像过滤器将每个像素变成黑色或白色,没有中间色。 可能提高OCR性能,用于文本与背景对比度非常低的情况。
  • OcrInput.ToGrayScale()** - 此图像过滤器将每个像素转换为灰度阴影。 不太可能提高OCR精度,但可以提高速度。
  • OcrInput.对比度()** - 自动增加对比度。 此过滤器通常能提高低对比度扫描中的OCR速度和准确性。
  • OcrInput.DeNoise()** - 去除数字噪声。此过滤器应仅在预期有噪声的地方使用。
  • OcrInput.Invert()** - 反转每个颜色。 例如,白变黑:黑变白。
  • OcrInput.膨胀()* - 高级形态学。 膨胀*会在图像中的物体边界处添加像素。 侵蚀的反义词
  • OcrInput.Erode()* - 高级形态学。 侵蚀* 移除物体边界上的像素,与膨胀相反
  • OcrInput.Deskew()** - 旋转图像,使其正向上并保持正交。 这对OCR非常有用,因为Tesseract对倾斜扫描的容忍度可以低至5度。
  • OcrInput.EnhanceResolution - 增强低质量图像的分辨率。 此过滤器通常不是必需的,因为 OcrInput.MinimumDPIOcrInput.TargetDPI 将自动捕捉并解决低分辨率输入。
  • EnhanceResolution 是一个设置,它会自动检测低分辨率图像(低于 275 dpi)并自动升级图像,然后对所有文本进行锐化处理,以便OCR库可以完美识别。 尽管这个操作本身是耗时的,但它通常会缩短对图像进行OCR操作的总时间。
  • 语言 IronOCR支持22种国际语言包,语言设置可以用来选择一个或多个语言,以供OCR操作使用。
  • 策略 IronOCR 支持两种策略。 我们可以选择进行快速且不太准确的文档扫描,或者采用高级策略,使用一些人工智能模型通过观察句子中单词之间的统计关系来自动提高OCR文本的准确性。
  • ColorSpace 是一个设置,我们可以选择以灰度或彩色进行OCR。 通常,灰度是最佳选择。 然而,有时当文本或背景具有相似的色调但非常不同的颜色时,全彩色空间将提供更好的结果。
  • DetectWhiteTextOnDarkBackgrounds. 通常,所有OCR库都预期在白色背景上看到黑色文本。 此设置允许IronOCR自动检测负片或带有白色文字的深色页面,并进行识别。
  • InputImageType. 此设置允许开发者指导OCR库是在查看完整文档还是片段,例如屏幕截图。
  • RotateAndStraighten 是一个高级设置,它赋予 IronOCR 独特的能力,可以识别不仅被旋转,而且可能包含透视的文档,例如文本文件的照片。
  • ReadBarcodes 是一个有用的功能,它允许 IronOCR 在读取文本的同时自动识别页面上的条形码和二维码,而不会增加太多额外的时间负担。
  • ColorDepth. 此设置决定了OCR库将使用多少位每像素来确定颜色的深度。 更高的色深可能会提高OCR的质量,但也会延长OCR操作的完成时间。

125 语言包

IronOCR支持通过作为DLL分发的语言包支持125种国际语言,可以从本网站下载或也可从NuGet 软件包管理器.

语言包括德语、法语、英语、中文、日语等等。 专门的语言包用于护照MRZ、MICR支票、金融数据、车牌等多种用途。 您也可以使用任何 tesseract ".traineddata" 文件 - 包括您自己创建的文件。

语言示例

:path=/static-assets/ocr/content-code-examples/get-started/get-started-12.cs
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");
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
VB   C#

多种语言示例

也可以同时使用多种语言进行光学字符识别。这真的可以帮助获取Unicode文档中的英语元数据和URL。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-13.cs
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");
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
VB   C#

详细的OCR结果对象

IronOCR为每个OCR操作返回一个OCR结果对象。 通常,开发者只使用此对象的文本属性来获取从图像中扫描的文本。 然而,OCR结果的DOM比这更高级。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-14.cs
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
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
VB   C#

性能

IronOCR 无需进行性能调优或大幅修改输入图像即可开箱即用。

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

了解更多

要了解更多关于在C#、VB、F#或任何其他.NET语言中的OCR的信息,请阅读我们的社区教程,其中提供了IronOCR实际应用的真实示例,并可能展示如何充分利用这个库的细微差别。

A full.NET开发人员的应用程序接口参考也可以使用。