跳至頁尾內容
使用IRONBARCODE

C# Barcode Scanner Step-by-Step Tutorial

本教程將演示如何使用第三方程式庫建立條碼掃描器。 有多個程式庫可用於此目的,但其中一些是付費的,一些提供的功能有限,還有一些難以實施。 找到一個免費、高效且易於實施的全面且有用的程式庫是一項艱鉅的任務。

基於此,我們將使用IronBarcode,因為它最適合開發 .NET 條碼掃描器。 它還具有開發免費的附加優勢,高效且易於實施。 IronBarcode允許開發者在.NET應用程式和網站中讀取和寫入條碼和QR碼。 使用該程式庫讀取或寫入條碼只需一行程式碼。

.NET條碼程式庫讀寫大多數條碼和QR碼標準。 支持的條碼型別包括code 39/93/128、UPC A/E、EAN 8/13、ITF、RSS 14/擴展、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 套件管理器控制台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

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

首先,建立一個列表以存放所有條碼圖像的文件路徑。 接下來,調用List<string>型別作為參數並返回資料。 此方法並行讀取多個圖像中的條碼。 多個執行緒將啟動並自動管理以改善批量條碼讀取任務的性能。

掃描QR碼

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

QR碼

C#條碼掃描器逐步教程,圖8:此演示中使用的QR碼 此演示中使用的QR碼

考慮以下程式碼範例:

// 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函式用於讀取QR碼。 這就是IronBarcode程式庫提供的簡單性,允許相同的功能和程式碼用於不同的圖像來源。

輸出

C#條碼掃描器逐步教程,圖9:QR碼讀取器的控制台輸出 QR碼讀取器的控制台輸出

總結

本教程展示了從單個圖像掃描條碼、從PDF文件掃描條碼、以及並行地從多個圖像和多個文件掃描條碼的非常簡單的方法。 我們使用相同的功能來實現不同的功能,同時提供性能和可用性。IronBarcode提供生成條碼和QR碼的功能,具有不同的配置。 有許多功能無法在同一篇文章中討論。 請點擊文件頁面以進一步探索IronBarcode。

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

常見問題

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

要在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檔案讀取條碼嗎?

是的,IronBarcode允許您使用ReadPdf方法從PDF檔案中讀取條碼,這將掃描整個PDF文件以查找條碼影像並返回其資料。

是否可以從不同的影像檔中掃描多個條碼?

是的,您可以使用IronBarcode中的ReadAsync函式同時從不同的影像檔中掃描多個條碼,利用非同步處理來提高性能。

如何安裝用於條碼功能的.NET程式庫?

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

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

是的,您可以使用IronBarcode中的相同Read函式掃描QR碼,這使得在各種影像來源中條碼和QR碼的一致處理成為可能。

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

IronBarcode使用者友好、高效並具適應性。支援各種條碼型別,並提供簡單的API以讀取和寫入條碼及QR碼,是.NET應用程式的理想選擇。

Iron Software Suite包含哪些程式庫?

Iron Software Suite包含IronPDF用於PDF操作、IronXL用於Excel檔案處理、IronOCR用於光學字元識別、IronWebScraper用於網頁資料提取。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

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