如何在 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.006. 使用 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.57. 閱讀收據上的條碼
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 在收據影像上執行 OCR,方法是將該影像載入 OcrInput 類別,並呼叫 Read 方法來擷取文字資料,例如分項清單和總金額。
與 Tesseract 相比,使用 IronOCR 處理發票有哪些優勢?
IronOCR 提供更高的精確度,支援超過 125 種語言,並包含自動文字方向偵測和深度學習功能等特色。它也更容易使用 NuGet Package Manager 與 C# 專案整合。
如何將 IronOCR 整合到 Visual Studio 專案中?
要將 IronOCR 整合到 Visual Studio 專案中,請使用 NuGet 套件管理員。導覽到工具 > NuGet 套件管理員 > 套件管理員控制台,然後執行 Install-Package IronOcr 以將函式庫新增至您的專案。
IronOCR 可以在收據 OCR 中處理多種語言嗎?
是的,IronOCR 可以處理多種語言,支援超過 125 種全球語言,因此非常適合處理包含多種語言文字的收據。
IronOCR 如何提高收據中的文字辨識準確度?
IronOCR 透過深度學習、自動偵測文字方向,以及使用 OcrInput 類別優化影像以獲得更好的 OCR 結果等功能,提高文字辨識的準確度。
是否可以使用 IronOCR 從收據中提取分項清單?
是的,IronOCR 可用於從收據中擷取分項清單,方法是在執行 OCR 之後,處理文字資料並透過模式匹配識別行列項目。
IronOCR 如何處理收據上的 BarCode 讀取?
IronOCR 使用 BarcodeReader 類和 ReadBarCodes 方法掃描和解碼收據上的條碼,從而處理條碼讀取。
IronOCR 可以處理哪些檔案格式的收據 OCR?
IronOcr 可處理多種檔案格式的收據 OCR,包括 PNG、JPG、TIFF 和 PDF,使其適用於不同的輸入類型。
在 C# 中設定 IronOCR 進行發票處理需要哪些步驟?
設定 IronOCR 以進行發票處理,包括透過 NuGet 安裝函式庫,以收據影像設定 OcrInput,並使用 Read 方法擷取文字資料。您也可以使用函式庫的功能來提高精確度,並擷取特定資料,例如總計。







