跳至页脚内容
使用 IRONOCR

如何在 C# 教程中从发票中获取文本

How to OCR Receipt in Tesseract

  1. Install C# library OCR receipt with Tesseract
  2. Explore features rich C# library for performing OCR on receipt
  3. Extract data from receipt with Tesseract
  4. Search in the extracted text result for specific data
  5. Read barcodes value on the input receipt image

1. IronOCR, An Optical Character Recognition API

IronOCR is an OCR library that can be used to recognize text data from images for information extraction, including receipt OCR. It is built on the Tesseract OCR engine, which is considered one of the most accurate OCR engines available to date for receipt recognition. IronOCR can read key information from different document types, including PNG, JPG, TIFF, JSON, and PDF formats, and it can recognize text in multiple languages.

One of the key features of IronOCR that makes it particularly useful for receipt OCR is its ability to automatically detect text orientation, even if the image has been rotated or skewed. This is essential for accurate text recognition on receipts uploads and data extraction, as receipts often contain a lot of information and can be folded or crumpled, causing the text to become skewed.

2. IronOCR Features

  • C# OCR uses Deep Learning to scan and recognize texts from pictures, scanned documents, and PDFs.
  • .NET OCR supports more than 125 global languages.
  • IronOCR can read text from images in many file formats, including PNG, JPG, TIFF, and PDF.
  • Text, structured data, JSON output, or searchable PDFs can be produced from extracted information.
  • IronOCR supports .NET versions 5, 6, and 7 (Core, Framework, and Standard).
  • IronOCR divides the input into different pictures based on text regions. It uses Computer Vision to identify areas that contain text elements.

3. Creating a New Project in Visual Studio

Open Visual Studio and go to the File menu. Select "New Project" and then choose Console Application.

Enter the project name and select the path in the appropriate text box. Then, click the Create button. Select the required .NET Framework, as shown in the screenshot below:

Creating a new project in Visual Studio

The project structure for the Console Application will now be generated. Once finished, it will open the Program.cs file, in which you can write and execute source code.

The Program.cs file generated from the Visual Studio New Project Wizard

4. Install IronOCR

In Visual Studio, you can integrate IronOCR with your C# project easily. IronOCR offers multiple ways to integrate with a C# .NET project. Here, we'll discuss one of them: Installing IronOCR using the NuGet Package Manager.

In Visual Studio, go to Tools > NuGet Package Manager > Package Manager Console

The Visual Studio NuGet Package Manager Console

A new console will appear at the bottom of Visual Studio's window. Type the below command in the console and press enter.

Install-Package IronOcr

IronOCR will be installed in just a few seconds.

5. Data Extraction from Receipts Using IronOCR

IronOCR is a powerful OCR library that can be used to extract and access detailed data from receipts. With IronOCR, you can convert a picture of a receipt into machine-readable text that can be easily analyzed and processed without compromising data privacy.

Here's an example of how you can use IronOCR to extract text from a receipt:

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);
        }
    }
}
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
$vbLabelText   $csharpLabel

Refer to the Reading Text from Image tutorial for further details on how IronOCR reads text from images using C#.

The output of the code above:

- 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. Specific Data Extraction From Receipt Image Using IronOCR

IronOCR allows developers to retrieve crucial information from scanned receipts, such as tax amounts and merchant names.

Here is an example demonstrating how to extract the total amount value from a receipt image:

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);
        }
    }
}
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
$vbLabelText   $csharpLabel

Thanks to the multiple settings provided by the OcrInput class, it is possible to optimize the input image for better accuracy in the OCR process.

Input

The input image used for demonstrating extraction of specific data from receipts

Output

- Total 16.5

7. Read Barcodes on Receipts

IronOCR can be used to read barcodes on receipts as well as text. To read barcodes on receipts, you will need to use the BarcodeReader class in combination with the ReadBarCodes method.

Here's an example of how to read barcodes:

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);
            }
        }
    }
}
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

Friend Class Program
	Shared Sub Main()
		Dim ocrTesseract = New IronTesseract()
		ocrTesseract.Configuration.ReadBarCodes = True

		' Load the receipt image with a barcode
		Using ocrInput As 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 barcode
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

Input Image

Input for Reading Barcode

Output Text

The result of processing the barcode image

8. Conclusion

The article above explains the process of installing and using IronOCR in a C# project to extract data from receipts, with example code snippets provided.

Please read the tutorial on reading text from images.

IronOCR is a part of the Iron Suite, which includes five different .NET libraries for manipulating documents and images. You can buy the entire Iron Suite for the price of just two IronOCR licenses.

Try IronOCR in your production apps with a free trial.

常见问题解答

如何使用 IronOCR 在 C# 中对收据图像执行 OCR?

您可以使用 IronOCR 通过加载图像到 OcrInput 类并调用 Read 方法来提取文本数据,如项目列表和总金额,从而对收据图像执行 OCR。

在发票处理方面使用 IronOCR 相较于 Tesseract 的优势是什么?

IronOCR 提供增强的准确性,支持超过 125 种语言,并包括自动文本方向检测和深度学习功能。它还更易于通过 NuGet 包管理器与 C# 项目集成。

如何将 IronOCR 集成到 Visual Studio 项目中?

要将 IronOCR 集成到 Visual Studio 项目,请使用 NuGet 包管理器。导航到工具 > NuGet 包管理器 > 包管理器控制台,然后执行 Install-Package IronOcr 将该库添加到项目中。

IronOCR 可以在收据 OCR 中处理多种语言吗?

是的,IronOCR 可以处理多种语言,支持超过 125 种全球语言,使其非常适合处理带有多语种文本的收据。

IronOCR 如何改善收据中的文本识别准确性?

IronOCR 通过深度学习、自动文本方向检测等功能及使用 OcrInput 类优化图像以获得更好的 OCR 结果来改善文本识别准确性。

是否可以使用 IronOCR 从收据中提取项目化清单?

是的,IronOCR 可以用来从收据中提取项目化清单,通过执行 OCR 后处理文本数据并通过模式匹配识别行项目。

IronOCR 如何处理收据上的条形码读取?

IronOCR 通过使用 BarcodeReader 类和 ReadBarCodes 方法扫描和解码收据上存在的条形码来处理条形码读取。

IronOCR 可以处理哪些文件格式以进行收据 OCR?

IronOCR 可以处理多种文件格式以进行收据 OCR,包括 PNG、JPG、TIFF 和 PDF,使其适用于不同类型的输入。

在 C# 中设置 IronOCR 以进行发票处理需要哪些步骤?

设置 IronOCR 用于发票处理涉及通过 NuGet 安装库,配置带有收据图像的 OcrInput,并使用 Read 方法提取文本数据。您还可以使用库的功能以提高准确性并提取总计等特定数据。

Kannaopat Udonpant
软件工程师
在成为软件工程师之前,Kannapat 在日本北海道大学完成了环境资源博士学位。在攻读学位期间,Kannapat 还成为了车辆机器人实验室的成员,隶属于生物生产工程系。2022 年,他利用自己的 C# 技能加入 Iron Software 的工程团队,专注于 IronPDF。Kannapat 珍视他的工作,因为他可以直接从编写大多数 IronPDF 代码的开发者那里学习。除了同行学习外,Kannapat 还喜欢在 Iron Software 工作的社交方面。不撰写代码或文档时,Kannapat 通常可以在他的 PS5 上玩游戏或重温《最后生还者》。