跳過到頁腳內容
與其他組件的比較

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

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

ZXing 條碼掃描器

Zebra Crossing,通常稱為 ZXing,是一個開源、多格式的 1D/2D 條碼圖像處理工具包,用 Java 開發,並對其他語言進行移植。 ZXing 由核心圖像解碼庫、Java 專用客戶端代碼和 Android 客戶端條碼掃描器等模塊組成。 許多其他獨立的開源項目都基於它。

1. 功能

  • 它可以跟踪 URL、聯繫信息、日曆事件等等。
  • 它是為 Java SE 應用程序而創建的。
  • 通過用途,可以集成條碼掃描器。
  • 它是一個簡單的 Google Glass 應用程序。

2. 在 .NET 中使用 ZXing

打開 Visual Studio,從文件菜單中選擇“新建項目”,然後選擇“控制台應用程序”。 在本文中,我們選擇 C# 控制台應用程序。

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

在適當的文本框中輸入項目名稱和文件路徑。 接下來,點擊 創建 按鈕以選擇所需的 .NET 框架。

如果您選擇了控制台應用程式,則項目現在將創建其結構並打開 program.cs 文件,允許您輸入程序的代碼並對其進行構建或執行。

如何在 ZXing 中掃描條碼給 C# 開發者 圖2

2.1 安裝 ZXing 條碼

在 NuGet Package Manager Console 中輸入以下命令以安裝 ZXing 庫:

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;

// Encode the string into a QR code bitmap image
System.Drawing.Bitmap _bitmap = writer.Write("Hello world");

// Save the bitmap as a PNG file
_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;

// Encode the string into a QR code bitmap image
System.Drawing.Bitmap _bitmap = writer.Write("Hello world");

// Save the bitmap as a PNG file
_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

' Encode the string into a QR code bitmap image
Dim _bitmap As System.Drawing.Bitmap = writer.Write("Hello world")

' Save the bitmap as a PNG file
_bitmap.Save("Demo1.png")
$vbLabelText   $csharpLabel

上述代碼設置了 QrCodeEncodingOptions 的高度和寬度。 然後它創建了一個 BarcodeWriter 的實例。 對於 BarcodeWriter,我們將條碼格式設為 QR 碼。 我們將先前創建的 QR 碼選項分配給編寫器。 BarcodeWriter 中的 Write 方法將給定字符串編碼為條碼並將其作為位圖圖像返回。 該圖像使用位圖的 Save 方法保存。以下是代碼的結果。

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

下一個代碼示例演示了使用 ZXing 解碼條碼。

using ZXing.Windows.Compatibility;

// Load the barcode image into a bitmap
var barcodeBitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("demo.png");

// Create a BarcodeReader object
var reader = new BarcodeReader();

// Decode the bitmap into a result
var result = reader.Decode(barcodeBitmap);

if (result != null)
{
    // Output the decoded text to the console
    Console.WriteLine(result.Text);
    Console.ReadKey();
}
using ZXing.Windows.Compatibility;

// Load the barcode image into a bitmap
var barcodeBitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("demo.png");

// Create a BarcodeReader object
var reader = new BarcodeReader();

// Decode the bitmap into a result
var result = reader.Decode(barcodeBitmap);

if (result != null)
{
    // Output the decoded text to the console
    Console.WriteLine(result.Text);
    Console.ReadKey();
}
Imports ZXing.Windows.Compatibility

' Load the barcode image into a bitmap
Private barcodeBitmap = CType(System.Drawing.Bitmap.FromFile("demo.png"), System.Drawing.Bitmap)

' Create a BarcodeReader object
Private reader = New BarcodeReader()

' Decode the bitmap into a result
Private result = reader.Decode(barcodeBitmap)

If result IsNot Nothing Then
	' Output the decoded text to the console
	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 庫,您必須下載所需的包。 為此,請在 NuGet Package Manager Console 中使用以下命令:

Install-Package BarCode

作為替代方案,您可以使用 NuGet Package Manager,這將顯示所有搜索結果,尋找並下載“Barcode”包。 然後,您可以從中選擇要下載到程序的基本包。

如何在 ZXing 中掃描條碼給 C# 開發者 圖6

3. 使用 IronBarcode 讀取和寫入條碼

用少量的代碼行,我們可以快速地使用 IronBarcode 庫製作條碼圖像。 此外,它允許我們將創建的條碼作為單獨的圖像文件保存。以下是一些用於創建條碼標籤的 C# 範例代碼,搭配控制台程序。

using IronBarCode;

