跳過到頁腳內容
使用IRONBARCODE

C#條碼掃描器逐步教程

本教學將示範如何使用第三方函式庫建立條碼掃描器。 有多種庫可用於此目的,但有些是付費的,有些功能較少,有些難以實現。 找到一個既全面又實用,而且免費、高效、易於實現的庫是一項具有挑戰性的任務。

因此,我們將使用IronBarcode ,因為它最適合開發 .NET 條碼掃描器。 它還具有開發免費、效率高、易於實施等額外優點。 IronBarcode 讓開發人員可以在 .NET 應用程式和網站中讀取和寫入條碼和二維碼。 使用此庫,讀取或寫入條碼只需要一行程式碼。

.NET 條碼庫可以讀取和寫入大多數條碼和二維碼標準。 支援的條碼類型包括 Code 39/93/128、UPC A/E、EAN 8/13、ITF、RSS 14 / Expanded、Databar、Codabar、Aztec、Data Matrix、MaxiCode、PDF417、MSI、Plessey、USPS 和 QR。 條碼結果資料包括類型、文字、二進位資料、頁面和圖像檔案。

條碼寫入 API 會檢查並驗證格式、長度、數量和校驗和,以自動避免編碼錯誤。 條碼寫入器允許設定樣式、調整大小、邊距、邊框、重新著色和新增文字註解。 條碼寫入器可以寫入包括 BMP、PNG、GIF、TIFF 和 JPG 在內的映像檔。 它也可以寫入PDF或HTML檔案

讓我們創建自己的條碼掃描器,以便更好地理解它。

建立一個 Visual Studio 項目

首先,建立一個用於開發示範應用程式的 Visual Studio 專案。您也可以開啟一個現有的項目。

請按照以下步驟建立一個新的控制台應用程式項目,重點關注核心功能。 相同的程式碼可以在 Web API、MVC、Web Forms 或 Windows Forms 應用程式中實作。

  1. 開啟 Visual Studio
  2. 點選"建立新項目"
  3. 選擇模板,點選"下一步"按鈕
  4. 為項目命名,然後點選"下一步"按鈕
  5. 選擇目標框架,然後按一下"下一步"按鈕
  6. 點選"建立"按鈕,然後點選"下一步"按鈕

將建立一個新項目,如下所示:

C# 條碼掃描器逐步教程,圖 1:控制台應用程式 控制台應用程式

下一步是安裝IronBarcode NuGet 套件以使用其功能。

安裝 IronBarcode 的 NuGet 套件

您可以使用套件管理器控制台、NuGet 套件管理器解決方案安裝該庫,或直接從NuGet BarCode 套件頁面安裝。

請遵循以下步驟:

  1. 點選"工具" > "NuGet 套件管理員" > "套件管理員控制台"

    C# 條碼掃描器逐步教程,圖 2:套件管理器控制台 UI 軟體包管理器控制台使用者介面

  2. 寫出以下指令
Install-Package BarCode

該軟體包將被安裝。

現在我們來編寫掃描條碼圖像的程式碼。

從影像檔案掃描條碼

新增以下命名空間

using IronBarCode;
using IronBarCode;
Imports IronBarCode
$vbLabelText   $csharpLabel

接下來,在主函數中編寫以下程式碼,以從圖像中讀取條碼資料。

// Read the barcode from an image file
var myBarcode = BarcodeReader.Read(@"D:\Barcode Images\mybarcode.jpeg");

// Print the barcode data to the console
Console.WriteLine(myBarcode);
// Read the barcode from an image file
var myBarcode = BarcodeReader.Read(@"D:\Barcode Images\mybarcode.jpeg");

// Print the barcode data to the console
Console.WriteLine(myBarcode);
' Read the barcode from an image file
Dim myBarcode = BarcodeReader.Read("D:\Barcode Images\mybarcode.jpeg")

' Print the barcode data to the console
Console.WriteLine(myBarcode)
$vbLabelText   $csharpLabel

BarcodeReader類別提供了一個Read函數,該函數接受檔案路徑作為參數。 此函數讀取圖像並傳回條碼資料。 此方法可從 BMP、PNG、 GIF、TIFF或 JPG 中讀取條碼,並提供精細的設定供開發人員控制,以便在給定用例中平衡效能和準確性。

該路徑包含以下條碼圖像,應用程式將掃描該圖像。

條碼影像

C# 條碼掃描器逐步教程,圖 3:此控制台應用程式中使用的條碼圖像 此控制台應用程式中使用的條碼圖像

