Saltar al pie de página
USANDO IRONOCR

Cómo crear un escáner de recibos OCR en C#

This tutorial is designed to help beginners create an OCR Receipt Scanner using the IronOCR, an OCR API in C#. By the end of this guide, you will understand how to implement optical character recognition (OCR) to convert different types of receipt files into editable and searchable data using receipt OCR API. This technology can be a game-changer for businesses looking to automate expense management and minimize manual data entry. Let's get started!

How To Create an OCR Receipt Scanner In C#

  1. Create a C# Console project in Visual Studio.
  2. Install the OCR library using NuGet Package Manager.
  3. Load the receipt into the program using the OcrInput method.
  4. Extract the text using the Read method.
  5. Show the extracted text on the console.

Prerequisites

Before we dive into the coding part, make sure you have the following:

  1. Visual Studio: This will be our Integrated Development Environment (IDE), where we will write and run our C# code.
  2. IronOCR Library: We will use IronOCR, an advanced OCR library that can be easily integrated into C# applications.
  3. Sample Receipt: A receipt image file named Sample_Receipt.jpg, which we will use to test our OCR implementation.

How To Create an OCR Receipt Scanner In C#: Figure 1 - Image of sample receipt

Step 1: Setting Up the Project

Open Visual Studio: Locate the Visual Studio icon on your desktop or in your applications menu and double-click it to open the program.

Create a New Project: Once Visual Studio is open, you’ll find a launch window. Click on the "Create a new project" button. If you have already opened Visual Studio and don’t see the launch window, you can access this by clicking File > New > Project from the top menu.

Select Project Type: In the “Create a new project” window, you’ll see a variety of project templates. In the search box, type “Console App” to filter the options, then select Console App (.NET Core) or Console App (.NET Framework), depending on your preference and compatibility. Then click the Next button.

Configure Your New Project: Now, you’ll see a screen titled "Configure your new project".

  • In the Project name field, enter OCRReceiptScanner as the name of your project.
  • Choose or confirm the location where your project will be saved in the location field.
  • Optionally, you can also specify a solution name if you want it to be different from the project name.
  • Click the Next button after filling in these details.

Additional Information: You might be asked to select the target .NET Framework. Choose the most recent version (unless you have specific compatibility requirements) and click Create.

Step 2: Integrating IronOCR

Before we can use the IronOCR library, we need to include it in our project. Follow these steps:

  1. Right-click on your project in the Solution Explorer.
  2. Choose "Manage NuGet Packages".
  3. In the NuGet Package Manager window, you will see several tabs like Browse, Installed, Updates, and Consolidate. Click on the Browse tab.
  4. In the search box, type IronOcr. This is the name of the library we wish to add to our project. Press enter to search.
  5. The search results will show the IronOCR library package. It should be one of the first results you see. Click on it to select it.
  6. After selecting the IronOCR package, you will notice a panel on the right side displaying the package's information, including its description and version. There is also an Install button in this panel.

    How To Create an OCR Receipt Scanner In C#: Figure 2 - Installing IronOCR through NuGet package manager

  7. Click the Install button. This action might prompt you to review changes and may show a list of dependencies that will be included along with IronOcr. Review the changes and dependencies, and if everything looks correct, confirm and proceed with the installation.

Step 3: Configuring the Project

After installing IronOCR, your next step is to configure your project. Here's how:

Add Namespaces: At the top of your Program.cs file, include the following namespaces:

using IronOcr;
using System;
using IronOcr;
using System;
Imports IronOcr
Imports System
$vbLabelText   $csharpLabel

Configuration Settings: If you have any configuration settings like an API key or a license key, make sure to include them. For IronOCR, you'll need to set the license key as shown in the provided code:

License.LicenseKey = "License-Key"; // replace 'License-Key' with your key
License.LicenseKey = "License-Key"; // replace 'License-Key' with your key
License.LicenseKey = "License-Key" ' replace 'License-Key' with your key
$vbLabelText   $csharpLabel

Step 4: Reading the Receipt

Now, let's write the code to read the receipt.

Define the Path to Your Receipt: Specify the path to the receipt file you want to scan.

string pdfFilePath = "Sample_Receipt.jpg";
string pdfFilePath = "Sample_Receipt.jpg";
Dim pdfFilePath As String = "Sample_Receipt.jpg"
$vbLabelText   $csharpLabel

Try-Catch Block: Implement error handling using a try-catch block. This will help you manage any exceptions that occur during the OCR process.

try
{
    // OCR code will go here
}
catch (Exception ex)
{
    // Handle exceptions here
    Console.WriteLine($"An error occurred: {ex.Message}");
}
try
{
    // OCR code will go here
}
catch (Exception ex)
{
    // Handle exceptions here
    Console.WriteLine($"An error occurred: {ex.Message}");
}
Try
	' OCR code will go here
