與其他組件比較

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

喬迪·巴迪亞
喬迪·巴迪亞
2023年6月21日
分享:

介紹

條形碼提供了一種清晰且機器可讀的方式來呈現數據。 最初,條碼由不同寬度和間距的平行線組成,用來表示數據。 這些傳統的線性或一維(1D)條碼可以由專門的光學設備稱為條碼閱讀器掃描。 然而,條碼的演變促使發明了二維(2D)條碼,也稱為矩陣碼。 與傳統條碼不同,2D條碼使用矩形、點和六邊形等圖案,而非條狀圖案。 要读取这些二维条码,可以使用特定的光学扫描仪设置,或者选择使用连接到运行解码软件的计算机的数码相机作为替代方法。 此外,像智慧型手機這樣的行動裝置可以利用其內建的相機和專用應用程式來作為2D條碼掃描器。

ZXing 條碼掃描器

Zebra Crossing,通常稱為 ZXing,是一個開源的多格式 1D/2D 條碼圖像處理工具包,最初用 Java 開發,並具有其他語言的移植版本。 核心圖像解碼庫、Java特定的客户端代碼以及Android客户端條碼扫描器只是構成ZXing的幾個模塊。 有許多獨立的開源專案是基於它構建的。

1. 功能

  • 它可以跟踪網址、聯絡資訊、日曆事件等。
  • 它是以 Java SE 應用程式為考量而創建的。
  • 透過目的,條碼掃描器的整合是可能的。
  • 這是一個簡單的 Google Glass 應用程式。

2. 在 .NET 中使用 ZXing

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

如何在 ZXing 中為 C# 開發者掃描條碼 圖 1

在相应的文本框中輸入專案名稱和文件路徑。 接下來,點擊建立按鈕以選擇所需的 .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")
$vbLabelText   $csharpLabel

上面,我們正在設置QrCodeEncodingOptions的高度和寬度。 然後我們為BarcodeWriter創建一個對象。 對於BarcodeWriter,我們將條碼格式設置為QR_Code。 然後我們分配之前創建的qrcode選項。 BarcodeWriter 中的 write 函數將給定的字串編碼成條碼,並以點陣圖圖像的形式返回條碼。 然後,在位圖中的保存功能的幫助下,我們可以保存影像。 以下是程式碼的結果。

如何在 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
$vbLabelText   $csharpLabel

在上述代碼中,我們首先將圖像加載到位圖中。然後,我們創建一個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庫快速製作條碼圖像。 此外,它使我們能夠將生成的條形碼另存為一個單獨的圖片文件。以下是一段使用 Console 程式建立條形碼標籤的 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")
$vbLabelText   $csharpLabel

使用中等級別的錯誤校正生成 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()
$vbLabelText   $csharpLabel

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

如需了解更多關於ZXing的信息及其與IronBarcode的比較,請閱讀這篇下一篇博客文章

我們的閱讀條碼教程還提供有關如何使用IronBarcode來讀取條碼和QR碼的更多信息。 更多有關 IronBarcode 的程式教學。

結論

ZXing 條碼掃描器可以創建高質量的條碼,但它過時且僅支持少數條碼格式。 它也有有限的文件和產品支持。

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

開發人員可以免費使用 IronBarcode。 用戶可以購買授權以獲得額外功能,並享受一整年的支援和產品更新。

喬迪·巴迪亞
軟體工程師
Jordi 最擅長 Python、C# 和 C++,當他不在 Iron Software 發揮技能時,他會進行遊戲編程。他負責產品測試、產品開發和研究,為持續產品改進增添了巨大的價值。多樣化的經驗使他感到挑戰和投入,他說這是與 Iron Software 合作的最喜歡的方面之一。Jordi 在佛羅里達州邁阿密長大,並在佛羅里達大學學習計算機科學和統計學。
下一個 >
ZXing.org QR Code Library 和 IronBarcode:全面比較