跳過到頁腳內容
使用 IRONOCR

OCR 收據數據提取(逐步教學)

IronOCR 的收據 OCR 功能對企業和個人來說都是一項顛覆性的創新。它能夠從紙質收據中提取重要訊息,並將其轉換為數位資料。 本文將一步步指導您如何使用 IronOCR 來充分利用您的收據。

OCR快速入門

光學字元辨識(OCR)是一種使電腦能夠讀取和理解圖像或掃描文件中的文字的技術。 OCR 透過將印刷文本轉換為機器可讀文本,使您能夠儲存、處理和分析實體文件中包含的資訊。

2. IronOCR簡介

IronOCR是一個針對 C# 和 .NET 開發人員的 OCR(光學字元辨識)函式庫。 它使開發人員能夠從圖像PDF和其他文件格式中提取文字。 IronOCR 基於流行的Tesseract OCR 引擎構建,並增加了額外的功能,使其成為各種應用的理想選擇,包括收據 OCR。

3. 使用 IronOCR 進行資料擷取的優勢

以下是使用 IronOCR 進行 OCR 收據資料擷取的一些主要優點:

-高精度:IronOCR 提供卓越的 OCR API 精度,確保從收據和其他文件中可靠地提取資料。 -多語言支援:IronOCR 支援超過 125 種語言,使其適用於全球應用。 -易於使用:該程式庫提供了一個簡單直觀的 API,使開發人員能夠輕鬆地在其專案中實現 OCR 功能。 -可自訂:IronOCR提供各種選項來微調 OCR 結果,確保針對您的特定用例進行最佳資料擷取。

4. IronOCR 的工作原理

IronOCR採用先進的OCR演算法來辨識和擷取影像和文件中的文字。 它可以處理多種格式,包括 JPEG、PNG、TIFF 和 PDF。 該庫讀取輸入文件,識別其中的文本,並將提取的文本作為字串輸出,然後可以根據需要進行處理或儲存。 IronOCR 也利用電腦視覺技術來獲得最佳效果。

5. 使用 IronOCR 的前提條件

要開始使用 IronOCR 提取收據數據,您首先需要安裝 IronOCR 軟體套件。 這可以透過 .NET 的套件管理器 NuGet 輕鬆完成。 只需在 Visual Studio 中開啟您的項目,然後按照以下步驟操作:

  1. 在解決方案資源管理器中以滑鼠右鍵按一下您的項目,然後選擇"管理 NuGet 套件"。
  2. 在 NuGet 套件管理器視窗中,搜尋"IronOCR"。
  3. 選擇IronOcr軟體包,然後按一下"安裝"。

    OCR 收據資料擷取(逐步教學),圖 1:在 NuGet 套件管理器 UI 中搜尋 IronOcr 套件 在 NuGet 套件管理器 UI 中搜尋IronOcr

6. 準備收據影像

在從收據中提取資料之前,您需要確保收據影像品質高,以提高收據 OCR API 流程的準確性。 以下是一些拍攝清晰收據照片的小技巧:

  1. 使用掃描文件。 您可以使用高解析度掃描器掃描收據。
  2. 確保收據光線充足,沒有陰影。
  3. 將收據上的所有摺痕或褶皺撫平,以免關鍵訊息被遮蔽。
  4. 確保收據上的文字清晰無污跡,以便更好地處理收據。

    OCR收據資料擷取(逐步教學),圖2:用於文字擷取的收據圖像範例 用於文字擷取的收據樣本圖像

7. 對收據影像進行OCR識別

IronOCR 安裝完畢,收據影像也準備就緒,現在可以執行 OCR 流程了。 在您的 .NET 應用程式中,使用以下程式碼片段:

using IronOcr;

// Initialize the IronTesseract class, which is responsible for OCR operations
var ocr = new IronTesseract();

// Use the OcrInput class to load the image of your receipt.
// Replace @"path/to/your/receipt/image.png" with the actual file path.
using (var ocrInput = new OcrInput(@"path/to/your/receipt/image.png"))
{
    // Read the content of the image and perform OCR recognition
    var result = ocr.Read(ocrInput);

    // Output the recognized text to the console
    Console.WriteLine(result.Text);
}
using IronOcr;

// Initialize the IronTesseract class, which is responsible for OCR operations
var ocr = new IronTesseract();

// Use the OcrInput class to load the image of your receipt.
// Replace @"path/to/your/receipt/image.png" with the actual file path.
using (var ocrInput = new OcrInput(@"path/to/your/receipt/image.png"))
{
    // Read the content of the image and perform OCR recognition
    var result = ocr.Read(ocrInput);

    // Output the recognized text to the console
    Console.WriteLine(result.Text);
}
Imports IronOcr

' Initialize the IronTesseract class, which is responsible for OCR operations
Private ocr = New IronTesseract()