Catch ex As Exception
	' Handle exceptions here
	Console.WriteLine($"An error occurred: {ex.Message}")
End Try
$vbLabelText   $csharpLabel

Step 5: Implementing OCR

In Step 5, we delve into the core functionality of our application: implementing OCR to read and interpret the data from our receipt. This involves initializing the OCR engine, configuring the input, performing the OCR operation, and displaying the results.

Initialize IronTesseract

The first part of the code creates an instance of the IronTesseract class:

var ocr = new IronTesseract();
var ocr = new IronTesseract();
Dim ocr = New IronTesseract()
$vbLabelText   $csharpLabel

By creating an instance of IronTesseract, we are essentially setting up our OCR tool, gearing it up to perform the text recognition tasks. It's like starting the engine of a car before you can drive it. This object will be used to control the OCR process, including reading the input and extracting text from it.

Configure OCR Input

Next, we define the input for our OCR process:

using (var input = new OcrInput(pdfFilePath))
{
    // OCR processing will go here
}
using (var input = new OcrInput(pdfFilePath))
{
    // OCR processing will go here
}
Using input = New OcrInput(pdfFilePath)
	' OCR processing will go here
End Using
$vbLabelText   $csharpLabel

In this segment, OcrInput is used to specify the file we want to process. pdfFilePath is a variable that contains the path to our receipt file. By passing this variable to OcrInput, we are telling the OCR engine, "Here's the file I want you to read." The using statement is a special C# construct that ensures that the resources used by OcrInput (like file handles) are properly released once the processing is done. It's a way to manage resources efficiently and ensure that your application runs smoothly without unnecessary memory usage.

Perform OCR

Within the using block, we call the Read method on our ocr instance:

var result = ocr.Read(input);
var result = ocr.Read(input);
Dim result = ocr.Read(input)
$vbLabelText   $csharpLabel

The Read method will take the input file path as the parameter. This line will start the receipt scanning. It'll do the OCR of the given input file, extract data, and store it in a variable result. We can use the extracted text from this method to perform any text operation.

Output the Results

Finally, we output the text that was recognized by the OCR process:

Console.WriteLine(result.Text);
Console.WriteLine(result.Text);
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

The result variable contains the output of the OCR process and result.Text contains the actual text extracted from the receipt. The Console.WriteLine function then takes this text and displays it on the console. This allows you to see and verify the results of the OCR process. Here is the complete Program.cs file code:

using IronOcr;
using System;