讓我們讀取這個條碼圖像,看看這個程式是否能產生正確的結果。

輸出

C# 條碼掃描器逐步教程,圖 4:應用程式運行時的控制台輸出 應用程式運行時的控制台輸出

可以看出,該程式產生了準確的輸出結果。

掃描 PDF 檔案中的條碼

很多情況都需要掃描PDF發票上的條碼。在這個例子中,我們將掃描以下發票上的條碼。

PDF文檔

C# 條碼掃描器逐步教程,圖 5:PDF 格式的發票 PDF格式的發票

以下程式碼片段可用於掃描 PDF 文件中的條碼:

// Read barcodes from a PDF file
var myBarcode = BarcodeReader.ReadPdf(@"D:\Barcode Images\invoice.pdf");

// Iterate through each barcode found and print its value
foreach(var barcodeData in myBarcode)
{
    Console.WriteLine(barcodeData.Value);
}
// Read barcodes from a PDF file
var myBarcode = BarcodeReader.ReadPdf(@"D:\Barcode Images\invoice.pdf");

// Iterate through each barcode found and print its value
foreach(var barcodeData in myBarcode)
{
    Console.WriteLine(barcodeData.Value);
}
' Read barcodes from a PDF file
Dim myBarcode = BarcodeReader.ReadPdf("D:\Barcode Images\invoice.pdf")

' Iterate through each barcode found and print its value
For Each barcodeData In myBarcode
	Console.WriteLine(barcodeData.Value)
Next barcodeData
$vbLabelText   $csharpLabel

BarcodeReader類別提供了ReadPdf函數,該函數接受檔案路徑作為參數。 此函數在 PDF 檔案中尋找條碼影像,掃描整個條碼,並以陣列的形式傳回其資料。 此函數讀取文件中嵌入的每張圖片的條碼。

使用foreach循環將條碼資料列印到控制台。

輸出

發票號碼列印在主機上。

C# 條碼掃描器逐步教程,圖 6:控制台輸出顯示發票號碼 控制台輸出顯示發票號碼

掃描多個檔案中的條碼

本範例將示範如何同時掃描影像檔案中的多個條碼。

條碼影像

C# 條碼掃描器逐步教程,圖 7:以下範例中使用的條碼影像 以下範例中使用的條碼圖像

請考慮以下程式碼片段,它們使用多執行緒讀取多個條碼並掃描結果。

// Create a list of file paths containing barcode images
List<string> barcodeList = new List<string>
{
    @"D:\Barcode Images\barcode1.jpg",
    @"D:\Barcode Images\barcode2.jpg",
    @"D:\Barcode Images\barcode3.jpg"
};

// Read barcodes asynchronously from multiple files
var batchResults = BarcodeReader.ReadAsync(barcodeList);

// Work with the results
foreach (var result in batchResults)
{
    string barcodeValue = result.Text;
    Console.WriteLine(barcodeValue);
}
// Create a list of file paths containing barcode images
List<string> barcodeList = new List<string>
{
    @"D:\Barcode Images\barcode1.jpg",
    @"D:\Barcode Images\barcode2.jpg",
    @"D:\Barcode Images\barcode3.jpg"
};

// Read barcodes asynchronously from multiple files
var batchResults = BarcodeReader.ReadAsync(barcodeList);

// Work with the results
foreach (var result in batchResults)
{
    string barcodeValue = result.Text;
    Console.WriteLine(barcodeValue);
}
' Create a list of file paths containing barcode images
Dim barcodeList As New List(Of String) From {"D:\Barcode Images\barcode1.jpg", "D:\Barcode Images\barcode2.jpg", "D:\Barcode Images\barcode3.jpg"}

' Read barcodes asynchronously from multiple files
Dim batchResults = BarcodeReader.ReadAsync(barcodeList)

' Work with the results
For Each result In batchResults
	Dim barcodeValue As String = result.Text
	Console.WriteLine(barcodeValue)
Next result
$vbLabelText   $csharpLabel

首先,建立一個清單來保存所有條碼影像的檔案路徑。 接下來,呼叫ReadAsync函數,該函數接受一個List<string>將類型作為參數傳遞,並傳回資料。 此方法可以並行讀取多張影像中的條碼。 將啟動多個執行緒並自動管理,以提高批次條碼讀取任務的效能。

掃描二維碼

二維碼的使用正在迅速增長。 因此,本節將展示如何使用 C# 掃描二維碼。

