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

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

條碼提供了一種清晰且機器可讀的資料呈現方式。 最初,條碼由寬度和間距各異的平行線組成,用於表示資料。 這些傳統的線性或一維(1D)條碼可以透過稱為條碼閱讀器的專用光學設備進行掃描。 然而,條碼的發展催生了二維(2D)條碼,也稱為矩陣碼。 與傳統條碼不同,二維條碼使用矩形、點和六邊形等圖案而不是條形。 要讀取這些二維條碼,可以使用專門的光學掃描器裝置,或者也可以使用連接到運行解碼軟體的電腦的數位相機。 此外,智慧型手機等行動裝置可以利用其內建相機和專用應用程式作為二維條碼掃描器。

ZXing條碼掃描器

Zebra Crossing(通常被稱為 ZXing)是一個開源的多格式 1D/2D 條碼影像處理工具包,它使用 Java 開發,並有其他語言的移植版本。 核心圖像解碼庫、Java 特定客戶端程式碼和 Android 用戶端條碼掃描器只是構成 ZXing 的幾個模組。 許多其他獨立的開源專案都是基於它建構的。

1. 特點

它可以追蹤網址、聯絡資訊、日曆事件等等。 它是專為 Java SE 應用程式而設計的。 透過實現目標,條碼掃描器整合成為可能。 這是一個簡單的谷歌眼鏡應用程式。

2. 將 ZXing 與.NET結合使用

開啟 Visual Studio,從檔案選單中選擇"新專案",然後選擇"控制台應用程式"。 本文中,我們將選擇 C# 控制台應用程式。

如何在ZXing中掃描條碼(面向C#開發人員)圖1

請在對應的文字方塊中輸入項目名稱和檔案路徑。 接下來,按一下"建立"按鈕選擇所需的.NET Framework。

如果您選擇的是控制台應用程序,專案現在將創建其結構並打開 program.cs 文件,允許您輸入程式碼並建立或執行它。

如何在ZXing中掃描條碼(面向C#開發人員)圖2

2.1 安裝 ZXing 條碼

在NuGet套件管理器控制台中輸入以下命令以安裝 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");
$vbLabelText   $csharpLabel

上面的程式碼設定了 QrCodeEncodingOptions 的高度和寬度。 然後它創建了 BarcodeWriter 的實例。 對於 BarcodeWriter,我們將條碼格式設定為 QR 碼。 我們將先前建立的二維碼選項指派給編寫者。 Write 方法在 BarcodeWriter 中將給定的字串編碼成條碼,並以點陣圖圖像的形式傳回。 圖像是使用點陣圖的 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();
}
$vbLabelText   $csharpLabel

在上面的程式碼中,我們首先將圖像載入到位圖中,然後建立一個 BarcodeReader 物件。 Decode 函數允許我們將點陣圖作為參數傳遞,它可以傳回多種格式的結果。 我們使用 Text 屬性來取得條碼中編碼的文字。

如何在ZXing中掃描條碼(面向C#開發人員)圖5

IronBarcode

借助這個條碼庫,讀取和建立條碼變得非常簡單。 使用 IronBarcode 的函式庫可以輕鬆製作動態條碼。 只需幾行程式碼,這個簡單的函式庫就可以產生條碼,這有助於我們對條碼影像進行編碼。 IronBarcode讓我們能夠使用 C# 和.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套件管理器控制台中使用以下命令:

Install-Package BarCode

或者,您可以使用NuGet套件管理器,它會顯示所有搜尋結果,以便尋找和下載"條碼"套件。 然後您可以從中選擇要下載到程式中的必要軟體包。

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

這段程式碼使用中等程度的糾錯產生 500 x 500 像素的圖形,然後使用 SaveAsPng 方法將其儲存到檔案位置。

下一個程式碼範例讀取我們在上一個範例中建立的二維碼中編碼的文字。

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();
$vbLabelText   $csharpLabel

我們首先將圖像載入到位圖中,然後使用 Read 類別上的 BarcodeReader 方法讀取圖像。 我們使用從 Read 方法傳回的 BarcodeResults 物件上的 Values 屬性來取得從二維碼讀取的內容。

要了解更多關於 ZXing 以及它與IronBarcode 的比較,請閱讀下一篇部落格文章

我們的"讀取條碼"教學課程還提供了有關如何使用IronBarcode讀取條碼和二維碼的更多資訊。 更多IronBarcode程式碼教學。

結論

ZXing條碼掃描器可以產生高品質的條碼,但它已經過時,支援的條碼格式也比較少。 此外,它的文件和產品支援也十分有限。

另一方面, IronBarcode非常有效率且靈活,能夠在多種作業系統上運作。 IronBarcode可以更改條碼的顏色、大小、間距和字型。 它還支援 Crystal Reports。

開發者可以免費使用IronBarcode 。 用戶可以購買許可證來存取更多功能,並獲得一整年的支援和產品更新。

請注意ZXing 是其各自所有者的註冊商標。 本網站與 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 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。

鋼鐵支援團隊

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