
如何在 C# 教程中从发票中获取文本
如何在 Tesseract 中对收据进行 OCR 识别
- 安装通过 Tesseract 进行收据 OCR 的 C# 库 探索功能丰富的C#库以对收据执行OCR
- 使用 Tesseract 从收据中提取数据
- 在提取的文本结果中搜索特定数据 读取输入收据图像上的条形码值
1. IronOCR,一种光学字符识别 API
IronOCR 是一个 OCR 库,可用于识别图像中的文本数据以进行信息提取,包括收据 OCR。 它建立在 Tesseract OCR 引擎上,该引擎被认为是迄今为止用于收据识别的最准确的 OCR 引擎之一。 IronOCR 可以读取不同文档类型的关键信息,包括 PNG、JPG、TIFF、JSON 和 PDF 格式,并且可以识别多种语言的文本。
IronOCR 的一个重要功能是能够自动检测文本方向,即使图像已旋转或倾斜。 这对于准确识别收据上传和数据提取中的文本至关重要,因为收据通常包含大量信息,并且可能被折叠或皱褶,导致文本倾斜。
2. IronOCR功能
- C# OCR 使用深度学习从照片、扫描的文档和 PDF 中扫描和识别文本。
- .NET OCR 支持超过125种全球语言。
- IronOCR 可以读取多种文件格式的图像中的文本,包括 PNG、JPG、TIFF 和 PDF。
从提取信息中可以生成<Text>、结构化数据、JSON 输出或可搜索的 PDF。- IronOCR 支持 .NET 版本 5、6 和 7(Core、Framework 和 Standard)。
- IronOCR 将输入根据文本区域划分成不同的图片。 它使用计算机视觉来识别包含文本元素的区域。
3. 在Visual Studio中创建新项目
打开 Visual Studio 并转到文件菜单。 选择"新建项目",然后选择控制台应用程序。
在适当的文本框中输入项目名称并选择路径。 然后,点击创建按钮。 选择所需的 .NET Framework,如下图所示:

控制台应用程序的项目结构将被生成。 完成后,它将打开 Program.cs 文件,您可以在其中编写和执行源代码。

4. 安装IronOCR
在 Visual Studio 中,您可以轻松地将 IronOCR 集成到您的 C# 项目中。 IronOCR 提供了多种与 C# .NET 项目集成的方式。 在这里,我们将讨论其中之一:使用 NuGet 包管理器安装 IronOCR。
在 Visual Studio 中,进入工具 > NuGet 包管理器 > 包管理器控制台

