跳過到頁腳內容
使用 IRONOCR

發票 OCR API 開發者教學

發票 OCR API 利用機器學習和電腦視覺技術,將發票資料轉換為適合自動化處理的格式。 這項技術解決了人工資料輸入的延遲、成本和錯誤等問題,能夠準確地從電子發票和掃描發票中提取供應商資訊、發票號碼和價格等詳細資訊。

本文將使用名為IronOCR 的頂級發票 OCR API。

1. IronOCR

IronOCR 由 Iron Software 開發,是一個 OCR 函式庫,為開發人員提供一系列工具。 它利用機器學習和電腦視覺技術從掃描文件、圖像和 PDF 中提取文本,以實現自動化處理。 其 API 可整合到各種語言和平台中,減少人工資料輸入錯誤,提高效率。 提取的數據可以進行分析並整合到現有系統中,從而幫助決策和提高生產力。 影像預處理條碼識別和檔案解析等功能增強了其多功能性。 IronOCR 使開發人員能夠將文字辨識功能整合到他們的應用程式中。

2. 先決條件

在使用 IronOCR 之前,需要滿足一些先決條件。 這些先決條件包括:

  1. 確保您的電腦上已設定合適的開發環境。 這通常需要安裝整合開發環境(IDE),例如 Visual Studio。
  2. 對 C# 程式語言有基本的了解很重要。 這將使您能夠有效地理解和修改文章中提供的程式碼範例。
  3. 你需要將 IronOCR 庫安裝到你的專案中。 這可以透過在 Visual Studio 中使用 NuGet 套件管理器或透過命令列介面來實現。

確保滿足這些先決條件後,您就可以開始使用 IronOCR 了。

3. 建立新的 Visual Studio 項目

要開始使用 IronOCR,第一步是建立一個新的 Visual Studio 專案。

開啟 Visual Studio,轉到"檔案",然後將滑鼠懸停在"新建"上,然後按一下"專案"。

發票 OCR API(開發者教學):圖 1 - 新建項目 新專案

在新視窗中,選擇"控制台應用程式",然後按一下"下一步"。

發票 OCR API(開發者教學):圖 2 - 控制台應用程式 控制台應用程式

將出現一個新窗口,輸入新項目的名稱和位置,然後按一下"下一步"。

發票 OCR API(開發者教學):圖 3 - 專案配置 專案配置

最後,提供目標框架並點擊"建立"。

發票 OCR API(開發者教學):圖 4 - 目標框架 目標框架

現在,您的新 Visual Studio 專案已建立完成。 我們來安裝IronOCR。

4. 安裝 IronOCR

下載和安裝 IronOCR 庫有多種方法。 但以下是兩種最簡單的方法。

  1. 使用 Visual Studio NuGet 套件管理器
  2. 使用 Visual Studio 命令列

4.1. 使用 Visual Studio NuGet 套件管理器

可以透過使用 Visual Studio NuGet 套件管理器將 IronOCR 整合到 C# 專案中。

透過選擇"工具" > "NuGet 套件管理器" > "管理解決方案的 NuGet 套件",導覽至 NuGet 套件管理器圖形使用者介面。

發票 OCR API(開發者教學):圖 5 - NuGet 套件管理器 NuGet 套件管理器

之後,將出現一個新視窗。 搜尋 IronOCR 並將其安裝到專案中。

發票 OCR API(開發者教學):圖 6 - 在 NuGet 套件管理器 UI 中選擇 IronOCR 套件 在 NuGet 套件管理器介面中選擇 IronOCR 套件。

也可以使用上述相同的方法安裝 IronOCR 的其他語言套件。

4.2. 使用 Visual Studio 命令列

  1. 在 Visual Studio 中,前往"工具" > "NuGet 套件管理器" > "套件管理器控制台"。
  2. 在軟體包管理器控制台標籤中輸入以下命令以安裝 IronOCR:

    Install-Package IronOcr

發票 OCR API(開發者教學):圖 7 - 套件管理器控制台 軟體包管理器控制台

