與其他組件比較

如何在ZXing中為C#開發人員掃描條碼

發佈 2023年6月21日
分享:

介紹

條碼提供了一種清晰且可機讀的方式來呈現數據。最初,條碼由具有不同寬度和間距的平行線組成,用於表示數據。這些傳統的一維條碼 (1D) 條碼可以由專門的光學裝置稱為條碼讀取器來掃描。然而,條碼的演變導致了二維條碼的發明。 (2D) 條碼,也稱為矩陣碼。與傳統條碼不同,2D 條碼使用矩形、點和六邊形等圖案,而不是條紋。為了讀取這些 2D 條碼,可以使用特定的光學掃描器設置,或使用連接到運行解碼軟件的計算機的數碼相機作為替代方法。此外,智能手機等移動設備可以利用其集成的攝像頭和專用應用程序作為 2D 條碼掃描器。

ZXing 條碼掃描器

Zebra Crossing,通常稱為 ZXing,是一個開源的、多格式的 1D/2D 条碼影像處理工具包,最初用 Java 開發,並有其他語言的版本。ZXing 由多個模組組成,包括核心影像解碼庫、Java 專用客戶端代碼和 Android 客戶端 Barcode Scanner。許多其他獨立的開源項目都是基於它構建的。

1. 功能

  • 可以追蹤URLs、聯繫資訊、行事曆事件等。
  • 專為Java SE應用程式設計。
  • 透過目標,可以整合條碼掃描器。
  • 這是一款簡單的Google Glass應用程式。

2. 使用ZXing與.NET

打開Visual Studio,從檔案選單中選擇“新建專案”,然後選擇“主控台應用程式”。在本文中,我們選擇C# 主控台應用程式。