// Create a QR code with a medium error correction level
QRCodeWriter.CreateQrCode("Your text here", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("demo.png");
using IronBarCode;

// Create a QR code with a medium error correction level
QRCodeWriter.CreateQrCode("Your text here", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("demo.png");
Imports IronBarCode

' Create a QR code with a medium error correction level
QRCodeWriter.CreateQrCode("Your text here", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("demo.png")
$vbLabelText   $csharpLabel

此代碼以中等級別的糾錯生成一個500x500 像素的圖形,然後使用 SaveAsPng 方法將其保存到文件位置。

下一個代碼示例讀取我們在前一個示例中創建的 QR 碼中編碼的文本。

using IronBarCode;

// Load the QR code image into a bitmap
var barcodeBitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("demo.png");

// Read the barcode image
var reader = IronBarCode.BarcodeReader.Read(barcodeBitmap);

// Output the decoded value to the console
Console.WriteLine(reader.Values[0]);
Console.ReadKey();
using IronBarCode;

// Load the QR code image into a bitmap
var barcodeBitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("demo.png");

// Read the barcode image
var reader = IronBarCode.BarcodeReader.Read(barcodeBitmap);

// Output the decoded value to the console
Console.WriteLine(reader.Values[0]);
Console.ReadKey();
Imports IronBarCode

' Load the QR code image into a bitmap
Private barcodeBitmap = CType(System.Drawing.Bitmap.FromFile("demo.png"), System.Drawing.Bitmap)

' Read the barcode image
Private reader = IronBarCode.BarcodeReader.Read(barcodeBitmap)

' Output the decoded value to the console
Console.WriteLine(reader.Values(0))
Console.ReadKey()
$vbLabelText   $csharpLabel

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

要了解更多關於 ZXing 以及它如何與 IronBarcode 進行比較的信息,請閱讀這篇 下一篇博文

我們的 閱讀條碼 教程還提供更多關於如何使用 IronBarcode 讀取條碼和 QR 碼的資訊。 更多有關 IronBarcode 的代碼教程。

結論

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

另一方面,IronBarcode 非常高效靈活,能夠在許多操作系統上工作。 IronBarcode 可以更改條碼中使用的顏色、大小、間距和文字。 它還支持水晶報表。

開發人員可以 免費使用 IronBarcode。 用戶可以購買 許可證,以獲取附加功能並獲得完整一年 的支援和產品更新。

[{i:(ZXing 是其各自所有者的註冊商標。 此站點與 ZXing 並無關聯,不由其認可或贊助。 所有產品名稱、徽標和品牌均為其各自所有者的財產。 比較僅供參考,反映撰寫時公開可用的信息。

常見問題解答

怎樣在 C# 中將 HTML 轉換為 PDF?

您可以使用 IronPDF 的 RenderHtmlAsPdf 方法將 HTML 字符串轉換為 PDF。您還可以使用 RenderHtmlFileAsPdf 將 HTML 文件轉換為 PDF。

IronBarcode和ZXing有哪些不同點?

與ZXing相比,IronBarcode提供了更大的靈活性並支持更廣泛的條碼格式。雖然ZXing在創建條碼方面有效,但在格式支持和文檔方面存在限制。IronBarcode在效率上表現出色,並兼容於各種操作系統。

如何使用移動設備掃描條碼?

使用ZXing的Android客戶端條碼掃描器,可以利用設備的相機來掃描條碼。IronBarcode可集成到移動應用中以增強條碼掃描能力,提供更強大的功能。

IronBarcode是否支持二維條碼?

是的,IronBarcode支持一維和二維條碼,包括QR碼,使條碼的讀取和創建更加多樣化。

IronBarcode能否處理動態條碼創建?

IronBarcode支持動態條碼創建,可以根據具體需求自定義條碼的顏色、大小、間距和字母。

在.NET專案中集成IronBarcode需要什麼?

要在.NET專案中集成IronBarcode,可以使用NuGet包管理器控制台通過Install-Package IronBarcode安裝包,或者在NuGet包管理器中找到它。

使用IronBarcode是否有費用?

IronBarcode提供免費試用,但購買許可可獲得更多功能、產品更新和為期一年的支持。

IronBarcode可以用來從視頻幀中讀取條碼嗎?

是的,IronBarcode可以處理實時視頻幀,矯正旋轉、噪聲、失真和偏斜,從而提高條碼掃描的準確性和速度。

ZXing有哪些條碼掃描功能?

ZXing提供了一個開源套件,特別是用於一維和二維條碼掃描。它包括核心圖像解碼庫和一個Android客戶端條碼掃描器。

IronBarcode支持哪些編程語言?

IronBarcode支持C#和VB.NET,是在.NET框架內工作的開發人員的理想選擇。

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