一个新的控制台将出现在 Visual Studio 窗口的底部。 在控制台中输入以下命令并按回车。
IronOCR将在几秒钟内安装完成。
5. 使用 IronOCR 从收据中提取数据
IronOCR 是一个强大的 OCR 库,可以用于从收据中提取和访问详细数据。使用 IronOCR,您可以将收据的图片转换为易于分析和处理的机读文本,而不会影响数据隐私。
以下是如何使用 IronOCR 从收据中提取文本的示例:
using IronOcr;
using System;
class Program
{
static void Main()
{
IronTesseract ocrTesseract = new IronTesseract();
// Load the receipt image
using (OcrInput ocrInput = new OcrInput("ocr.png"))
{
// Read the OCR result
OcrResult ocrResult = ocrTesseract.Read(ocrInput);
string recognizedText = ocrResult.Text;
// Output the recognized text to the console
Console.WriteLine(recognizedText);
}
}
}Imports IronOcr
Imports System
Friend Class Program
Shared Sub Main()
Dim ocrTesseract As New IronTesseract()
' Load the receipt image
Using ocrInput As New OcrInput("ocr.png")
' Read the OCR result
Dim ocrResult As OcrResult = ocrTesseract.Read(ocrInput)
Dim recognizedText As String = ocrResult.Text
' Output the recognized text to the console
Console.WriteLine(recognizedText)
End Using
End Sub
End Class请参考从图像读取文本教程以了解更多关于 IronOCR 如何使用 C# 从图像中读取文本。
以上代码的输出:
- LOGO SHOP
- LOREM IPSUM
- DOLOR SIT AMET CONSECTETUR
- ADIPISCING ELIT
- 1 LOREM IPSUM $3.20
- 2 ORNARE MALESUADA $9.50
- 3 PORTA FERMENTUM $5.90
- 4 SODALES ARCU $6.00
- 5 ELEIFEND $9.00
- 6 SEM NISIMASSA $0.50
- 7 DUIS FAMES DIS $7.60
- 8 FACILISI RISUS $810
- TOTAL AMOUNT $49.80
- CASH $50.00
6. 使用 IronOCR 从收据图像中提取特定数据
IronOCR 允许开发人员从扫描的收据中提取关键信息,例如税额和商家名称。
这是一个展示如何从收据图像中提取总金额的示例:
using IronOcr;
using System;
class Program
{
static void Main()
{
IronTesseract ocrTesseract = new IronTesseract();
// Set the language for OCR
ocrTesseract.Language = OcrLanguage.English;
// Load the receipt image
using (OcrInput ocrInput = new OcrInput("ocr.png"))
{
// Optimize the input image for OCR
ocrInput.DeNoise(true);
ocrInput.Contrast();
ocrInput.EnhanceResolution();
ocrInput.ToGrayScale();
OcrResult ocrResult = ocrTesseract.Read(ocrInput);
// Search for the total amount in the OCR result
var totalAmount = ocrResult.Text.Contains("Total:") ? ocrResult.Text.Split("Total:")[1].Split("\n")[0] : "";
Console.WriteLine("Total Amount: " + totalAmount);
}
}
}Imports Microsoft.VisualBasic
Imports IronOcr
Imports System
Friend Class Program
Shared Sub Main()
Dim ocrTesseract As New IronTesseract()
' Set the language for OCR
ocrTesseract.Language = OcrLanguage.English
' Load the receipt image
Using ocrInput As New OcrInput("ocr.png")
' Optimize the input image for OCR
ocrInput.DeNoise(True)
ocrInput.Contrast()
ocrInput.EnhanceResolution()
ocrInput.ToGrayScale()
Dim ocrResult As OcrResult = ocrTesseract.Read(ocrInput)
' Search for the total amount in the OCR result
Dim totalAmount = If(ocrResult.Text.Contains("Total:"), ocrResult.Text.Split("Total:")(1).Split(vbLf)(0), "")
Console.WriteLine("Total Amount: " & totalAmount)
End Using
End Sub
End Class感谢OcrInput类提供的多个设置,可以优化输入图像以提高OCR过程的准确性。
输入

输出
- Total 16.5
7. 读取收据上的条形码
IronOCR 可用于读取收据上的条形码以及文本。 要读取收据上的条形码,您需要将BarcodeReader类与ReadBarCodes方法结合使用。
以下是如何读取条形码的示例:
using IronOcr;
using System;
class Program
{
static void Main()
{
var ocrTesseract = new IronTesseract();
ocrTesseract.Configuration.ReadBarCodes = true;
// Load the receipt image with a barcode
using (var ocrInput = new OcrInput("b.png"))
{
OcrResult ocrResult = ocrTesseract.Read(ocrInput);
// Output the barcode values to the console
foreach (var barcode in ocrResult.Barcodes)
{
Console.WriteLine(barcode.Value);
}
}
}
}Imports IronOcr
Imports System
Class Program
Shared Sub Main()
Dim ocrTesseract = New IronTesseract()
ocrTesseract.Configuration.ReadBarCodes = True
' Load the receipt image with a barcode
Using ocrInput = New OcrInput("b.png")
Dim ocrResult As OcrResult = ocrTesseract.Read(ocrInput)
' Output the barcode values to the console
For Each barcode In ocrResult.Barcodes
Console.WriteLine(barcode.Value)
Next
End Using
End Sub
End Class输入图像

输出文本

8. 结论
本文上面解释了如何在 C# 项目中安装和使用 IronOCR 以从收据中提取数据,提供了示例代码片段。
请阅读从图像读取文本的教程。
IronOCR 是Iron Suite的一部分,该套件包括五个不同的 .NET 库用于操作文档和图像。 您可以以仅相当于两个IronOCR 许可证的价格购买整个 Iron Suite。
在您的生产应用程序中尝试 IronOCR,免费试用。
相关文章


