
如何在 C# 教學中從發票中提取文字
如何在Tesseract中OCR掃描收據
- 安裝具備Tesseract的C#庫OCR掃描收據
- 探索功能豐富的C#庫以對收據進行OCR
- 使用Tesseract從收據中提取資料
- 在提取的文字結果中搜尋特定資料
- 讀取輸入收據圖像上的條碼值
1. IronOCR,一個光學字元識別API
IronOCR是一個OCR庫,可以用於從圖像中識別文字資料進行資訊提取,包括收據OCR。 它是基於Tesseract OCR引擎構建的,Tesseract被認為是至今最準確的收據識別OCR引擎之一。 IronOCR可以從不同的文件型別中讀取關鍵資訊,包括PNG、JPG、TIFF、JSON和PDF格式,並且可以識別多種語言的文字。
IronOCR的一個關鍵特點是它可以自動檢測文字方向,即使圖像已旋轉或傾斜。 這對於正確識別收據上傳的文字和資料提取至關重要,因為收據通常包含大量資訊且可能被折疊或弄皺,導致文字傾斜。
2. IronOCR功能
- C# OCR使用深度學習來掃描並識別圖片、掃描文件和PDF中的文字。
- .NET OCR支援超過125種全球語言。
- IronOCR可以從多個文件格式的圖像中讀取文字,包括PNG、JPG、TIFF和PDF。
- 可以從提取的資訊中生成文字、結構化資料、JSON輸出或可搜尋的PDF。
- IronOCR支援.NET版本5、6和7(Core、Framework和Standard)。
- IronOCR將輸入根據文字區域劃分為不同的圖片。 它使用計算機視覺識別包含文字元素的區域。
3. 在Visual Studio中建立新專案
打開Visual Studio並進入文件選單。 選擇"新專案",然後選擇控制台應用程式。
輸入專案名稱並在適當的文字框中選擇路徑。 然後,點擊建立按鈕。 選擇所需的.NET Framework,如下圖所示:

控制台應用程式的專案結構將被生成。 完成後,將打開Program.cs文件,您可以在其中撰寫和執行源程式碼。

4. 安裝IronOCR
在Visual Studio中,您可以輕鬆將IronOCR整合到您的C#專案中。 IronOCR提供了多種與C# .NET專案整合的方法。 在這裡,我們將討論其中之一:使用NuGet包管理器安裝IronOCR。
在Visual Studio中,前往工具 > NuGet包管理器 > 包管理器控制台

一個新的控制台會出現在Visual Studio窗口的底部。 在控制台中輸入以下命令並按回車。
IronOCR將在幾秒鐘內安裝完成。
5. 使用IronOCR從收據中提取資料
IronOCR是一個強大的OCR庫,可以用於從收據中提取和存取詳細資料。使用IronOCR,您可以將收據的圖片轉換為機器可讀的文字,該文字可以輕鬆分析和處理而不損害資料隱私。
以下是一個如何使用IronOCR從收據中提取文字的範例:
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請參閱從圖片讀取文字教程以獲取更多有關IronOCR如何使用C#從圖像讀取文字的詳細資訊。
上述程式碼的輸出:
- 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. 使用IronOCR從收據圖像中提取特定資料
IronOCR允許開發者從掃描的收據中提取重要資訊,例如稅額和商家名稱。
以下是一個說明如何從收據圖像中提取總金額數值的範例:
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感謝OcrInput類提供的多種設置,可以優化輸入圖像以提高OCR過程的準確性。
輸入

輸出
- Total 16.5
7. 讀取收據上的條碼
IronOCR可以用於讀取收據上的條碼以及文字。 要讀取收據上的條碼,您需要使用BarcodeReader類與ReadBarCodes方法結合。
以下是如何讀取條碼的範例:
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
Class Program
Shared Sub Main()
Dim ocrTesseract = New IronTesseract()
ocrTesseract.Configuration.ReadBarCodes = True
' Load the receipt image with a barcode
Using ocrInput = 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
End Using
End Sub
End Class輸入圖像

輸出文字

8. 結論
上述文章解釋了將IronOCR安裝和使用到C#專案中以從收據中提取資料的過程,並提供了範例程式碼片段。
請閱讀從圖片讀取文字的教程。
IronOCR是Iron Suite的一部分,其中包括五個不同的.NET庫,用於操作文件和圖像。 您可以購買整個Iron Suite,價格僅為兩個IronOCR授權。
在您的生產應用程式中試用IronOCR,享受免費試用。
相關文章


