
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中打開您的項目並按照以下步驟操作:
-
在解決方案資源管理器中右鍵點擊您的專案並選擇"管理NuGet包"。
-
在NuGet套件管理器視窗中,搜尋"IronOCR"。
-
選擇
IronOcr套件並點擊"安裝"。
在NuGet套件管理器UI中搜尋IronOcr套件
6. 準備收據圖像
在從收據中提取資料之前,您會希望確保收據圖像質量良好,以提高收據OCR API過程的準確性。 這是一些捕捉良好收據圖像的提示:
-
使用掃描文件。 您可以使用高解析度的掃描儀來掃描收據。
-
確保收據光線良好且無陰影。
-
拉直任何摺痕或折痕,避免關鍵資訊被遮住。
-
確保收據上的文字清晰且未被弄髒,以改善收據處理。
文字提取的樣例收據圖像
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);
}Imports IronOcr
' Initialize the IronTesseract class, which is responsible for OCR operations
Dim ocr As 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程式碼解釋
using IronOcr;Imports IronOcr這行將IronOCR程式庫導入到您的.NET應用中,允許您存取其功能。
var ocr = new IronTesseract();Dim ocr = New IronTesseract()這行建立了一個IronTesseract類的新實例,這是IronOCR中負責OCR操作的主要類別。
using (var ocrInput = new OcrInput(@"path/to/your/receipt/image.png"))Using ocrInput As New OcrInput("path/to/your/receipt/image.png")在這裡建立了OcrInput類的新實例,代表OCR過程的輸入圖像。 **"path/to/your/receipt/image.png"**應被替換為您收據圖像的實際文件路徑。 OcrInput實例的資源被正確釋放。
var result = ocr.Read(ocrInput);Dim result = ocr.Read(ocrInput)這行調用了OcrInput物件作為參數傳遞。 Read方法處理輸入圖像,並進行OCR操作,識別並提取圖像中的文字。 這將開始收據識別過程。
Console.WriteLine(result.Text);Console.WriteLine(result.Text)最後,這行將提取的文字輸出到控制台。 這個result物件,一個OcrResult類的實例,包含識別出的文字及關於OCR過程的附加訊息。 可以透過存取result物件的提取文字。
提取文字的輸出
微調OCR結果
IronOCR提供多種選項來改善OCR的準確性和性能。 這些選項包括對圖像進行預處理、調整OCR引擎設置以及為您的收據選擇合適的語言。
圖像預處理
您可以通過應用圖像預處理技術來增強OCR結果,如:
- 校正:修正圖像中的任何旋轉或傾斜。
- 去噪:通過移除圖片中的噪音來提高文字的可讀性。
這是如何應用這些技術的例子:
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語言選擇
IronOCR支持超過125種語言,選擇正確的收據語言可以顯著改善OCR結果。 要指定語言,請將以下行新增到您的程式碼中:
ocr.Configuration.Language = OcrLanguage.English;ocr.Configuration.Language = OcrLanguage.English從OCR結果中提取資料
隨著OCR過程的完成,是時候從文字中提取特定資訊了。 根據您的需求,您可能希望提取如:
- 商店名稱和地址。
- 購買的日期和時間。
- 商品名稱和價格。
- 小計、稅金和總額。
要做到這一點,您可以在您的.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);
}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您可以為需要從收據中提取的其他資訊建立類似的模式。
儲存和分析提取的資料
既然您從收據中提取了相關資訊,您可以將其儲存在資料庫中,進行分析或導出到其他文件格式,如CSV、JSON或Excel。
結論
總而言之,使用IronOCR進行收據OCR是一種創新的高效率方案,可以數字化和管理您的財務資料。 有了IronOCR,您可以替代手動資料輸入。 通過遵循這個逐步指南,您可以利用IronOCR的力量來改進您的支出跟踪和資料分析。 最好的部分是IronOCR提供免費試用,讓您無需承諾即可體驗其能力。
在試用期結束後,如果您決定繼續使用IronOCR,授權從$999開始,提供一種經濟實惠的方式來在您的應用中利用OCR技術的優勢。
相關文章


