开始使用 C# 和 VB.NET 进行 OCR
IronOCR 是一个 C# 软件库,允许 .NET 平台软件开发人员从图像和 PDF 文档中识别和读取文本。 这是一个纯粹的 .NET OCR 库,使用了目前已知的最先进的 Tesseract 引擎。
安装
使用 NuGet 包管理器进行安装
在 Visual Studio 中安装 IronOcr,或使用 NuGet 软件包管理器在命令行中安装。 在 Visual Studio 中,用以下命令导航到控制台:
- 工具 ->
- NuGet 包管理器 ->
- 软件包管理器控制台
Install-Package IronOcr
并查看 IronOCR on NuGet 了解有关版本更新和安装的更多信息。
还有其他适用于不同平台的 IronOCR NuGet 包:
- 窗口:https://www.nuget.org/packages/IronOcr IronOcr
- Linux:https://www.nuget.org/packages/IronOcr.Linux IronOcr
- MacOS:https://www.nuget.org/packages/IronOcr.MacOs IronOcr
- MacOS (ARM):https://www.nuget.org/packages/IronOcr.MacOs.ARM IronOcr
IronOcr.Extensions.AdvancedScan(适用于 Linux 和 macOS)
本软件包适用于使用 Linux 和 Mac 的用户,他们也希望获得 IronOcr 的高级功能。
- Linux: https://www.nuget.org/packages/IronOcr.Extensions.AdvancedScan.Linux
- MacOS: https://www.nuget.org/packages/IronOcr.Extensions.AdvancedScan.MacOs
故障排除
在此软件包的新版本更新中,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.csstring Text = new IronTesseract().Read(@"img\Screenshot.png").Text;可配置的 Hello World.
:path=/static-assets/ocr/content-code-examples/get-started/get-started-2.csusing 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);C# PDF OCR
相同的方法也可以用来从任何PDF文档中提取文本。
:path=/static-assets/ocr/content-code-examples/get-started/get-started-3.csusing 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");用于多页 TIFF 的OCR
:path=/static-assets/ocr/content-code-examples/get-started/get-started-4.csusing 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);条形码和 QR.
IronOCR 的一个独特功能是它可以在扫描文本的同时读取文档中的条形码和二维码。 OcrResult.OcrBarcode 类的实例可为开发人员提供有关每个扫描条形码的详细信息。
:path=/static-assets/ocr/content-code-examples/get-started/get-started-5.csusing 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);
}对图像特定区域进行OCR
IronOCR 的所有扫描和阅读方法都能准确指定我们希望从页面的哪个或哪些部分读取文本。 当我们查看标准化表单时,这非常有用,可以节省大量时间并提高效率。
要使用裁剪区域,我们需要添加对System.Drawing的系统引用,以便我们可以使用System.Drawing.Rectangle对象。
:path=/static-assets/ocr/content-code-examples/get-started/get-started-6.csusing 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);用于低质量扫描的OCR
IronOCR OcrInput类可以修复正常的Tesseract无法读取的扫描件。
:path=/static-assets/ocr/content-code-examples/get-started/get-started-7.csusing 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);将 OCR 结果导出为可搜索的 PDF 文件
:path=/static-assets/ocr/content-code-examples/get-started/get-started-8.csusing 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");TIFF转可搜索PDF转换
:path=/static-assets/ocr/content-code-examples/get-started/get-started-9.csusing 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");将 OCR 结果导出为 HTML.
:path=/static-assets/ocr/content-code-examples/get-started/get-started-10.csusing 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");OCR 图像增强过滤器
IronOCR 为 OcrInput 对象提供独特的过滤器,以提高 OCR 性能。
图像增强代码示例
:path=/static-assets/ocr/content-code-examples/get-started/get-started-11.csusing 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);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");多语言示例
还可以同时使用多种语言进行 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");详细的 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性能
IronOCR 开箱即用,无需调整性能或修改图像。
速度惊人:IronOcr.2020+快了多达10倍,并比以前的版本错误减少了超过250%。
了解更多
要了解有关 C#、VB、F# 或任何其他 .NET 语言中 OCR 的更多信息,请阅读我们的社区教程,其中给出了使用 IronOCR 的实际示例,并展示了优化库的细微差别。
完整的.NET开发人员API参考也可用。