該套件現在會下載/安裝到目前的專案中,並可立即使用。

5. 使用 IronOCR 從發票中提取數據

使用 IronOCR,只需幾行代碼即可輕鬆從發票中提取數據,並將提取的數據用於數據輸入等後續流程。 這將取代人工資料輸入等多種工作。

以下是一個可供提取文字的範例發票。

發票 OCR API(開發者教學):圖 8 - 範例發票 發票範本

現在,讓我們編寫程式碼來提取這張發票中的所有資料。

using IronOcr;
using System;

// Initialize a new instance of the IronTesseract class
var ocr = new IronTesseract();

// Use the OcrInput object to load the image file
using (var input = new OcrInput(@"r2.png"))
{
    // Read the image using the Read method, which performs OCR
    var result = ocr.Read(input);

    // Output the extracted text to the console
    Console.WriteLine(result.Text);
}
using IronOcr;
using System;

// Initialize a new instance of the IronTesseract class
var ocr = new IronTesseract();

// Use the OcrInput object to load the image file
using (var input = new OcrInput(@"r2.png"))
{
    // Read the image using the Read method, which performs OCR
    var result = ocr.Read(input);

    // Output the extracted text to the console
    Console.WriteLine(result.Text);
}
Imports IronOcr
Imports System

' Initialize a new instance of the IronTesseract class
Private ocr = New IronTesseract()

' Use the OcrInput object to load the image file
Using input = New OcrInput("r2.png")
	' Read the image using the Read method, which performs OCR
	Dim result = ocr.Read(input)

	' Output the extracted text to the console
	Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

上面的程式碼以圖像的形式接收輸入,然後使用IronTesseract類別的Read方法從該圖像中提取資料。

發票 OCR API(開發者教學):圖 9 - 發票解析器 發票解析器

5.1. 發票處理:從發票中提取特定數據

您也可以從發票中提取特定數據,例如客戶發票號碼。 以下是用於從發票中提取客戶發票號碼的代碼。

using IronOcr;
using System;
using System.Text.RegularExpressions;

// Initialize a new instance of the IronTesseract class
var ocr = new IronTesseract();

// Use the OcrInput object to load the image file
using (var input = new OcrInput(@"r2.png"))
{
    // Perform OCR on the image
    var result = ocr.Read(input);

    // Define a regular expression pattern for the invoice number
    var linePattern = @"INV\/\d{4}\/\d{5}";

    // Match the pattern in the extracted text
    var lineMatch = Regex.Match(result.Text, linePattern);

    // Check if the pattern matches any part of the text
    if (lineMatch.Success)
    {
        // If a match is found, print the invoice number
        var lineValue = lineMatch.Value;
        Console.WriteLine("Customer Invoice number: " + lineValue);
    }
}
using IronOcr;
using System;
using System.Text.RegularExpressions;

// Initialize a new instance of the IronTesseract class
var ocr = new IronTesseract();

// Use the OcrInput object to load the image file
using (var input = new OcrInput(@"r2.png"))
{
    // Perform OCR on the image
    var result = ocr.Read(input);

    // Define a regular expression pattern for the invoice number
    var linePattern = @"INV\/\d{4}\/\d{5}";

    // Match the pattern in the extracted text
    var lineMatch = Regex.Match(result.Text, linePattern);

    // Check if the pattern matches any part of the text
    if (lineMatch.Success)
    {
        // If a match is found, print the invoice number
        var lineValue = lineMatch.Value;
        Console.WriteLine("Customer Invoice number: " + lineValue);
    }
}
Imports IronOcr
Imports System
Imports System.Text.RegularExpressions

' Initialize a new instance of the IronTesseract class
Private ocr = New IronTesseract()

' Use the OcrInput object to load the image file
Using input = New OcrInput("r2.png")
	' Perform OCR on the image
	Dim result = ocr.Read(input)

	' Define a regular expression pattern for the invoice number
	Dim linePattern = "INV\/\d{4}\/\d{5}"

	' Match the pattern in the extracted text
	Dim lineMatch = Regex.Match(result.Text, linePattern)

	' Check if the pattern matches any part of the text
	If lineMatch.Success Then
		' If a match is found, print the invoice number
		Dim lineValue = lineMatch.Value
		Console.WriteLine("Customer Invoice number: " & lineValue)
	End If
