如何在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 文件,允許您輸入程式碼並建立或執行它。
2.1 安裝 ZXing 條碼
在 NuGet 套件管理器控制台中輸入以下命令以安裝 ZXing 庫:
Install-Package ZXing.Net.Bindings.Windows.Compatibility
或者,您可以使用 NuGet 套件管理器工具來取得該套件。 如圖所示。 嘗試安裝你選擇的第一個結果。
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")
上面的程式碼設定了 QrCodeEncodingOptions 的高度和寬度。 然後它創建了 BarcodeWriter 的實例。 對於 BarcodeWriter,我們將條碼格式設定為 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
在上面的程式碼中,我們首先將圖像載入到位圖,然後建立一個 BarcodeReader 物件。 Decode 函數允許我們將點陣圖作為參數傳遞,它可以傳回多種格式的結果。 我們使用 Text 屬性來取得條碼中編碼的文字。
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 套件管理器,它會顯示所有搜尋結果,以便尋找和下載"條碼"套件。 然後您可以從中選擇要下載到程式中的必要軟體包。
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")
這段程式碼使用中等程度的糾錯產生 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();
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()
我們首先將映像載入到位圖中,然後使用 Read 類別上的 BarcodeReader 方法讀取映像。 我們使用從 Read 方法傳回的 BarcodeResults 物件上的 Values 屬性來取得從二維碼讀取的內容。
要了解更多關於 ZXing 以及它與 IronBarcode 的比較,請閱讀下一篇部落格文章。
我們的"讀取條碼"教學課程還提供了有關如何使用 IronBarcode 讀取條碼和二維碼的更多資訊。 更多代碼教學請造訪 IronBarcode。
結論
ZXing條碼掃描器可以產生高品質的條碼,但它已經過時,支援的條碼格式也比較少。 此外,它的文件和產品支援也十分有限。
另一方面,IronBarcode 非常有效率且靈活,能夠在多種作業系統上運作。 IronBarcode 可以變更條碼的顏色、大小、間距和字型。 它還支援 Crystal Reports。
開發者可以免費使用 IronBarcode 。 用戶可以購買許可證來存取更多功能,並獲得一整年的支援和產品更新。
常見問題解答
怎樣在 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框架內工作的開發人員的理想選擇。

