使用 IRONOCR

如何在C#中创建OCR软件演示

发布 2024年六月6日
分享:

光学字符识别 (光学字符识别) 是一种将各种文件格式(包括扫描的纸质文件、PDF、数字文件或用数码相机拍摄的印刷文本图像)转换为可编辑和可搜索的机器编码文本数据的技术。

IronOCR 是一个出色的 OCR 引擎库,可为开发人员提供强大的 OCR 功能。在本文中,我们将通过OCR 软件演示的代码示例,探讨如何使用 IronOCR 执行 OCR。

什么是 IronOCR?

IronOCR 是一个功能强大的 .NET 库,旨在促进光学字符识别 (光学字符识别) 在 C# 和 VB.NET 应用程序中使用。利用先进的算法和机器学习技术,IronOCR 可以从扫描的 PDF 文件、图像和 PDF 文件中准确提取文本和内容,从而更轻松地以编程方式处理、搜索和分析此类文件。

凭借其简单的应用程序接口(API)和丰富的功能,开发人员可以将 OCR 功能无缝集成到自己的应用程序中,从而实现数据提取、文档处理、数据录入和内容管理任务的自动化。无论您是要处理业务、发票、报告、自动数据提取、可搜索的 PDF 还是其他文本丰富的文档,IronOCR 都能提供可靠的解决方案,高效处理 OCR 要求。

IronOCR 入门

在深入学习示例代码之前,您需要安装 通过 NuGet 软件包管理器运行 IronOCR.在软件包管理器控制台运行以下命令即可安装 IronOCR:

Install-Package IronOcr
Install-Package IronOcr
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronOcr
VB   C#

使用 IronOCR 执行 OCR

基本文本识别

要使用 IronOCR 进行基本文本识别,可以使用以下代码片段:

using IronOcr;
using System;
IronTesseract ocrTesseract = new IronTesseract();
using (OcrInput ocrInput = new OcrInput("ocr.png"))
{
    OcrResult ocrResult = ocrTesseract.Read(ocrInput);
    string RecognizedText = ocrResult.Text;
    Console.WriteLine(RecognizedText);
}
using IronOcr;
using System;
IronTesseract ocrTesseract = new IronTesseract();
using (OcrInput ocrInput = new OcrInput("ocr.png"))
{
    OcrResult ocrResult = ocrTesseract.Read(ocrInput);
    string RecognizedText = ocrResult.Text;
    Console.WriteLine(RecognizedText);
}
Imports IronOcr
Imports System
Private ocrTesseract As New IronTesseract()
Using ocrInput As New OcrInput("ocr.png")
	Dim ocrResult As OcrResult = ocrTesseract.Read(ocrInput)
	Dim RecognizedText As String = ocrResult.Text
	Console.WriteLine(RecognizedText)
End Using
VB   C#

该代码使用 IronOCR 进行光学字符识别 (光学字符识别) 在名为 "ocr.png "的图像文件上运行。它初始化一个 IronTesseract 对象,并将图像文件的文本层读入一个 OcrInput 对象。

然后以RecognizedText的形式获取 OCR 结果,并打印到控制台。

输出

``cs

  • LOGO SHOP

  • lorem ipsum

  • 无色无迹

  • adipiscing elit

  • 1 lorem ipsum $3.20

  • 2 ornare malesuada 9.50 美元

  • 3 porta fermentum $5.90

  • 4 个蜗牛 6.00 美元

  • 5 eleifend 9.00 美元

  • 6 semnisimassa 0.50 美元

  • 7 duis fames dis $7.60

  • 8 facilisirisus $810

  • 总计 49.80 美元

  • 现金 $50.00

### 高级 OCR 选项

IronOCR 提供多种选项,可让您根据图像文件和要求自定义 OCR 流程。例如,您可以指定 OCR 语言、调整图像预处理设置或启用文本清理。下面的示例演示了其中一些高级选项:

