如何使用 C# 中的 OCR 從收據中提取資料
IronOCR 提供強大的 C# 程式庫,利用先進的光學字元辨識(OCR)技術從收據影像中提取文字,支援 125 種語言與內建的影像預處理,實現自動化的支出跟蹤和資料分析。
收據與自動化
收據在當今快速發展的世界中至關重要。 無論是購買雜貨還是在外用餐,收據都有助於跟蹤支出並幫助制定預算。 同時,商店使用收據掃描器來分析銷售資料,幫助他們通過資料提取技術預測需求和管理庫存。
然而,收據可能難以閱讀,計算也不總是清晰的。 手動資料輸入到預算中乏味且易出錯,尤其是當涉及許多項目時。 丟失收據可能會使您每月的超支成為迷。 傳統紙質收據通常印刷質量差、墨水褪色,且熱感紙容易降解,這使得OCR 影像優化變得至關重要。
為了解決這個問題,預算和金融應用程式採用了OCR(光學字元辨識)技術。 通過將收據掃描成數位格式,OCR 減少錯誤、自動化資料輸入、跟蹤支出,並揭示購買模式。 現代的OCR 解決方案能處理各種收據格式,從傳統的銷售點列印到具有條碼和 QR 碼閱讀能力的數位收據。
OCR 利用機器學習來識別和提取影像中的文字。 該過程包括影像預處理、字元分割、模式識別和驗證。 然而,OCR 並不完美——模糊或污漬可能導致錯誤。 先進系統使用計算機視覺技術來提高準確性。 選擇一個可靠的 OCR 程式庫來高效處理和優化讀取是成功文件自動化的關鍵。
為什麼我應該選擇 IronOCR 進行收據處理?
IronOCR 是基於Tesseract OCR 引擎定制構建的 C# 程式庫。與標準 Tesseract 不同,IronOCR 包含了Tesseract 5 優化和專為 .NET 開發者設計的功能。 以下是其獨特之處:
-
跨相容性:支持 .NET 8、7、6、5 和 Framework 4.6.2+。 可在 Windows、macOS、Azure 和 Linux 上運行。 Deploys seamlessly to Docker, AWS Lambda, and Azure Functions.
-
靈活性和可擴展性:處理 JPG、PNG 和 GIF 格式。 與 System.Drawing 物件整合。 Processes multi-page TIFFs and PDF streams. 支持多執行緒以用於高容量場景。
-
易用性和支持:文件詳盡,擁有強大的 API 和 24/5 支持。 提供簡單的一行操作和詳細的配置選項。 包含全面的故障排除指南。
-
多語言功能:支持125 種國際語言。 有效識別產品名稱和價格。 處理文件中的多種語言。 支持客製化的traineddata 文件。
- 高級影像處理:內建濾鏡自動增強收據質量。 包含降噪、方向校正和DPI 優化。 濾鏡精靈自動確定最優設置。
如何在我的應用程式中實現收據 OCR?
開始需要什麼授權?
在使用 IronOCR 之前,您需要一個授權金鑰。 在這裡獲得免費試用。 授權選項包括 Lite、Plus 和 Professional 等級,以適應不同的團隊規模和部署。 請參閱文件以應用授權金鑰。
// Replace the license key variable with the trial key you obtained
IronOcr.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";
// Replace the license key variable with the trial key you obtained
IronOcr.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";
' Replace the license key variable with the trial key you obtained
IronOcr.License.LicenseKey = "REPLACE-WITH-YOUR-KEY"
對於網頁應用程式,在 Web.config 中設置授權金鑰以進行集中配置。 授權系統支持隨著您的成長而擴展和升級。
我如何使用 IronOCR 讀取超市收據?
讓我們來探討在應用程式中使用 IronOCR 掃描智慧型手機的超市收據,提取產品名稱和價格,並根據購買情況授予忠誠度積分。 這包括影像捕獲、預處理、OCR 執行和通過結果信心分數進行資料驗證。
典型的收據影像是什麼樣子的?

