C# OCR 图像转文本教程:无需 Tesseract 即可将图像转换为文本
想在 C# 中将图像转换为文本,而无需进行复杂的 Tesseract 配置? 本综合 IronOCR for .NET C# 教程向您展示如何在您的 .NET 应用程序中实现强大的光学字符识别功能,只需几行代码。
快速入门:一行代码从图片中提取文本此示例展示了掌握IronOCR的容易程度——只需一行C#代码即可将您的图像转换为文本。 它演示了如何初始化 OCR 引擎并立即读取和检索文本,而无需进行复杂的设置。
-
1使用 IronOCR 安装 NuGet Package Manager
-
2复制并运行这段代码。
string text = new IronTesseract().Read("image.png").Text;Dim text As String = New IronTesseract().Read("image.png").TextDim text As String = New IronTesseract().Read("image.png").Text -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronOCR
最小工作流程(5 个步骤)
- 下载IronOCR - 适用于图像到文本转换的C# OCR库
- 使用
IronTesseract类可以立即从图像中读取文本。 - 应用图像过滤器以提高低质量扫描的 OCR 准确性
- 使用可下载的语言包处理多种语言
- 将结果导出为可搜索的 PDF 或提取文本字符串
如何在.NET应用程序中从图像读取文本?
要在您的 .NET 应用程序中实现 C# OCR 图像转文本功能,您需要一个可靠的 OCR 库。 IronOCR提供了一种托管解决方案,使用IronOcr.IronTesseract类最大限度地提高准确性和速度,而无需外部依赖。
首先,将 IronOCR 安装到 Visual Studio 项目中。 您可以直接下载IronOCR DLL或使用NuGet包管理器。
为何选择不依赖Tesseract的IronOCR用于C# OCR?
当您需要用 C# 将图像转换为文本时,IronOCR 与传统的 Tesseract 实现相比具有显著优势:
- 可在纯 .NET 环境中立即使用
- 无需安装或配置 Tesseract
- 运行最新的引擎:Tesseract 5(加上 Tesseract 4 和 3)
- 兼容 .NET Framework 4.6.2 及以上版本、.NET Standard 2 及以上版本,以及 .NET Core 2、3、5、6、7、8、9 和 10
- 与原版 Tesseract 相比,提高了准确性和速度
- 支持 Xamarin、Mono、Azure 和 Docker 部署
- 通过 NuGet 包管理复杂的 Tesseract 词典
- 自动处理 PDF、多帧 TIFF 和所有主要图像格式
- 纠正低质量和倾斜的扫描,以获得最佳效果
如何使用IronOCR C#教程进行基本OCR?
这个 Iron Tesseract C# 示例演示了使用 IronOCR 从图像读取文本的最简单方法。 IronOcr.IronTesseract类提取文本并将其作为字符串返回。
// PM> Install-Package IronOcr
using IronOcr;
OcrResult result = new IronTesseract().Read(@"img\Screenshot.png");
Console.WriteLine(result.Text);// PM> Install-Package IronOcr
using IronOcr;
OcrResult result = new IronTesseract().Read(@"img\Screenshot.png");
Console.WriteLine(result.Text);' PM> Install-Package IronOcr
Imports IronOcr
Private result As OcrResult = (New IronTesseract()).Read("img\Screenshot.png")
Console.WriteLine(result.Text)' PM> Install-Package IronOcr
Imports IronOcr
Private result As OcrResult = (New IronTesseract()).Read("img\Screenshot.png")
Console.WriteLine(result.Text)这段代码从高质量图像中提取纯文本,就像它出现的那样:
IronOCR Simple Example
In this simple example we test the accuracy of our C# OCR library to read text from a PNG Image. This is a very basic test, but things will get more complicated as the tutorial continues.
The quick brown fox jumps over the lazy dog
IronTesseract类在内部处理复杂的OCR操作。 它会自动扫描对齐,优化分辨率,并使用AI通过IronOCR读取图像中的文本。
尽管后台进行复杂的处理——包括图像分析、引擎优化和智能文本识别——但OCR过程仍保持高效。
截图展示IronOCR从PNG图像中提取文本的能力
如何在不使用Tesseract配置的情况下实现高级C# OCR?
对于需要最佳性能的生产应用程序,当您在C#中将图像转换为文本时,请一起使用OcrInput和IronTesseract类。 这种方法提供了对 OCR 过程的细粒度控制。
OcrInput类特性
- 处理多种图像格式:JPEG、TIFF、GIF、BMP、PNG
- 导入完整的 PDF 或特定页面
- 自动增强对比度、分辨率和图像质量
- 纠正旋转、扫描噪声、倾斜和负像
IronTesseract类特性
- 支持 127 种以上预置语言
- 包括 Tesseract 5、4 和 3 引擎
- 文档类型说明(截图、片段或完整文档)
- 集成条形码读取功能
- 多种输出格式:可搜索的 PDF、HOCR HTML、DOM 对象和字符串
如何开始使用OcrInput和IronTesseract?
下面是本 IronOCR C# 教程的推荐配置,可以很好地适用于大多数文档类型:
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\Potter.tiff", pageindices);
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\Potter.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("img\Potter.tiff", pageindices)
Dim result As OcrResult = 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)
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)该配置在中等质量的扫描件上始终保持近乎完美的准确性。 LoadImageFrames方法有效处理多页文档,使其成为批处理场景的理想选择。
展示 IronOCR 多页文本提取功能的 TIFF 文档示例
IronOCR 能够从 TIFF 等扫描文档中的图像和条形码读取文本,这充分展现了它如何简化复杂的 OCR 任务。 该库在处理真实文档时表现出色,可以处理多页TIFF和PDF文本提取。
IronOCR如何处理低质量扫描?
IronOCR 可以使用图像滤波器准确处理的带有噪点的低分辨率文档
在处理包含失真和数字噪声的不完美扫描时, IronOCR 的性能优于其他 C# OCR 库。 它专为真实场景而设计,而非为完美的测试图像而设计。
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\Potter.LowQuality.tiff", pageindices);
input.Deskew(); // removes rotation and perspective
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\Potter.LowQuality.tiff", pageindices);
input.Deskew(); // removes rotation and perspective
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.LowQuality.tiff", pageindices)
input.Deskew() ' removes rotation and perspective
Dim result As OcrResult = 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.LowQuality.tiff", pageindices)
input.Deskew() ' removes rotation and perspective
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)使用Input.Deskew(),可以显著提高低质量扫描的准确性,几乎与高质量结果相匹配。 这就说明了为什么 IronOCR 是 C# OCR 的首选,而不会带来 Tesseract 的复杂性。
图像过滤器可能会略微增加处理时间,但会大大缩短整个 OCR 的持续时间。 找到正确的平衡点取决于您的文档质量。
对于大多数场景,Input.Deskew()和Input.DeNoise()提供可靠的OCR性能改进。 了解有关图像预处理技术的更多信息。
如何优化OCR性能和速度?
在 C# 中将图像转换为文本时,影响 OCR 速度的最主要因素是输入质量。 较高的 DPI(约 200 dpi)和最小的噪点可以产生最快、最准确的结果。
虽然 IronOCR 擅长纠正不完善的文档,但这种增强功能需要额外的处理时间。
选择压缩痕迹最小的图像格式。 由于数字噪声较低,TIFF 和 PNG 通常比 JPEG 能产生更快的效果。
哪些图像滤镜可以提高OCR速度?
以下过滤器可显著提高 C# OCR 图像到文本工作流程的性能:
OcrInput.Rotate(double degrees): 顺时针旋转图像(负数用于逆时针)OcrInput.Binarize(): 转换为黑/白,改善低对比度场景下的性能OcrInput.ToGrayScale(): 转换为灰度以潜在提高速度OcrInput.Contrast(): 自动调整对比度以提高准确性OcrInput.DeNoise(): 当预期噪声存在时去除数字伪影OcrInput.Invert(): 反转白底黑字的颜色OcrInput.Dilate(): 扩展文本边界OcrInput.Erode(): 缩小文本边界OcrInput.Deskew(): 校正对齐 - 对于倾斜的文档至关重要OcrInput.Despeckle(): 激进的噪声去除OcrInput.EnhanceResolution: 改善低分辨率图像质量OcrInput.DetectPageOrientation(): 检测并纠正页面旋转。 传递一个OrientationDetectionMode以控制精度/速度权衡:Fast、Balanced、Detailed或ExtremeDetailed(添加于v2025.8.6)
Scale()和EnhanceResolution()与SaveAsSearchablePdf()不兼容,原因是v2025.12.3中的一个已知问题。所有其他过滤器可以与可搜索的PDF输出正确配合使用。如何配置IronOCR以实现最大速度?
在处理高质量扫描时,使用这些设置可优化速度:
using IronOcr;
IronTesseract ocr = new IronTesseract();
// Configure for speed
ocr.Configuration.BlackListCharacters = "~`$#^*_}{][|\\";
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto;
ocr.Language = OcrLanguage.EnglishFast;
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\Potter.tiff", pageindices);
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);using IronOcr;
IronTesseract ocr = new IronTesseract();
// Configure for speed
ocr.Configuration.BlackListCharacters = "~`$#^*_}{][|\\";
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto;
ocr.Language = OcrLanguage.EnglishFast;
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\Potter.tiff", pageindices);
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);Imports IronOcr
Private ocr As New IronTesseract()
' Configure for speed
ocr.Configuration.BlackListCharacters = "~`$#^*_}{][|\"
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto
ocr.Language = OcrLanguage.EnglishFast
Using input As New OcrInput()
Dim pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("img\Potter.tiff", pageindices)
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
End UsingImports IronOcr
Private ocr As New IronTesseract()
' Configure for speed
ocr.Configuration.BlackListCharacters = "~`$#^*_}{][|\"
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto
ocr.Language = OcrLanguage.EnglishFast
Using input As New OcrInput()
Dim pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("img\Potter.tiff", pageindices)
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
End Using这种优化设置在提高处理速度的同时保持较高的准确性,与默认设置相比。
如何使用C# OCR读取图像的特定区域?
下面的Iron Tesseract C#示例展示了如何使用System.Drawing.Rectangle定位特定区域。 这种技术对于处理文本出现在可预测位置的标准化表单非常有用。
IronOCR能否处理裁剪区域以获得更快的结果?
使用基于像素的坐标,您可以将 OCR 限制在特定区域,从而大幅提高速度并防止提取不必要的文本:
using IronOcr;
using IronSoftware.Drawing;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
// restrict OCR to a content area for faster processing
Rectangle contentArea = new Rectangle(x: 215, y: 1250, height: 280, width: 1335);
input.LoadImage("img/ComSci.png", contentArea);
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);using IronOcr;
using IronSoftware.Drawing;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
// restrict OCR to a content area for faster processing
Rectangle contentArea = new Rectangle(x: 215, y: 1250, height: 280, width: 1335);
input.LoadImage("img/ComSci.png", contentArea);
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);Imports IronOcr
Imports IronSoftware.Drawing
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' restrict OCR to a content area for faster processing
Dim contentArea As New Rectangle(x:=215, y:=1250, height:=280, width:=1335)
input.LoadImage("img/ComSci.png", contentArea)
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
End UsingImports IronOcr
Imports IronSoftware.Drawing
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' restrict OCR to a content area for faster processing
Dim contentArea As New Rectangle(x:=215, y:=1250, height:=280, width:=1335)
input.LoadImage("img/ComSci.png", contentArea)
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
End Using这种针对性的方法在提取相关文本的同时提高了处理速度。 这对于结构化文档(如发票、支票和表格)来说非常理想。 相同的裁剪技术也适用于PDF OCR操作。
文档展示使用IronOCR的矩形选择进行精确区域文本提取
IronOCR支持多少种语言?
IronOCR 通过便捷的语言包支持 127 种国际语言。 通过我们的网站或NuGet包管理器下载它们作为DLL。
通过NuGet接口安装语言包(搜索"IronOcr.Languages")或访问完整语言包列表。
支持的语言包括阿拉伯语、中文(简体/繁体)、日语、韩语、印地语、俄语、德语、法语、西班牙语和其他 115 多种语言,每种语言都经过优化,可实现准确的文本识别。
如何实现多语言OCR?
此 IronOCR C# 教程示例演示了阿拉伯语文本识别:

IronOCR 从 GIF 图像中准确提取阿拉伯语文本
// PM> Install IronOcr.Languages.Arabic
using IronOcr;
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.Arabic;
using OcrInput input = new OcrInput();
input.LoadImageFrame("img/arabic.gif", 1);
// 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");// PM> Install IronOcr.Languages.Arabic
using IronOcr;
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.Arabic;
using OcrInput input = new OcrInput();
input.LoadImageFrame("img/arabic.gif", 1);
// 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");' PM> Install IronOcr.Languages.Arabic
Imports IronOcr
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.Arabic
Using input As New OcrInput()
input.LoadImageFrame("img/arabic.gif", 1)
' 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' PM> Install IronOcr.Languages.Arabic
Imports IronOcr
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.Arabic
Using input As New OcrInput()
input.LoadImageFrame("img/arabic.gif", 1)
' 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 UsingIronOCR能够处理多语言文档吗?
当文档包含混合语言时,配置 IronOCR 以支持多语言:
using IronOcr;
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.ChineseSimplified;
// We can add any number of languages.
ocr.AddSecondaryLanguage(OcrLanguage.English);
// Optionally add custom tesseract .traineddata files by specifying a file path
using OcrInput input = new OcrInput();
input.LoadImage("img/MultiLanguage.jpeg");
OcrResult result = ocr.Read(input);
result.SaveAsTextFile("MultiLanguage.txt");using IronOcr;
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.ChineseSimplified;
// We can add any number of languages.
ocr.AddSecondaryLanguage(OcrLanguage.English);
// Optionally add custom tesseract .traineddata files by specifying a file path
using OcrInput input = new OcrInput();
input.LoadImage("img/MultiLanguage.jpeg");
OcrResult result = ocr.Read(input);
result.SaveAsTextFile("MultiLanguage.txt");Imports IronOcr
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.ChineseSimplified
' We can add any number of languages.
ocr.AddSecondaryLanguage(OcrLanguage.English)
' Optionally add custom tesseract .traineddata files by specifying a file path
Using input As New OcrInput()
input.LoadImage("img/MultiLanguage.jpeg")
Dim result As OcrResult = ocr.Read(input)
result.SaveAsTextFile("MultiLanguage.txt")
End UsingImports IronOcr
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.ChineseSimplified
' We can add any number of languages.
ocr.AddSecondaryLanguage(OcrLanguage.English)
' Optionally add custom tesseract .traineddata files by specifying a file path
Using input As New OcrInput()
input.LoadImage("img/MultiLanguage.jpeg")
Dim result As OcrResult = ocr.Read(input)
result.SaveAsTextFile("MultiLanguage.txt")
End Using如何使用C# OCR处理多页文档?
IronOCR将多页或多个图像组合成单个OcrResult。 此功能启用强大的功能,如创建可搜索PDFs和从整个文档集提取文本。
在单个 OCR 操作中混合和匹配各种来源(图像、TIFF 框架和 PDF 页面):
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.LoadImage("image1.jpeg");
input.LoadImage("image2.png");
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("image3.gif", pageindices);
OcrResult result = ocr.Read(input);
Console.WriteLine($"{result.Pages.Length} Pages"); // 3 Pagesusing IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.LoadImage("image1.jpeg");
input.LoadImage("image2.png");
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("image3.gif", pageindices);
OcrResult result = ocr.Read(input);
Console.WriteLine($"{result.Pages.Length} Pages"); // 3 PagesImports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
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)
Console.WriteLine($"{result.Pages.Length} Pages") ' 3 PagesImports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
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)
Console.WriteLine($"{result.Pages.Length} Pages") ' 3 Pages高效处理 TIFF 文件的所有页面:
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("MultiFrame.Tiff", pageindices);
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Console.WriteLine($"{result.Pages.Length} Pages");
// 1 page for every frame (page) in the TIFFusing IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("MultiFrame.Tiff", pageindices);
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Console.WriteLine($"{result.Pages.Length} Pages");
// 1 page for every frame (page) in the TIFFImports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("MultiFrame.Tiff", pageindices)
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
Console.WriteLine($"{result.Pages.Length} Pages")
' 1 page for every frame (page) in the TIFFImports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("MultiFrame.Tiff", pageindices)
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
Console.WriteLine($"{result.Pages.Length} Pages")
' 1 page for every frame (page) in the TIFF将 TIFF 或 PDF 转换为可搜索格式:
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");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")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")将现有 PDF 转换为可搜索版本:
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Pdf Metadata Name";
input.LoadPdf("example.pdf", Password: "password");
OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable.pdf");using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Pdf Metadata Name";
input.LoadPdf("example.pdf", Password: "password");
OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable.pdf");Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Pdf Metadata Name"
input.LoadPdf("example.pdf", Password:= "password")
Dim result As OcrResult = ocr.Read(input)
result.SaveAsSearchablePdf("searchable.pdf")Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Pdf Metadata Name"
input.LoadPdf("example.pdf", Password:= "password")
Dim result As OcrResult = ocr.Read(input)
result.SaveAsSearchablePdf("searchable.pdf")对 TIFF 转换应用同样的技巧:
using IronOcr;
var ocr = new IronTesseract();
using (var input = new OcrInput())
{
// Configure document properties
input.Title = "Scanned Archive Document";
// Select pages to process
var pageIndices = new int[] { 1, 2 };
input.LoadImageFrames("example.tiff", pageIndices);
// Create searchable PDF from TIFF
OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable.pdf");
}using IronOcr;
var ocr = new IronTesseract();
using (var input = new OcrInput())
{
// Configure document properties
input.Title = "Scanned Archive Document";
// Select pages to process
var pageIndices = new int[] { 1, 2 };
input.LoadImageFrames("example.tiff", pageIndices);
// Create searchable PDF from TIFF
OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable.pdf");
}Imports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' Configure document properties
input.Title = "Scanned Archive Document"
' Select pages to process
Dim pageIndices As Integer() = {1, 2}
input.LoadImageFrames("example.tiff", pageIndices)
' Create searchable PDF from TIFF
Dim result As OcrResult = ocr.Read(input)
result.SaveAsSearchablePdf("searchable.pdf")
End UsingImports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' Configure document properties
input.Title = "Scanned Archive Document"
' Select pages to process
Dim pageIndices As Integer() = {1, 2}
input.LoadImageFrames("example.tiff", pageIndices)
' Create searchable PDF from TIFF
Dim result As OcrResult = ocr.Read(input)
result.SaveAsSearchablePdf("searchable.pdf")
End Using如何将OCR结果导出为HOCR HTML?
IronOCR 支持 HOCR HTML 导出,可实现结构化的 PDF 至 HTML 和 TIFF 至 HTML 转换,同时保留布局信息:
using IronOcr;
var ocr = new IronTesseract();
using (var input = new OcrInput())
{
// Set HTML title
input.Title = "Document Archive";
// Process multiple document types
input.LoadImage("image2.jpeg");
input.LoadPdf("example.pdf", Password: "password");
// Add TIFF pages
var pageIndices = new int[] { 1, 2 };
input.LoadImageFrames("example.tiff", pageIndices);
// Export as HOCR with position data
OcrResult result = ocr.Read(input);
result.SaveAsHocrFile("hocr.html");
}using IronOcr;
var ocr = new IronTesseract();
using (var input = new OcrInput())
{
// Set HTML title
input.Title = "Document Archive";
// Process multiple document types
input.LoadImage("image2.jpeg");
input.LoadPdf("example.pdf", Password: "password");
// Add TIFF pages
var pageIndices = new int[] { 1, 2 };
input.LoadImageFrames("example.tiff", pageIndices);
// Export as HOCR with position data
OcrResult result = ocr.Read(input);
result.SaveAsHocrFile("hocr.html");
}Imports IronOcr
Dim ocr = New IronTesseract()
Using input = New OcrInput()
' Set HTML title
input.Title = "Document Archive"
' Process multiple document types
input.LoadImage("image2.jpeg")
input.LoadPdf("example.pdf", Password:="password")
' Add TIFF pages
Dim pageIndices = New Integer() {1, 2}
input.LoadImageFrames("example.tiff", pageIndices)
' Export as HOCR with position data
Dim result As OcrResult = ocr.Read(input)
result.SaveAsHocrFile("hocr.html")
End UsingImports IronOcr
Dim ocr = New IronTesseract()
Using input = New OcrInput()
' Set HTML title
input.Title = "Document Archive"
' Process multiple document types
input.LoadImage("image2.jpeg")
input.LoadPdf("example.pdf", Password:="password")
' Add TIFF pages
Dim pageIndices = New Integer() {1, 2}
input.LoadImageFrames("example.tiff", pageIndices)
' Export as HOCR with position data
Dim result As OcrResult = ocr.Read(input)
result.SaveAsHocrFile("hocr.html")
End UsingIronOCR能与文本一起读取条形码吗?
IronOCR独特地结合了文本识别与条形码读取功能,无需使用单独的库:
// Enable combined text and barcode recognition
using IronOcr;
var ocr = new IronTesseract();
// Enable barcode detection
ocr.Configuration.ReadBarCodes = true;
using (var input = new OcrInput())
{
// Load image containing both text and barcodes
input.LoadImage("img/Barcode.png");
// Process both text and barcodes
var result = ocr.Read(input);
// Extract barcode data
foreach (var barcode in result.Barcodes)
{
Console.WriteLine($"Barcode Value: {barcode.Value}");
Console.WriteLine($"Format: {barcode.Format}");
}
}// Enable combined text and barcode recognition
using IronOcr;
var ocr = new IronTesseract();
// Enable barcode detection
ocr.Configuration.ReadBarCodes = true;
using (var input = new OcrInput())
{
// Load image containing both text and barcodes
input.LoadImage("img/Barcode.png");
// Process both text and barcodes
var result = ocr.Read(input);
// Extract barcode data
foreach (var barcode in result.Barcodes)
{
Console.WriteLine($"Barcode Value: {barcode.Value}");
Console.WriteLine($"Format: {barcode.Format}");
}
}' Enable combined text and barcode recognition
Imports IronOcr
Dim ocr = New IronTesseract()
' Enable barcode detection
ocr.Configuration.ReadBarCodes = True
Using input = New OcrInput()
' Load image containing both text and barcodes
input.LoadImage("img/Barcode.png")
' Process both text and barcodes
Dim result = ocr.Read(input)
' Extract barcode data
For Each barcode In result.Barcodes
Console.WriteLine($"Barcode Value: {barcode.Value}")
Console.WriteLine($"Format: {barcode.Format}")
Next
End Using' Enable combined text and barcode recognition
Imports IronOcr
Dim ocr = New IronTesseract()
' Enable barcode detection
ocr.Configuration.ReadBarCodes = True
Using input = New OcrInput()
' Load image containing both text and barcodes
input.LoadImage("img/Barcode.png")
' Process both text and barcodes
Dim result = ocr.Read(input)
' Extract barcode data
For Each barcode In result.Barcodes
Console.WriteLine($"Barcode Value: {barcode.Value}")
Console.WriteLine($"Format: {barcode.Format}")
Next
End Using如何访问详细的OCR结果和元数据?
IronOCR 结果对象提供了全面的数据,高级开发人员可以利用这些数据开发复杂的应用程序。
每个OcrResult包含分级集合:页面、段落、行、词和字符。 所有元素都包括详细的元数据,如位置、字体信息和置信度分数。
单个元素(段落、单词、Barcode)可以导出为图像或位图,以便进一步处理:
using System;
using IronOcr;
using IronSoftware.Drawing;
// Configure with barcode support
IronTesseract ocr = new IronTesseract
{
Configuration = { ReadBarCodes = true }
};
using OcrInput input = new OcrInput();
// Process multi-page document
int[] pageIndices = { 1, 2 };
input.LoadImageFrames(@"img\Potter.tiff", pageIndices);
OcrResult result = ocr.Read(input);
// Navigate the complete results hierarchy
foreach (var page in result.Pages)
{
// Page-level data
int pageNumber = page.PageNumber;
string pageText = page.Text;
int pageWordCount = page.WordCount;
// Extract page elements
OcrResult.Barcode[] barcodes = page.Barcodes;
AnyBitmap pageImage = page.ToBitmap(input);
double pageWidth = page.Width;
double pageHeight = page.Height;
foreach (var paragraph in page.Paragraphs)
{
// Paragraph properties
int paragraphNumber = paragraph.ParagraphNumber;
string paragraphText = paragraph.Text;
double paragraphConfidence = paragraph.Confidence;
var textDirection = paragraph.TextDirection;
foreach (var line in paragraph.Lines)
{
// Line details including baseline information
string lineText = line.Text;
double lineConfidence = line.Confidence;
double baselineAngle = line.BaselineAngle;
double baselineOffset = line.BaselineOffset;
foreach (var word in line.Words)
{
// Word-level data
string wordText = word.Text;
double wordConfidence = word.Confidence;
// Font information (when available)
if (word.Font != null)
{
string fontName = word.Font.FontName;
double fontSize = word.Font.FontSize;
bool isBold = word.Font.IsBold;
bool isItalic = word.Font.IsItalic;
}
foreach (var character in word.Characters)
{
// Character-level analysis
string charText = character.Text;
double charConfidence = character.Confidence;
// Alternative character choices for spell-checking
OcrResult.Choice[] alternatives = character.Choices;
}
}
}
}
}using System;
using IronOcr;
using IronSoftware.Drawing;
// Configure with barcode support
IronTesseract ocr = new IronTesseract
{
Configuration = { ReadBarCodes = true }
};
using OcrInput input = new OcrInput();
// Process multi-page document
int[] pageIndices = { 1, 2 };
input.LoadImageFrames(@"img\Potter.tiff", pageIndices);
OcrResult result = ocr.Read(input);
// Navigate the complete results hierarchy
foreach (var page in result.Pages)
{
// Page-level data
int pageNumber = page.PageNumber;
string pageText = page.Text;
int pageWordCount = page.WordCount;
// Extract page elements
OcrResult.Barcode[] barcodes = page.Barcodes;
AnyBitmap pageImage = page.ToBitmap(input);
double pageWidth = page.Width;
double pageHeight = page.Height;
foreach (var paragraph in page.Paragraphs)
{
// Paragraph properties
int paragraphNumber = paragraph.ParagraphNumber;
string paragraphText = paragraph.Text;
double paragraphConfidence = paragraph.Confidence;
var textDirection = paragraph.TextDirection;
foreach (var line in paragraph.Lines)
{
// Line details including baseline information
string lineText = line.Text;
double lineConfidence = line.Confidence;
double baselineAngle = line.BaselineAngle;
double baselineOffset = line.BaselineOffset;
foreach (var word in line.Words)
{
// Word-level data
string wordText = word.Text;
double wordConfidence = word.Confidence;
// Font information (when available)
if (word.Font != null)
{
string fontName = word.Font.FontName;
double fontSize = word.Font.FontSize;
bool isBold = word.Font.IsBold;
bool isItalic = word.Font.IsItalic;
}
foreach (var character in word.Characters)
{
// Character-level analysis
string charText = character.Text;
double charConfidence = character.Confidence;
// Alternative character choices for spell-checking
OcrResult.Choice[] alternatives = character.Choices;
}
}
}
}
}Imports System
Imports IronOcr
Imports IronSoftware.Drawing
' Configure with barcode support
Dim ocr As New IronTesseract With {
.Configuration = New OcrConfiguration With {
.ReadBarCodes = True
}
}
Using input As New OcrInput()
' Process multi-page document
Dim pageIndices() As Integer = {1, 2}
input.LoadImageFrames("img\Potter.tiff", pageIndices)
Dim result As OcrResult = ocr.Read(input)
' Navigate the complete results hierarchy
For Each page In result.Pages
' Page-level data
Dim pageNumber As Integer = page.PageNumber
Dim pageText As String = page.Text
Dim pageWordCount As Integer = page.WordCount
' Extract page elements
Dim barcodes() As OcrResult.Barcode = page.Barcodes
Dim pageImage As AnyBitmap = page.ToBitmap(input)
Dim pageWidth As Double = page.Width
Dim pageHeight As Double = page.Height
For Each paragraph In page.Paragraphs
' Paragraph properties
Dim paragraphNumber As Integer = paragraph.ParagraphNumber
Dim paragraphText As String = paragraph.Text
Dim paragraphConfidence As Double = paragraph.Confidence
Dim textDirection = paragraph.TextDirection
For Each line In paragraph.Lines
' Line details including baseline information
Dim lineText As String = line.Text
Dim lineConfidence As Double = line.Confidence
Dim baselineAngle As Double = line.BaselineAngle
Dim baselineOffset As Double = line.BaselineOffset
For Each word In line.Words
' Word-level data
Dim wordText As String = word.Text
Dim wordConfidence As Double = word.Confidence
' Font information (when available)
If word.Font IsNot Nothing Then
Dim fontName As String = word.Font.FontName
Dim fontSize As Double = word.Font.FontSize
Dim isBold As Boolean = word.Font.IsBold
Dim isItalic As Boolean = word.Font.IsItalic
End If
For Each character In word.Characters
' Character-level analysis
Dim charText As String = character.Text
Dim charConfidence As Double = character.Confidence
' Alternative character choices for spell-checking
Dim alternatives() As OcrResult.Choice = character.Choices
Next
Next
Next
Next
Next
End UsingImports System
Imports IronOcr
Imports IronSoftware.Drawing
' Configure with barcode support
Dim ocr As New IronTesseract With {
.Configuration = New OcrConfiguration With {
.ReadBarCodes = True
}
}
Using input As New OcrInput()
' Process multi-page document
Dim pageIndices() As Integer = {1, 2}
input.LoadImageFrames("img\Potter.tiff", pageIndices)
Dim result As OcrResult = ocr.Read(input)
' Navigate the complete results hierarchy
For Each page In result.Pages
' Page-level data
Dim pageNumber As Integer = page.PageNumber
Dim pageText As String = page.Text
Dim pageWordCount As Integer = page.WordCount
' Extract page elements
Dim barcodes() As OcrResult.Barcode = page.Barcodes
Dim pageImage As AnyBitmap = page.ToBitmap(input)
Dim pageWidth As Double = page.Width
Dim pageHeight As Double = page.Height
For Each paragraph In page.Paragraphs
' Paragraph properties
Dim paragraphNumber As Integer = paragraph.ParagraphNumber
Dim paragraphText As String = paragraph.Text
Dim paragraphConfidence As Double = paragraph.Confidence
Dim textDirection = paragraph.TextDirection
For Each line In paragraph.Lines
' Line details including baseline information
Dim lineText As String = line.Text
Dim lineConfidence As Double = line.Confidence
Dim baselineAngle As Double = line.BaselineAngle
Dim baselineOffset As Double = line.BaselineOffset
For Each word In line.Words
' Word-level data
Dim wordText As String = word.Text
Dim wordConfidence As Double = word.Confidence
' Font information (when available)
If word.Font IsNot Nothing Then
Dim fontName As String = word.Font.FontName
Dim fontSize As Double = word.Font.FontSize
Dim isBold As Boolean = word.Font.IsBold
Dim isItalic As Boolean = word.Font.IsItalic
End If
For Each character In word.Characters
' Character-level analysis
Dim charText As String = character.Text
Dim charConfidence As Double = character.Confidence
' Alternative character choices for spell-checking
Dim alternatives() As OcrResult.Choice = character.Choices
Next
Next
Next
Next
Next
End Using总结
IronOCR为C#开发者提供最先进的Tesseract API实现,可在Windows、Linux和Mac平台上运行。 其使用 IronOCR 从图像中准确读取文本的能力--即使是不完美的文档--使其有别于基本的 OCR 解决方案。
该库的独特功能包括集成条形码读取功能,以及将结果导出为可搜索 PDF 或 HOCR HTML 的功能,这些功能是标准 Tesseract 实现所不具备的。
向前迈进
要继续掌握 IronOCR:
源代码下载
准备好在您的应用程序中实现 C# OCR 图像到文本的转换了吗? 下载IronOCR并立即开始您的免费试用。
常见问题
什么是IronOCR,与Tesseract有何不同?
IronOCR是一个C# OCR库,提供图像到文本的转换,而不需要如Tesseract那样复杂的安装和配置。它结合了Tesseract引擎,但针对.NET应用进行了优化。
如何使用C# IronOCR从图像中提取文本?
要使用IronOCR从图像中提取文本,您可以使用IronTesseract类。对于简单的使用情况,初始化一个新实例并调用Read方法与图像文件路径,这将返回识别的文本。
IronOCR支持哪些图像文件格式?
IronOCR支持包括JPEG、TIFF、GIF、BMP和PNG在内的多种图像格式。它还可以处理整个PDF或其中的特定页面。
图像滤镜如何提高IronOCR中的OCR准确性?
IronOCR中的图像滤镜通过纠正图像缺陷,如倾斜、噪点和对比度不佳来提高OCR准确性。滤镜如Deskew、DeNoise和EnhanceResolution通常用于提升输入质量。
IronOCR 能处理多种语言吗?
是的,IronOCR支持127种国际语言。您可以通过从NuGet或Iron Software网站下载语言包来轻松配置它以识别不同语言的文本。
如何使用IronOCR进行特定区域的文本提取?
IronOCR可以使用System.Drawing.Rectangle针对特定区域进行文本提取,这限制了OCR操作到定义的区域,从而提高速度并防止不需要的文本提取。
使用IronOCR进行多页文档处理有哪些好处?
IronOCR可以在单次OCR操作中处理多页文档或图像,产生全面的结果,可组合成可搜索的PDF或提取为完整的文本文件。
IronOCR是否支持将OCR结果导出为HTML?
是的,IronOCR允许您将OCR结果导出为HOCR HTML,保留布局信息,并支持将PDF和TIFF结构化转换为HTML。
IronOCR能否同时读取条形码和文本?
IronOCR不仅能够进行文本识别,还可以读取条形码,将这两个功能合并在一个库中,这消除了需要单独的条形码阅读软件的需求。
如何优化IronOCR以提高OCR处理速度?
要优化IronOCR的速度,使用高质量、较少噪点的图像,利用合适的图像滤镜,并配置BlackListCharacters和PageSegmentationMode等设置以优化处理。
Jacob Mellor是Iron Software的首席技术官,也是C# PDF技术的先锋工程师。作为Iron Software核心代码库的原始开发者,自公司成立以来,他就一直在塑造公司的产品架构,与首席执行官Cameron Rimington一起将其转型为拥有50多人、服务于NASA、特斯拉和全球政府机构的公司。