' Use the OcrInput class to load the image of your receipt.
' Replace @"path/to/your/receipt/image.png" with the actual file path.
Using ocrInput As New OcrInput("path/to/your/receipt/image.png")
	' Read the content of the image and perform OCR recognition
	Dim result = ocr.Read(ocrInput)

	' Output the recognized text to the console
	Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

程式碼說明

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

這行程式碼將 IronOCR 庫匯入到您的 .NET 應用程式中,使您能夠存取其功能。

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

這一行建立了IronTesseract類別的一個新實例,IronTesseract 類別是 IronOCR 中負責 OCR 操作的主要類別。

using (var ocrInput = new OcrInput(@"path/to/your/receipt/image.png"))
using (var ocrInput = new OcrInput(@"path/to/your/receipt/image.png"))
Using ocrInput As New OcrInput("path/to/your/receipt/image.png")
$vbLabelText   $csharpLabel

在這裡,創建了OcrInput類別的一個新實例,它代表 OCR 過程的輸入圖像。 @"path/to/your/receipt/image.png"應替換為您的收據圖片的實際文件路徑。 using語句確保在 OCR 操作完成後,指派給OcrInput實例的資源能夠被正確釋放。

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

這行程式碼呼叫IronTesseract實例的Read方法,並將OcrInput物件作為參數傳遞。 Read方法處理輸入影像並執行 OCR 操作,識別和提取影像中的文字。 它將啟動收據識別流程。

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

最後,這行程式碼將提取的文字輸出到控制台。 result物件是OcrResult類別的一個實例,其中包含識別出的文字和有關 OCR 過程的附加資訊。 可以透過存取result物件的Text屬性來顯示擷取的文字。

OCR收據資料擷取(逐步教學),圖3:擷取文字的輸出 提取文字的輸出

微調 OCR 結果

IronOCR 提供多種選項來提高 OCR 的準確性和性能。 這些步驟包括影像預處理、調整 OCR 引擎設定以及選擇收據的合適語言。

影像預處理

您可以透過應用影像預處理技術來提高 OCR 結果,例如:

  1. 校正傾斜:校正影像中的任何旋轉或傾斜。
  2. 降噪:透過去除圖片中的雜訊來提高文字的可讀性。

以下是一個應用這些技巧的範例:

using IronOcr;

// Initialize the IronTesseract class
var ocr = new IronTesseract();

// Load the image of your receipt and apply preprocessing techniques
using (var input = new OcrInput(@"path/to/your/receipt/image.png"))
{
    input.DeNoise(); // Remove noise from the image
    input.DeSkew();  // Correct any skewing in the image

    // Perform OCR and extract the recognized text
    var result = ocr.Read(input);
    Console.WriteLine(result.Text);
}
using IronOcr;

// Initialize the IronTesseract class
var ocr = new IronTesseract();

// Load the image of your receipt and apply preprocessing techniques
using (var input = new OcrInput(@"path/to/your/receipt/image.png"))
{
    input.DeNoise(); // Remove noise from the image
    input.DeSkew();  // Correct any skewing in the image

    // Perform OCR and extract the recognized text
    var result = ocr.Read(input);
    Console.WriteLine(result.Text);
}
Imports IronOcr

' Initialize the IronTesseract class
Private ocr = New IronTesseract()

' Load the image of your receipt and apply preprocessing techniques
Using input = New OcrInput("path/to/your/receipt/image.png")
	input.DeNoise() ' Remove noise from the image
	input.DeSkew() ' Correct any skewing in the image

	' Perform OCR and extract the recognized text
	Dim result = ocr.Read(input)
	Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

語言選擇

IronOCR 支援 125 多種語言,選擇正確的收據語言可以顯著提高 OCR 結果。 若要指定語言,請在程式碼中新增以下行:

ocr.Configuration.Language = OcrLanguage.English;
ocr.Configuration.Language = OcrLanguage.English;
ocr.Configuration.Language = OcrLanguage.English
$vbLabelText   $csharpLabel

從 OCR 結果中提取數據

OCR 過程完成後,就可以從文字中提取特定資訊了。 根據您的需求,您可能需要提取以下數據:

  1. 商店名稱和地址。
  2. 購買日期和時間。
  3. 商品名稱和價格。
  4. 小計、稅額和總金額。

為此,您可以在 .NET 應用程式中使用正規表示式或字串操作技術。 例如,您可以使用以下程式碼片段從 OCR 結果中提取日期:

using System;
using System.Text.RegularExpressions;

// Define a regular expression pattern for matching dates
var datePattern = @"\d{1,2}\/\d{1,2}\/\d{2,4}";

// Search for a date in the OCR result text
var dateMatch = Regex.Match(result.Text, datePattern);
if (dateMatch.Success)
{
    // Parse the matched date string into a DateTime object
    var dateValue = DateTime.Parse(dateMatch.Value);
    Console.WriteLine("Date: " + dateValue);
}
using System;
using System.Text.RegularExpressions;