常見的收據挑戰包括熱感紙質量、不同的字體、擁擠的佈局,以及因折疊或潮濕造成的損壞。 IronOCR 的預處理通過影像質量校正和顏色校正技術來處理這些問題。
我需要什麼 C# 程式碼來提取收據資料?
using IronOcr;
class ReceiptScanner
{
static void Main()
{
// Set the license key for IronOCR
IronOcr.License.LicenseKey = "YOUR-KEY";
// Instantiate OCR engine with optimal settings for receipts
var ocr = new IronTesseract();
// Configure for receipt-specific text
ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.$,- ";
ocr.Configuration.BlackListCharacters = "~`@#%^*_+={}[]|\\:;\"'<>?";
using var inputPhoto = new OcrInput();
inputPhoto.LoadImage("supermarketexample.jpg");
// Apply preprocessing for better accuracy
inputPhoto.DeNoise();
inputPhoto.ToGrayScale();
inputPhoto.Contrast(1.2);
// Perform OCR on the loaded image
OcrResult result = ocr.Read(inputPhoto);
// Output the text extracted from the receipt
string text = result.Text;
Console.WriteLine(text);
// Extract specific data using OcrResult features
foreach (var line in result.Lines)
{
if (line.Text.Contains("TOTAL"))
{
Console.WriteLine($"Total Found: {line.Text}");
}
}
}
}
using IronOcr;
class ReceiptScanner
{
static void Main()
{
// Set the license key for IronOCR
IronOcr.License.LicenseKey = "YOUR-KEY";
// Instantiate OCR engine with optimal settings for receipts
var ocr = new IronTesseract();
// Configure for receipt-specific text
ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.$,- ";
ocr.Configuration.BlackListCharacters = "~`@#%^*_+={}[]|\\:;\"'<>?";
using var inputPhoto = new OcrInput();
inputPhoto.LoadImage("supermarketexample.jpg");
// Apply preprocessing for better accuracy
inputPhoto.DeNoise();
inputPhoto.ToGrayScale();
inputPhoto.Contrast(1.2);
// Perform OCR on the loaded image
OcrResult result = ocr.Read(inputPhoto);
// Output the text extracted from the receipt
string text = result.Text;
Console.WriteLine(text);
// Extract specific data using OcrResult features
foreach (var line in result.Lines)
{
if (line.Text.Contains("TOTAL"))
{
Console.WriteLine($"Total Found: {line.Text}");
}
}
}
}
Imports IronOcr
Class ReceiptScanner
Shared Sub Main()
' Set the license key for IronOCR
IronOcr.License.LicenseKey = "YOUR-KEY"
' Instantiate OCR engine with optimal settings for receipts
Dim ocr As New IronTesseract()
' Configure for receipt-specific text
ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.$,- "
ocr.Configuration.BlackListCharacters = "~`@#%^*_+={}[]|\:;""'<>?"
Using inputPhoto As New OcrInput()
inputPhoto.LoadImage("supermarketexample.jpg")
' Apply preprocessing for better accuracy
inputPhoto.DeNoise()
inputPhoto.ToGrayScale()
inputPhoto.Contrast(1.2)
' Perform OCR on the loaded image
Dim result As OcrResult = ocr.Read(inputPhoto)
' Output the text extracted from the receipt
Dim text As String = result.Text
Console.WriteLine(text)
' Extract specific data using OcrResult features
For Each line In result.Lines
If line.Text.Contains("TOTAL") Then
Console.WriteLine($"Total Found: {line.Text}")
End If
Next
End Using
End Sub
End Class
該程式碼展示了:
- 導入 IronOCR 程式庫。
- 使用配置選項實例化 OCR 引擎(
IronTesseract)。 - 建立新的 OcrInput 來載入收據影像。
- 應用預處理以提高準確性。
- 使用
Read方法提取文字。 - 使用 OcrResult 類處理結果以獲取結構化資料。
我如何驗證提取資料的準確性?
為了確保一致性,檢查提取資料的信心水準。 IronOCR 提供全面的信心指標,涵蓋多個層次:
OcrResult result = ocr.Read(inputPhoto);
string text = result.Text;
Console.WriteLine(text);
Console.WriteLine($"Overall Confidence: {result.Confidence}%");
// Check confidence for individual elements
foreach (var word in result.Words)
{
if (word.Confidence < 80)
{
Console.WriteLine($"Low confidence word: '{word.Text}' ({word.Confidence}%)");
}
}
// Validate numeric values
foreach (var block in result.Blocks)
{
if (block.Text.Contains("$"))
{
Console.WriteLine($"Price detected: {block.Text} (Confidence: {block.Confidence}%)");
}
}
OcrResult result = ocr.Read(inputPhoto);
string text = result.Text;
Console.WriteLine(text);
Console.WriteLine($"Overall Confidence: {result.Confidence}%");
// Check confidence for individual elements
foreach (var word in result.Words)
{
if (word.Confidence < 80)
{
Console.WriteLine($"Low confidence word: '{word.Text}' ({word.Confidence}%)");
}
}
// Validate numeric values
foreach (var block in result.Blocks)
{
if (block.Text.Contains("$"))
{
Console.WriteLine($"Price detected: {block.Text} (Confidence: {block.Confidence}%)");
}
}
Dim result As OcrResult = ocr.Read(inputPhoto)
Dim text As String = result.Text
Console.WriteLine(text)
Console.WriteLine($"Overall Confidence: {result.Confidence}%")
' Check confidence for individual elements
For Each word In result.Words
If word.Confidence < 80 Then
Console.WriteLine($"Low confidence word: '{word.Text}' ({word.Confidence}%)")
End If
Next
' Validate numeric values
For Each block In result.Blocks
If block.Text.Contains("$") Then
Console.WriteLine($"Price detected: {block.Text} (Confidence: {block.Confidence}%)")
End If
Next
Confidence 屬性測量從 0(低)到 100(高)的統計準確性。 使用這些信心水準來決定如何處理資料。 對於生產系統,實施進度跟蹤來監控 OCR 操作。
我如何通過影像預處理提高 OCR 的準確性?
在處理之前,使用這些方法來準備影像以獲得更好的結果:
using var inputPhoto = new OcrInput();
inputPhoto.LoadImage("receipt.jpg");
// Basic preprocessing
inputPhoto.DeNoise(); // Removes noise from the image
inputPhoto.ToGrayScale(); // Converts image to grayscale
inputPhoto.Contrast(1.5); // Enhance contrast for faded receipts
inputPhoto.Sharpen(); // Improve text clarity
// Advanced preprocessing for challenging receipts
inputPhoto.Rotate(2.5); // Correct slight rotation
inputPhoto.Deskew(); // Automatically straighten receipt
inputPhoto.Scale(200); // Upscale low-resolution images
// Handle specific receipt issues
if (receiptIsDamaged)
{
inputPhoto.Dilate(); // Thicken thin text
inputPhoto.Erode(); // Reduce text bleeding
}
// For colored or patterned backgrounds
inputPhoto.Binarize(); // Convert to pure black and white
inputPhoto.Invert(); // Handle white text on dark background
using var inputPhoto = new OcrInput();
inputPhoto.LoadImage("receipt.jpg");
// Basic preprocessing
inputPhoto.DeNoise(); // Removes noise from the image
inputPhoto.ToGrayScale(); // Converts image to grayscale
inputPhoto.Contrast(1.5); // Enhance contrast for faded receipts
inputPhoto.Sharpen(); // Improve text clarity
// Advanced preprocessing for challenging receipts
inputPhoto.Rotate(2.5); // Correct slight rotation
inputPhoto.Deskew(); // Automatically straighten receipt
inputPhoto.Scale(200); // Upscale low-resolution images
// Handle specific receipt issues
if (receiptIsDamaged)
{
inputPhoto.Dilate(); // Thicken thin text
inputPhoto.Erode(); // Reduce text bleeding
}
// For colored or patterned backgrounds
inputPhoto.Binarize(); // Convert to pure black and white
inputPhoto.Invert(); // Handle white text on dark background
使用 IronOCR 進行收據處理的關鍵好處是什麼?

