如何在 C# 教學中從發票中提取文字
如何在 Tesseract 中進行 OCR 收據識別
- 探索功能豐富的 C# 庫,用於對收據進行 OCR 識別
- 使用 Tesseract 從收據中提取資料
- 在擷取的文字結果中搜尋特定資料
- 讀取輸入收據影像上的條碼值
1. IronOCR,一種光學字元辨識 API
IronOCR是一個 OCR 庫,可用於識別影像中的文字資料以進行資訊擷取,包括收據 OCR。 它基於 Tesseract OCR 引擎構建,該引擎被認為是目前最精確的收據識別 OCR 引擎之一。 IronOCR 可以讀取不同文件類型(包括 PNG、JPG、TIFF、JSON 和 PDF 格式)中的關鍵訊息,並且可以識別多種語言的文字。
IronOCR 的關鍵特性使其特別適用於收據 OCR,即它能夠自動偵測文字方向,即使影像已旋轉或傾斜。 這對於收據上傳和資料提取的準確文字識別至關重要,因為收據通常包含大量資訊,並且可能會折疊或揉皺,導致文字變形。
2. 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 視窗底部將出現一個新的控制台。 在控制台中輸入以下命令並按下回車鍵。
Install-Package IronOcr
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);
}
}
}
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);
}
}
}
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);
}
}
}
}
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
Friend Class Program
Shared Sub Main()
Dim ocrTesseract = New IronTesseract()
ocrTesseract.Configuration.ReadBarCodes = True
' Load the receipt image with a barcode
Using ocrInput As 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 barcode
End Using
End Sub
End Class
輸入影像
輸出文字
8.結論
上面的文章解釋瞭如何在 C# 專案中安裝和使用 IronOCR 來從收據中提取資料,並提供了範例程式碼片段。
請閱讀關於從圖像中讀取文字的教程。
IronOCR 是Iron Suite的一部分,Iron Suite 包含五個不同的 .NET 庫,用於處理文件和圖像。 您只需支付兩份IronOCR 授權的價格,即可購買整個 Iron Suite 套件。
免費試用IronOCR,即可在您的生產應用程式中使用。
常見問題解答
如何使用 IronOCR 在 C# 中對收據圖像執行 OCR?
您可以使用 IronOCR 通過將圖像加載到 OcrInput 類並調用 Read 方法來執行收據圖像的 OCR,以提取文本數據,例如項目列表和總金額。
使用 IronOCR 比 Tesseract 處理發票有什麼優勢?
IronOCR 提供了增強的準確性,支持超過 125 種語言,並包括自動文本方向檢測和深度學習功能。 使用 NuGet 包管理器將其集成到 C# 項目中也更加簡單。
如何將 IronOCR 集成到 Visual Studio 項目中?
要將 IronOCR 集成到 Visual Studio 項目中,使用 NuGet 包管理器。瀏覽到工具 > NuGet 包管理器 > 包管理器控制台,然後執行 Install-Package IronOcr 以將庫添加到您的項目中。
IronOCR 能處理多語言的收據 OCR 嗎?
是的,IronOCR 可以處理多語言,支持超過 125 種全球語言,這使得它在處理多語言文本收據時非常理想。
IronOCR 如何提高收據中文本識別的準確性?
IronOCR 通過深度學習、自動文本方向檢測以及使用 OcrInput 類來優化圖像以獲得更好的 OCR 結果來提高文本識別準確性。
是否可以使用 IronOCR 從收據中提取項目列表?
是的,IronOCR 可以通過處理文本數據和在執行 OCR 之後通過模式匹配識別行項目以提取收據中的項目列表。
IronOCR 如何處理收據上的條碼讀取?
IronOCR 使用 BarcodeReader 類和 ReadBarCodes 方法來掃描和解碼收據上的條碼。
IronOCR 可以處理哪些格式的文件來進行收據 OCR?
IronOCR 可以處理包括 PNG、JPG、TIFF 和 PDF 在內的各種文件格式,以便對不同的輸入類型進行靈活的處理。
在 C# 中設置 IronOCR 用於發票處理涉及哪些步驟?
設置 IronOCR 用於發票處理涉及通過 NuGet 安裝庫,配置 OcrInput 與收據圖像,並使用 Read 方法提取文本數據。 您也可以使用庫的功能來提高準確性並提取具體數據,例如總金額。