```cs
using IronOcr;
class Program
{
    static void Main()
    {
        var ocr = new IronTesseract();
        using var ocrInput = new OcrInput();
    ocrInput.LoadImage(@"images\image.png");
        // Set OCR language to English
        ocr.Language = OcrLanguage.English;
        // Enable text cleaning
        ocrInput.DeNoise();
    ocrInput.EnhanceResolution(225);
        var result = ocr.Read(ocrInput);
        if (result != null && result.Text != null)
        {
            Console.WriteLine($"Recognized Text: {result.Text}");
        }
    }
}

代码使用 IronOCR 对位于 "images "文件夹中的图像文件 "image.png "执行 OCR。它将 OCR 语言设置为英语,清除图像噪点并增强其分辨率。从图像中提取识别出的文本,然后打印到控制台。

如何在 C# 中创建 OCR 软件演示:图 1

条形码读取

IronOCR 还支持条形码读取,允许您制作软件从图像中提取条形码信息。下面的代码示例演示了如何使用 IronOCR 读取条形码:

using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
ocrTesseract.Configuration.ReadBarCodes = true;
using var ocrInput = new OcrInput();
ocrInput.LoadImage(@"images\imageWithBarcode.png");
var ocrResult = ocrTesseract.Read(ocrInput);
foreach (var barcode in ocrResult.Barcodes)
{
    Console.WriteLine(barcode.Value);
}
using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
ocrTesseract.Configuration.ReadBarCodes = true;
using var ocrInput = new OcrInput();
ocrInput.LoadImage(@"images\imageWithBarcode.png");
var ocrResult = ocrTesseract.Read(ocrInput);
foreach (var barcode in ocrResult.Barcodes)
{
    Console.WriteLine(barcode.Value);
}
Imports IronOcr
Imports System
Private ocrTesseract = New IronTesseract()
ocrTesseract.Configuration.ReadBarCodes = True
Dim ocrInput As New OcrInput()
ocrInput.LoadImage("images\imageWithBarcode.png")
Dim ocrResult = ocrTesseract.Read(ocrInput)
For Each barcode In ocrResult.Barcodes
	Console.WriteLine(barcode.Value)
Next barcode
VB   C#

该代码使用 IronOCR 从 "images "文件夹中的图像文件 "imageWithBarcode.png "中检测和读取条形码。它通过将 ReadBarCodes 设置为 true 来配置 IronOCR 以启用条形码读取功能。检测到的条形码值将打印到控制台。

如何在 C# 中创建 OCR 软件演示:图 2

PDF 文本提取

IronOCR 还可以从 PDF 和扫描文件中提取文本。下面的代码示例演示了如何使用 IronOCR 从 PDF 文件中提取文本:

using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
// OCR entire document
ocrInput.LoadPdf("Email_Report.pdf");
int[] pages = { 1, 2, 3, 4, 5 };
// Alternatively OCR selected page numbers
ocrInput.LoadPdfPages("example.pdf", pages, Password: "password");
var ocrResult = ocrTesseract.Read(ocrInput);
Console.WriteLine(ocrResult.Text);
using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
// OCR entire document
ocrInput.LoadPdf("Email_Report.pdf");
int[] pages = { 1, 2, 3, 4, 5 };
// Alternatively OCR selected page numbers
ocrInput.LoadPdfPages("example.pdf", pages, Password: "password");
var ocrResult = ocrTesseract.Read(ocrInput);
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports System
Private ocrTesseract = New IronTesseract()
Private ocrInput = New OcrInput()
' OCR entire document
ocrInput.LoadPdf("Email_Report.pdf")
Dim pages() As Integer = { 1, 2, 3, 4, 5 }
' Alternatively OCR selected page numbers
ocrInput.LoadPdfPages("example.pdf", pages, Password:= "password")
Dim ocrResult = ocrTesseract.Read(ocrInput)
Console.WriteLine(ocrResult.Text)
VB   C#

代码使用 IronOCR 对名为 "Email_Report.pdf "的 PDF 文档执行 OCR 处理。它可以使用LoadPdf对整个文档进行 OCR 处理,也可以使用LoadPdfPages对 "example.pdf "中的特定页面进行 OCR 处理并输入密码。OCR 操作识别出的文本将打印到控制台。

如何在 C# 中创建 OCR 软件演示:图 3

结论

IronOCR IronOCR 是一个功能强大的 .NET 库,提供先进的 OCR 软件功能,使开发人员可以轻松地在其应用程序中执行 OCR 任务。在本文中,我们通过代码示例探讨了如何使用 IronOCR 执行基本和高级 OCR 软件演示。

无论您需要识别手写文本、各种数字格式、扫描和读取条形码,还是从 PDF 文档中提取文本,IronOCR 都能为您提供全面的功能。

如果您正在开发一个.NET项目,并需要集成 OCR 功能,那么 IronOCR 绝对值得您在考虑不同的 OCR 引擎时加以考虑。它的易用性、速度、灵活性和丰富的文档使其成为开发人员完成 OCR 自动化任务的首选。

为什么不试试 IronOCR,看看它如何简化您的 OCR 项目开发流程?它可能是最适合您项目的 OCR 引擎。

IronOCR 提供免费的 试用许可 IronOCR 的价格从 749 美元起,让您可以在项目中继续充分利用 IronOCR。

要了解有关 IronOCR 的更多信息,请访问 这里.

下一步 >
如何在C#中执行车辆注册OCR

准备开始了吗? 版本: 2024.9 刚刚发布

免费NuGet下载 总下载量: 2,319,721 查看许可证 >