收據 OCR 技術幫助企業和個人在預算、欺詐預防和自動化資料收集方面。 IronOCR 提供準確性、速度和易於與現有平台整合的優勢,使其成為收據掃描解決方案的理想選擇。
主要優勢包括:
常見問題
如何使用OCR技術自動化超市收據的處理?
OCR技術可以通過將掃描的收據轉換為數位資料來自動化超市收據的處理。使用IronOCR,可以自動讀取收據並提取文字,減少手動資料輸入的需要並最小化人為錯誤。
IronOCR在處理超市收據方面有什麼優勢?
IronOCR在處理超市收據方面提供了多項優勢,包括跨平台相容性、支持多種圖像格式、強大的API便於輕鬆整合,以及能處理多達125種語言,使之對處理國際收據非常理想。
如何在C#應用程式中整合IronOCR以讀取超市收據?
要在C#應用程式中整合IronOCR,您需要獲取授權金鑰,導入IronOcr程式庫,並使用IronTesseract引擎讀取和提取超市收據影像中的文字。
哪些預處理技術能改善收據掃描的OCR準確性?
IronOCR提供了DeNoise和ToGrayScale等預處理技術以改善OCR準確性。這些技術有助於去除影像噪音並將影像轉換為灰階,提升從收據中提取文字的效果。
為什麼信心測試在OCR中很重要,它是如何應用的?
IronOCR中的信心測試很重要,因為它測量了提取資料的準確性,其值範圍從0(低)到1(高)。它幫助使用者評估OCR結果的可靠性並通知資料處理決策。
IronOCR能否處理多語言的超市收據?
是的,IronOCR支持多達125種語言的OCR處理,能有效處理多語言的超市收據。
是否有IronOCR的試用版本供開發者使用?
是的,IronOCR提供免費試用版供開發者使用,以便在購買前探索其功能和能力。
IronOCR支持哪些平台進行收據掃描?
IronOCR相容.NET平台,包括 .NET 8、7、6、5和Framework 4.6.2及以上版本,支持在Windows、macOS、Azure和Linux環境中運行。
什麼使IronOCR適合整合到應用程式中的收據掃描功能?
IronOCR適合整合到應用程式中的收據掃描功能是因為其高準確性、使用方便、跨平台支持,並能無縫處理各種輸入格式和語言。



