跳至頁尾內容
與其他組件相比

如何在 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 ,我們將條碼格式設定為二維碼。 我們將先前建立的二維碼選項指派給編寫者。 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();
}
$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 套件管理器控制台中使用以下命令:

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

我們首先將圖像載入到位圖中,然後使用BarcodeReader類別的Read方法讀取圖像。 我們使用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 安卓用戶端條碼掃描器,您可以利用裝置的攝影機掃描條碼。為了獲得更強大的功能,IronBarcode 可以整合到行動應用程式中,以增強條碼掃描能力。

IronBarcode是否支援二維條碼?

是的,IronBarcode 支援一維和二維條碼,包括二維碼,可實現靈活的條碼讀取和建立。

IronBarcode 能否處理動態條碼產生?

IronBarcode 支援動態條碼創建,可讓您根據特定要求自訂條碼的顏色、大小、間距和文字。

將 IronBarcode 整合到 .NET 專案中需要哪些條件?

要將 IronBarcode 整合到 .NET 專案中,請使用Install-Package IronBarcode透過 NuGet 套件管理器控制台安裝該套件,或在 NuGet 套件管理器中找到它。

使用IronBarcode需要付費嗎?

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

IronBarcode 能否用於讀取視訊幀中的條碼?

是的,IronBarcode 可以處理即時視訊幀,校正旋轉、雜訊、失真和傾斜,從而提高條碼掃描的準確性和速度。

ZXing條碼掃描有哪些特色?

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

IronBarcode支援哪些程式語言?

IronBarcode 支援 C# 和 VB.NET,使其成為在 .NET 框架內工作的開發人員的理想選擇。

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。