End Using
$vbLabelText   $csharpLabel

發票 OCR API(開發者教學):圖 10 - 發票掃描 發票掃描

6.結論

IronOCR 的發票 OCR API 利用機器學習和電腦視覺技術,徹底革新了從發票中提取資料的方式。 這項技術將發票上的文字和數字轉換為機器可讀格式,簡化了資料擷取,以便於分析、整合和流程改進。 它為自動化發票處理、提高準確性以及優化應付帳款等工作流程提供了強大的解決方案。 這項技術還可以實現從掃描的發票中自動輸入資料。

IronOCR 利用 Tesseract 的最佳結果提供高精度,無需任何額外設定。 它支援多頁框架 TIFFPDF 文件以及所有流行的圖像格式。 也可以從影像中讀取條碼值

請造訪主頁網站以了解更多關於IronOCR的資訊。 有關發票 OCR 的更多教程,請訪問以下詳細的發票 OCR 教程。 若要了解如何使用電腦視覺來尋找文字(例如發票欄位),請造訪此電腦視覺操作指南

常見問題解答

如何使用 OCR 自動處理發票資料?

您可以利用 IronOCR 的機器學習演算法來自動處理發票資料。IronOcr 可從數位和掃描的發票中擷取詳細資料,例如供應商資訊、發票號碼和價格,減少手動輸入錯誤並提昇效率。

設定發票 OCR API 涉及哪些步驟?

要使用 IronOCR 設定發票 OCR API,首先要透過 Visual Studio 的 NuGet Package Manager 下載並安裝函式庫。接下來,建立一個新的 C# 專案,整合 IronOCR,並使用其方法載入和讀取影像檔案以擷取文字。

IronOCR 是否可以擷取特定資料,例如發票號碼?

是的,IronOCR 可以擷取特定資料,例如發票號碼。它利用正規表達式來匹配擷取文字中的模式,讓您可以從發票中擷取特定資訊。

IronOCR 有哪些有益於發票處理的功能?

IronOCR 包括圖像預處理、條碼識別和檔案解析等功能。這些功能可增強其從各種發票格式中準確擷取和處理文字的能力,改善資料擷取和工作流程效率。

影像預處理如何改善 OCR 結果?

IronOCR 中的圖像預處理可在文字擷取之前優化圖像品質,有助於改善 OCR 結果。這包括對比度調整和降噪等作業,可從發票中擷取更精確的資料。

IronOCR 是否可以同時用於數位發票和掃描發票?

是的,IronOCR 既能處理數位發票,也能處理掃描發票。它使用先進的機器學習和電腦視覺技術,從各種格式和影像品質中準確擷取文字。

IronOCR 如何處理多種頁面格式和檔案類型?

IronOCR 支援多種頁面格式以及常用的影像和 PDF 檔案類型。它可以有效率地從複雜的文件中抽取文字,使其成為各種發票處理應用程式的通用工具。

開發人員在哪裡可以找到 IronOCR 的使用教學?

開發人員可以在 IronOCR 網站上找到教學和其他資源。該網站提供一系列學習材料,包括在不同情境中應用 IronOCR 的操作指南和博文。

Kannaopat Udonpant
軟體工程師
在成為软件工程師之前,Kannapat 從日本北海道大學完成了環境資源博士學位。在追逐學位期间,Kannapat 還成為了生產工程系一部份——汽車机器人实验室的成員。2022 年,他利用他的 C# 技能加入 Iron Software 的工程團隊, 專注於 IronPDF。Kannapat 珍惜他的工作,因为他直接向编写大部分 IronPDF 使用的代码的开发者学习。除了同行学习,Kannapat 还喜欢在 Iron Software 工作的社交十环。当他不编写代码或文档时,Kannapat 通常在他的 PS5 上打游戏或重看《The Last of Us》。