// Define a regular expression pattern for matching dates
var datePattern = @"\d{1,2}\/\d{1,2}\/\d{2,4}";

// Search for a date in the OCR result text
var dateMatch = Regex.Match(result.Text, datePattern);
if (dateMatch.Success)
{
    // Parse the matched date string into a DateTime object
    var dateValue = DateTime.Parse(dateMatch.Value);
    Console.WriteLine("Date: " + dateValue);
}
Imports System
Imports System.Text.RegularExpressions

' Define a regular expression pattern for matching dates
Private datePattern = "\d{1,2}\/\d{1,2}\/\d{2,4}"

' Search for a date in the OCR result text
Private dateMatch = Regex.Match(result.Text, datePattern)
If dateMatch.Success Then
	' Parse the matched date string into a DateTime object
	Dim dateValue = DateTime.Parse(dateMatch.Value)
	Console.WriteLine("Date: " & dateValue)
End If
$vbLabelText   $csharpLabel

您可以建立類似的模式,用於從收據中提取其他需要的資訊。

儲存和分析提取的數據

現在您已經從收據中提取了相關信息,您可以將其儲存在資料庫中、進行分析,或將其匯出為其他文件格式,例如 CSV、JSON 或 Excel。

結論

總而言之,使用 IronOCR 進行收據 OCR 識別是一種創新且高效的解決方案,可用於數位化和管理您的財務資料。 使用 IronOCR,您可以取代手動資料輸入。 透過遵循本逐步指南,您可以利用 IronOCR 的強大功能來改善您的費用追蹤和數據分析。 最棒的是,IronOCR 提供免費試用,讓您無需任何承諾即可體驗其功能。

試用期結束後,如果您決定繼續使用 IronOCR,許可證起價為$799 ,這是一種經濟高效的方式,可以在您的應用程式中利用 OCR 技術的優勢。

常見問題解答

如何使用 C# 將收據影像轉換為數位文字?

您可以使用 IronOCR 將收據影像轉換為數位文字,方法是初始化 IronTesseract 類,使用 OcrInput 載入影像,並呼叫 Read 方法來擷取文字。

什麼是光學字元識別,以及它如何用於收據?

光學字元識別 (OCR) 是一種將影像或掃描文件中的文字轉換為機器可讀資料的技術。它透過掃描印刷品並將其轉換為可使用 IronOcr 儲存和分析的文字,從而對收據發揮作用。

如何提高收據影像的 OCR 結果品質?

要改善 OCR 結果,可透過確保接收影像的高品質、使用影像預處理技術 (如抗色偏和去噪),以及在 IronOCR 中選擇正確的語言設定來達成。

使用 C# OCR 函式庫進行收據資料擷取的優點為何?

使用 IronOCR 之類的 C# OCR 函式庫,可提高收據資料擷取的精確度,支援超過 125 種語言,並提供客製化選項,因此可輕鬆整合至 .NET 專案中。

如何將擷取的收據資料用於報告和分析?

擷取的收據資料可儲存於資料庫或匯出為 CSV、JSON 或 Excel 等格式,以便進一步處理、報告及分析。

在 .NET 環境中安裝 OCR 函式庫的步驟是什麼?

要在 .NET 環境中安裝 IronOCR,請開啟 Visual Studio,導覽到「管理 NuGet 套件」,搜尋「IronOCR」,然後將其安裝到您的專案中。

如何從收據的 OCR 輸出中擷取特定資料?

透過使用正則表達式或字串處理來解析商店名稱、購買日期和商品價格等資訊,即可從 OCR 輸出中擷取特定資料。

收據資料 OCR 有哪些常見的挑戰,該如何解決?

常見的挑戰包括影像品質不佳和複雜的收據佈局。這些問題可以透過改善影像品質、使用預先處理技術,以及利用 IronOcr 中的客製化選項來解決。

IronOCR 是否提供收據 OCR 的多語言支援?

是的,IronOCR 為收據 OCR 提供多語支援,使其能辨識和處理超過 125 種語言的文字,增強其在全球應用上的實用性。

C# OCR 函式庫是否有試用版本,以及授權選項為何?

IronOCR 提供免費試用版本,讓使用者探索其功能。試用版之後,有多種 License 選項可供選擇,首先是經濟實惠的 Lite 版本。

Kannaopat Udonpant
軟體工程師
在成為软件工程師之前,Kannapat 從日本北海道大學完成了環境資源博士學位。在追逐學位期间,Kannapat 還成為了生產工程系一部份——汽車机器人实验室的成員。2022 年,他利用他的 C# 技能加入 Iron Software 的工程團隊, 專注於 IronPDF。Kannapat 珍惜他的工作,因为他直接向编写大部分 IronPDF 使用的代码的开发者学习。除了同行学习,Kannapat 还喜欢在 Iron Software 工作的社交十环。当他不编写代码或文档时,Kannapat 通常在他的 PS5 上打游戏或重看《The Last of Us》。