跳至頁尾內容
USING IRONOCR

如何在 C# 教學中從發票中提取文字

如何在Tesseract中OCR掃描收據

  1. 安裝具備Tesseract的C#庫OCR掃描收據
  2. 探索功能豐富的C#庫以對收據進行OCR
  3. 使用Tesseract從收據中提取資料
  4. 在提取的文字結果中搜尋特定資料
  5. 讀取輸入收據圖像上的條碼值

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,如下圖所示:

在Visual Studio中建立一個新專案

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

由Visual Studio新專案向導生成的Program.cs文件

4. 安裝IronOCR

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

在Visual Studio中,前往工具 > NuGet包管理器 > 包管理器控制台

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
$vbLabelText   $csharpLabel

請參閱從圖片讀取文字教程以獲取更多有關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
$vbLabelText   $csharpLabel

感謝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
$vbLabelText   $csharpLabel

輸入圖像

讀取條碼的輸入

輸出文字

處理條碼圖像的結果

8. 結論

上述文章解釋了將IronOCR安裝和使用到C#專案中以從收據中提取資料的過程,並提供了範例程式碼片段。

請閱讀從圖片讀取文字的教程。

IronOCR是Iron Suite的一部分,其中包括五個不同的.NET庫,用於操作文件和圖像。 您可以購買整個Iron Suite,價格僅為兩個IronOCR授權

在您的生產應用程式中試用IronOCR,享受免費試用

常見問題

如何使用IronOCR在C#中對收據圖像進行OCR?

您可以使用IronOCR通過將圖像載入到OcrInput類中並調用Read方法來提取文字資料,如分項列表和總金額。

使用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可以處理多種文件格式進行收據OCR,包括PNG、JPG、TIFF和PDF,這使其對不同型別的輸入具有靈活性。

設置IronOCR進行C#中的發票處理涉及哪些步驟?

設置IronOCR進行發票處理涉及通過NuGet安裝程式庫,將OcrInput與收據圖像進行配置,並使用Read方法提取文字資料。您還可以使用程式庫的功能來提高準確性並提取總金額等特定資料。

Kannaopat Udonpant
軟體工程師
在成為軟體工程師之前,Kannapat在日本北海道大學完成了環境資源博士學位。在攻讀學位期間,Kannapat還成為車輛機器人實驗室的一員,該實驗室隸屬於生產工程系。在2022年,他憑藉C#技能加入了Iron Software的工程團隊,專注於IronPDF。Kannapat珍視他的工作,因為他能直接向撰寫大部分IronPDF程式碼的開發者學習。除了同儕學習,Kannapat還喜歡在Iron Software工作的社交方面。不寫程式碼或文件時,Kannapat通常在他的PS5上玩遊戲或重看The Last of Us。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話