class Program
{
    static void Main(string[] args)
    {
        // Set your IronOCR license key
        License.LicenseKey = "Your-License-Key";

        // Define the path to the receipt image
        string pdfFilePath = "Sample_Receipt.jpg";

        try
        {
            // Initialize the OCR engine
            var ocr = new IronTesseract();

            // Define the input file
            using (var input = new OcrInput(pdfFilePath))
            {
                // Perform OCR and get the result
                var result = ocr.Read(input);

                // Display the extracted text
                Console.WriteLine(result.Text);
            }
        }
        catch (Exception ex)
        {
            // Handle exceptions and log them if necessary
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}
using IronOcr;
using System;

class Program
{
    static void Main(string[] args)
    {
        // Set your IronOCR license key
        License.LicenseKey = "Your-License-Key";

        // Define the path to the receipt image
        string pdfFilePath = "Sample_Receipt.jpg";

        try
        {
            // Initialize the OCR engine
            var ocr = new IronTesseract();

            // Define the input file
            using (var input = new OcrInput(pdfFilePath))
            {
                // Perform OCR and get the result
                var result = ocr.Read(input);

                // Display the extracted text
                Console.WriteLine(result.Text);
            }
        }
        catch (Exception ex)
        {
            // Handle exceptions and log them if necessary
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}
Imports IronOcr
Imports System

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Set your IronOCR license key
		License.LicenseKey = "Your-License-Key"

		' Define the path to the receipt image
		Dim pdfFilePath As String = "Sample_Receipt.jpg"

		Try
			' Initialize the OCR engine
			Dim ocr = New IronTesseract()

			' Define the input file
			Using input = New OcrInput(pdfFilePath)
				' Perform OCR and get the result
				Dim result = ocr.Read(input)

				' Display the extracted text
				Console.WriteLine(result.Text)
			End Using
		Catch ex As Exception
			' Handle exceptions and log them if necessary
			Console.WriteLine($"An error occurred: {ex.Message}")
		End Try
	End Sub
End Class
$vbLabelText   $csharpLabel

Step 6: Running Your Application

  1. Build the Project: Click on the 'Build' menu and then select 'Build Solution'.
  2. Run the Project: Press F5 or click on the 'Start' button to run your application.

Now, you see the text from your receipt output to the console. This text represents the data extracted from your receipt image. It's how we scan receipts using IronOCR. This is a simple example of using OCR capabilities to extract data from paper receipts. It's a very generic implementation. You can modify your code to match the layout of your receipt images.

How To Create an OCR Receipt Scanner In C#: Figure 3 - Outputted text from the previous code example

After that, you can use the unstructured data from receipts that we got after scanning receipts. We can get important information from a particular section of the receipt. Or we can show the receipt data in a more organized way. We can make an OCR Receipt Scanning software application using the IronOCR. That will help us to extract accurate data of receipt fields.

Conclusion

Congratulations! You've successfully built an OCR receipt scanner using C# and IronOCR. This scanner can significantly increase the accuracy of data extraction for various business needs such as expense tracking, supply chain management, and more. There will be no more need to review the scanned receipts and extract data manually.

IronOCR offers a free trial, allowing users to explore and assess its capabilities at no initial cost. For those seeking to integrate and leverage the full spectrum of features in a professional setting, licenses begin at $799, providing a comprehensive solution for robust OCR receipt scanning and data extraction needs.

Remember, this is just the beginning. You can expand this application to support various file types, improve data privacy, or integrate additional features like receipt recognition for specific fields such as tax amount, date, line items, and more. With OCR technology, the possibilities are vast, paving the way for more efficient and intelligent business processes. Happy coding!

Preguntas Frecuentes

¿Qué es un escáner de recibos OCR y cómo puede beneficiar a las empresas?

Un escáner de recibos OCR es una herramienta que utiliza tecnología de reconocimiento óptico de caracteres para convertir imágenes de recibos en datos editables y buscables. Esto puede mejorar significativamente los procesos empresariales al automatizar tareas de entrada de datos, especialmente en áreas como la gestión de gastos.

¿Cómo puedo crear un escáner de recibos OCR en C#?

Para crear un escáner de recibos OCR en C#, puedes usar la biblioteca IronOCR. Comienza configurando un proyecto de consola en C# en Visual Studio, instala IronOCR a través del Administrador de Paquetes NuGet, y sigue el tutorial para implementar la funcionalidad OCR en archivos de recibos.

¿Cuáles son los requisitos previos para configurar un escáner de recibos OCR en C#?

Los requisitos previos incluyen tener instalado Visual Studio, la biblioteca IronOCR, y un archivo de imagen de recibo de muestra para probar el proceso de OCR.

¿Cómo instalo la biblioteca IronOCR en mi proyecto C#?

Puedes instalar la biblioteca IronOCR usando el Administrador de Paquetes NuGet en Visual Studio. Busca IronOCR y agrégala a tu proyecto para acceder a sus funcionalidades de OCR.

¿Cómo funciona la clase IronTesseract en OCR?

La clase IronTesseract se utiliza para iniciar el motor de OCR dentro de la biblioteca IronOCR, permitiendo realizar tareas de reconocimiento de texto en imágenes de recibos.

¿Cómo manejo los errores durante el proceso de OCR en C#?

Los errores durante el proceso de OCR pueden manejarse utilizando un bloque try-catch en tu código C#. Esto ayudará a gestionar excepciones y a asegurar que la aplicación funcione sin problemas incluso cuando se encuentren problemas.

¿Cómo puedo mejorar las funciones de mi escáner de recibos OCR?

Puedes ampliar la aplicación soportando varios tipos de archivo, mejorando la privacidad de los datos o integrando características adicionales como reconocimiento específico de campos para recibos para mejorar la precisión de la extracción de datos.

¿Cuáles son las mejores prácticas para integrar tecnología OCR en aplicaciones C#?

Las mejores prácticas incluyen usar una biblioteca confiable como IronOCR, manejar errores con bloques try-catch y optimizar el escáner para múltiples formatos de recibos para asegurar una extracción precisa de datos.

¿Cómo puedo convertir una imagen de recibo a texto usando C#?

Puedes convertir una imagen de recibo a texto usando la biblioteca IronOCR en C#. Utiliza la clase OcrInput para especificar la imagen y luego procésala con la clase IronTesseract para extraer el texto.

¿Qué opciones de licencia están disponibles para IronOCR?

IronOCR ofrece una prueba gratuita para exploración, con opciones de licenciamiento asequibles para uso extendido en entornos profesionales, haciéndolo accesible para diversas aplicaciones que requieran tecnología OCR.

Kannaopat Udonpant
Ingeniero de Software
Antes de convertirse en Ingeniero de Software, Kannapat completó un doctorado en Recursos Ambientales de la Universidad de Hokkaido en Japón. Mientras perseguía su grado, Kannapat también se convirtió en miembro del Laboratorio de Robótica de Vehículos, que es parte del Departamento de Ingeniería ...
Leer más