
已更新 2026年4月21日
Power Automate OCR(开发者教程)
光学字符识别技术在文档数字化、自动化PDF数据提取和录入、发票处理和使扫描的 PDF 可搜索的应用中得到了应用。
阅读更多
利用 IronOCR 和 Tesseract 等库,开发人员可以使用高级算法和机器学习技术从图像和扫描文档中提取文本信息。 本教程将向读者展示如何使用 Tesseract 库从图像中提取文本,最后介绍 IronOCR 的独特方法。
using NuGet 包管理器控制台,输入以下命令:
或者通过 NuGet 程序包管理器下载该程序包。
在NuGet包管理器中安装Tesseract包
安装 NuGet 包后,必须手动将语言文件安装并保存到项目文件夹中。 这可以被视为该特定库的一个缺陷。
请访问以下网站下载语言文件。 下载完成后,解压缩文件,并将"tessdata"文件夹添加到项目的调试文件夹中。
可以使用以下源代码对给定图像进行OCR识别:
using Tesseract;
class Program
{
static void Main()
{
// Initialize Tesseract engine with English language data
using var ocrEngine = new TesseractEngine(@"tessdata", "eng", EngineMode.Default);
// Load the image to be processed
using var img = Pix.LoadFromFile("Demo.png");
// Process the image to extract text
using var res = ocrEngine.Process(img);
// Output the recognized text
Console.WriteLine(res.GetText());
Console.ReadKey();
}
}Imports Tesseract
Friend Class Program
Shared Sub Main()
' Initialize Tesseract engine with English language data
Dim ocrEngine = New TesseractEngine("tessdata", "eng", EngineMode.Default)
' Load the image to be processed
Dim img = Pix.LoadFromFile("Demo.png")
' Process the image to extract text
Dim res = ocrEngine.Process(img)
' Output the recognized text
Console.WriteLine(res.GetText())
Console.ReadKey()
End Sub
End ClassTesseractEngine对象,将语言数据加载到引擎中。Pix.LoadFromFile帮助加载所需的图像文件。Process方法提取文本。GetText方法获取识别文本并打印到控制台。
从图像中提取的文本
要了解有关 C# 中 Tesseract 的更多信息,请访问Tesseract 教程。
在 NuGet 包管理器控制台中输入以下命令:
或者,您也可以通过 NuGet 包管理器安装 IronOCR 库,以及其他语言的附加包,这些包使用起来既简单又方便。
通过NuGet包管理器安装IronOCR和语言包
以下是识别给定图像中文本的示例代码:
using IronOcr;
class Program
{
static void Main()
{
// Create an IronTesseract instance with predefined settings
var ocr = new IronTesseract()
{
Language = OcrLanguage.EnglishBest,
Configuration = { TesseractVersion = TesseractVersion.Tesseract5 }
};
// Create an OcrInput instance for image processing
using var input = new OcrInput();
// Load the image to be processed
input.AddImage("Demo.png");
// Process the image and extract text
var result = ocr.Read(input);
// Output the recognized text
Console.WriteLine(result.Text);
Console.ReadKey();
}
}Imports IronOcr
Friend Class Program
Shared Sub Main()
' Create an IronTesseract instance with predefined settings
Dim ocr = New IronTesseract() With {
.Language = OcrLanguage.EnglishBest,
.Configuration = { TesseractVersion = TesseractVersion.Tesseract5 }
}
' Create an OcrInput instance for image processing
Dim input = New OcrInput()
' Load the image to be processed
input.AddImage("Demo.png")
' Process the image and extract text
Dim result = ocr.Read(input)
' Output the recognized text
Console.WriteLine(result.Text)
Console.ReadKey()
End Sub
End ClassIronTesseract对象,设置语言和Tesseract版本。AddImage方法加载图像文件。IronTesseract处理图像并提取文本,然后将其打印到控制台。
使用 IronOCR 库提取文本输出
有关 IronOCR 的详细教程,请参阅本文,了解如何在 C# 中从图像中读取文本。