如何在 ZXing 中掃描條碼(適用於 C# 開發人員) 圖 1

在適當的文本框中輸入專案名稱和檔案路徑。接下來,點擊「Create」按鈕以選擇所需的.NET Framework。

如果您選擇了一個控制台應用程式,專案現在將創建其結構並打開program.cs檔案,允許您輸入程式碼並構建或執行它。

如何在 ZXing 中掃描條碼:C# 開發人員圖 2

2.1 安裝ZXing條形碼

在NuGet套件管理器控制台中輸入以下命令。

Install-Package ZXing.Net.Bindings.Windows.Compatibility

或者,我們可以使用 NuGet 套件管理工具來獲取項目。如下圖所示。嘗試安裝您選擇的第一個結果。

如何在ZXing中為C#開發人員掃描條碼 圖3

2.2 使用 ZXing 讀取和寫入條碼

我們可以使用下方範例程式碼來創建條碼。ZXing 允許我們創建超過 10 種條碼格式。

using ZXing.Windows.Compatibility;

var options = new QrCodeEncodingOptions
{
    Width = 250,
    Height = 250,
};

var writer = new BarcodeWriter();
writer.Format= BarcodeFormat.QR_CODE;
writer.Options = options;
System.Drawing.Bitmap _bitmap=writer.Write("Hello world");
_bitmap.Save("Demo1.png");
using ZXing.Windows.Compatibility;

var options = new QrCodeEncodingOptions
{
    Width = 250,
    Height = 250,
};

var writer = new BarcodeWriter();
writer.Format= BarcodeFormat.QR_CODE;
writer.Options = options;
System.Drawing.Bitmap _bitmap=writer.Write("Hello world");
_bitmap.Save("Demo1.png");
Imports ZXing.Windows.Compatibility

Private options = New QrCodeEncodingOptions With {
	.Width = 250,
	.Height = 250
}

Private writer = New BarcodeWriter()
writer.Format= BarcodeFormat.QR_CODE
writer.Options = options
Dim _bitmap As System.Drawing.Bitmap=writer.Write("Hello world")
_bitmap.Save("Demo1.png")
VB   C#

以上,我們正在設置 QrCodeEncodingOptions 的高度和寬度。然後我們創建一個 BarcodeWriter 對象。對於 BarcodeWriter,我們將條形碼格式設置為 QR_Code。然後我們指定先前創建的 qrcode 選項。 BarcodeWriter 中的 write 函數負責將給定字串編碼為條形碼,並將條形碼以位圖圖像形式返回。然後,借助位圖中的 save 函數,我們可以保存圖像。以下是代碼的結果。

如何在 ZXing 中掃描條碼-C# 開發者圖 4


using ZXing.Windows.Compatibility;
var barcodeBitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("demo.png");
var reader = new BarcodeReader();
var result = reader.Decode(barcodeBitmap);
if (result != null)
{
    Console.WriteLine(result.Text);
    Console.ReadKey();
}

using ZXing.Windows.Compatibility;
var barcodeBitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("demo.png");
var reader = new BarcodeReader();
var result = reader.Decode(barcodeBitmap);
if (result != null)
{
    Console.WriteLine(result.Text);
    Console.ReadKey();
}
Imports ZXing.Windows.Compatibility
Private barcodeBitmap = CType(System.Drawing.Bitmap.FromFile("demo.png"), System.Drawing.Bitmap)
Private reader = New BarcodeReader()
Private result = reader.Decode(barcodeBitmap)
If result IsNot Nothing Then
	Console.WriteLine(result.Text)
	Console.ReadKey()
End If
VB   C#

在上面的代碼中,我們首先將圖像加載到位圖中。然後,我們創建一個 BarcodeReader 對象。Decode 函數允許我們將位圖作為參數傳遞,這可以返回多種類型的結果。接下來,我們使用 Text 屬性來獲取條碼中編碼的文本。

如何在 ZXing 中為 C# 開發人員掃描條碼 圖 5

IronBarcode

有了這個條碼函式庫,讀取和創建條碼變得簡單。使用 IronBarcode 的函式庫,可以輕鬆製作動態條碼。只需幾行程式碼,這個簡單的函式庫便可生成條碼,幫助我們編碼條碼影像。IronBarcode 使我們能夠以 C# 和 VB.NET 等語言生成條碼。

1. 功能

  • IronBarcode 可以讀取和寫入大多數條形碼圖像格式和 QR 標準,包括 UPC A/E、Databar、EAN 8/13、MSI、Code 39/93/128、CodaB、RSS 14/Expanded 和 ITF。
  • 在掃描掃描件和實時視頻幀時,IronBarcode 可以校正旋轉、噪聲、扭曲和傾斜。為了提高讀取準確性和速度,IronBarcode 在條形碼圖像生成時會自動預處理它們。動態條形碼經常被使用,因為它們允許更改內容。
  • IronBarcode 利用多核心和多線程的能力對批量處理服務器很有優勢。
  • IronBarcode 可以在單頁和多頁文檔中自動找到一個或多個條形碼。

2. 使用 IronBarcode

要在解決方案中使用 IronBarcode 庫,您必須下載所需的套件。請使用以下說明的套件管理器代碼來完成這項操作:

:PackageInstall

作為替代方案,您可以使用 NuGet 套件管理器,它會顯示所有搜尋結果,以搜尋並下載 "Barcode" 套件。然後您可以從中選擇必要的套件下載到程式中。

如何在ZXing中掃描條碼(適用於C#開發人員) 圖6

3. 使用 IronBarcode 讀寫條碼

只需幾行代碼,我們就可以使用 IronBarcode 庫快速生成條碼圖像。此外,它使我們能夠將創建的條碼保存為單獨的圖片文件。以下是使用控制台程序創建條碼標籤的一些示例 Visual Basic 代碼。

using IronBarCode;
QRCodeWriter.CreateQrCode(textBox1.Text, 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("demo.png");
using IronBarCode;
QRCodeWriter.CreateQrCode(textBox1.Text, 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("demo.png");
Imports IronBarCode
QRCodeWriter.CreateQrCode(textBox1.Text, 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("demo.png")
VB   C#

通過使用中等級錯誤更正產生一個500 x 500像素的圖形,然後使用SaveAsPng方法將其保存在文件位置。

以下的代碼範例會讀取我們在上一個範例中創建的 QR 碼中的編碼文本。

var barcodeBitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("demo.png");
var reader=IronBarCode.BarcodeReader.Read(barcodeBitmap);
Console.WriteLine(reader.Values()[0]);
Console.ReadKey();
var barcodeBitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("demo.png");
var reader=IronBarCode.BarcodeReader.Read(barcodeBitmap);
Console.WriteLine(reader.Values()[0]);
Console.ReadKey();
Dim barcodeBitmap = CType(System.Drawing.Bitmap.FromFile("demo.png"), System.Drawing.Bitmap)
Dim reader=IronBarCode.BarcodeReader.Read(barcodeBitmap)
Console.WriteLine(reader.Values()(0))
Console.ReadKey()
VB   C#

我們首先將圖像加載到位圖中,然後使用 BarcodeReader 類的 Read 方法讀取圖像。我們使用從 Read 方法返回的 BarcodeResults 對象的 Values 屬性來獲取從 QR 碼中讀取的內容。

要了解有關 ZXing 的更多信息以及它與 IronBarcode 的比較,請閱讀這個 下一篇博客文章我们的 讀取條碼 教程還提供了更多關於如何使用IronBarcode讀取條形碼和QR碼的信息。更多關於IronBarcode的代碼教程。

結論

ZXing 條碼掃描器可以創建高品質的條碼,但它已經過時並且支持的條碼格式很少。它的文檔和產品支持也很有限。

另一方面,IronBarcode 非常高效且靈活,能夠在許多操作系統上運行。IronBarcode 可以更改條碼使用的顏色、大小、間距和字母。它還支持 Crystal Reports。

開發人員可以 使用 IronBarcode 免費使用。用戶可以購買一個 許可證 以獲取更多功能並享受一整年的支援和產品更新。

下一個 >
ZXing.org QR Code Library 和 IronBarcode:全面比較

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 1,203,227 查看許可證 >