QR 圖碼

C# 條碼掃描器逐步教程,圖 8:本示範中使用的二維碼 本示範中使用的二維碼

請參考以下程式碼範例:

// Read the QR code from an image file
var qrCodeResult = BarcodeReader.Read(@"D:\Barcode Images\QRcode.jpeg");

// Iterate through each result and print its text
foreach (var result in qrCodeResult)
{
    Console.WriteLine(result.Text);
}
// Read the QR code from an image file
var qrCodeResult = BarcodeReader.Read(@"D:\Barcode Images\QRcode.jpeg");

// Iterate through each result and print its text
foreach (var result in qrCodeResult)
{
    Console.WriteLine(result.Text);
}
' Read the QR code from an image file
Dim qrCodeResult = BarcodeReader.Read("D:\Barcode Images\QRcode.jpeg")

' Iterate through each result and print its text
For Each result In qrCodeResult
	Console.WriteLine(result.Text)
Next result
$vbLabelText   $csharpLabel

讀取二維碼時,可以使用與上面討論的相同的Read功能。 這就是 IronBarcode 庫的優勢所在,它允許使用相同的函數和程式碼處理不同的圖像來源。

輸出

C# 條碼掃描器逐步教程,圖 9:二維碼閱讀器控制台輸出 控制台退出二維碼閱讀器

摘要

本教學課程示範了一種非常簡單的方法,可以從單一影像中掃描條碼,從 PDF 文件中掃描條碼,以及並行從多個影像和多個文件中掃描條碼。 我們使用同一函數實現了不同的功能,同時也兼顧了效能和易用性。 IronBarcode 提供了產生具有不同配置的條碼和二維碼的功能。 有很多功能無法在同一篇文章中一一討論。 請點擊文檔頁面,進一步了解 IronBarcode。

IronBarcode是Iron軟體套件的一部分。該套件包含其他非常有用的庫,例如用於讀取和寫入PDF文件的IronPDF、用於處理Excel文件的IronXL、用於從圖像中讀取文字的IronOCR以及用於從不同網站提取資料的IronWebScraper。 您可以用購買兩個獨立音色庫的價格,購買完整的Iron Suite 音色庫

常見問題解答

如何在 C# 中建立 BarCode 掃描器?

要在 C# 中建立條碼掃描器,您可以使用 IronBarcode。首先建立一個 Visual Studio 專案,安裝 IronBarcode NuGet 套件,並利用 BarcodeReader 類讀取影像或 PDF 檔案中的條碼。

使用 .NET 條碼函式庫可以讀取哪些條碼類型?

IronBarcode 可以讀取廣泛的條碼類型,包括 Code 39/93/128、UPC A/E、EAN 8/13、ITF、RSS 14 / Expanded、Databar、Codabar、Aztec、Data Matrix、MaxiCode、PDF417、MSI、Plessey、USPS 和 QR 代碼。

我可以用 C# 從 PDF 檔案讀取 BarCode 嗎?

是的,IronBarcode 允許您使用 ReadPdf 方法從 PDF 文件中讀取條碼,該方法會掃描整個 PDF 文件中的條碼圖像,並返回其數據。

是否可以用 C# 從不同的影像檔案掃描多個 BarCode?

是的,您可以使用 IronBarcode 中的 ReadAsync 函式來同時掃描不同影像檔案中的多個條碼,利用異步處理來改善效能。

如何為條碼功能安裝 .NET 函式庫?

要安裝 IronBarcode,請使用 Visual Studio 中的套件管理員控制台,使用命令 Install-Package BarCode,這將從 NuGet 套件來源中新增該函式庫。

QR 碼可以使用與 BarCode 相同的方法掃描嗎?

是的,您可以使用 IronBarcode 中相同的 Read 功能掃描 QR 代碼,允許在各種影像來源中一致處理條碼和 QR 代碼。

使用 IronBarcode 進行條碼掃描有哪些優勢?

IronBarcode 使用方便、效率高、適應性強。它支援多種條碼類型,並提供直接的 API 用於讀寫條碼和 QR 代碼,使其成為 .NET 應用程式的理想選擇。

Iron Software Suite 包含哪些程式庫?

Iron Software Suite 包括多個函式庫,例如用於 PDF 處理的 IronPDF、用於 Excel 檔案處理的 IronXL、用於光學字元辨識的 IronOCR,以及用於網頁資料擷取的 IronWebScraper。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。