與其他組件的比較 如何在ZXing中為C#開發人員掃描條碼 Jordi Bardia 更新:7月 28, 2025 下載 IronBarcode NuGet 下載 DLL 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 條碼提供了一種清晰且機器可讀的資料呈現方式。 最初,條碼由寬度和間距各異的平行線組成,用於表示資料。 這些傳統的線性或一維(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"); 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 ,我們將條碼格式設定為二維碼。 我們將先前建立的二維碼選項指派給編寫者。 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 套件管理器控制台中使用以下命令: 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"); 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 這段程式碼使用中等程度的糾錯產生 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() $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 在效率方面表現優異,並與各種作業系統相容。 如何使用行動裝置掃描 BarCode? 使用 ZXing Android 用戶端條碼掃描器,您可以利用裝置的相機掃描條碼。如需更強大的功能,可將 IronBarcode 整合至行動應用程式,以增強條碼掃描功能。 IronBarcode 支援二維條碼嗎? 是的,IronBarcode 支持一维和二维条码,包括 QR 码,允许多功能条码读取和创建。 IronBarcode 可以處理動態條碼創建嗎? IronBarcode 支援動態條碼建立,可讓您自訂條碼的顏色、大小、間距和字樣,以符合特定需求。 將 IronBarcode 整合到 .NET 專案中需要哪些條件? 要將 IronBarcode 整合到 .NET 專案中,請使用 Install-Package IronBarcode 透過 NuGet Package Manager Console 安裝套件,或在 NuGet Package Manager 中找到它。 使用 IronBarcode 需要付費嗎? IronBarcode 提供免費試用,但購買授權可獲得額外功能、產品更新和一年的支援。 IronBarcode 可以用來從視訊畫面讀取條碼嗎? 是的,IronBarcode 可以處理即時視訊畫面,修正旋轉、雜訊、扭曲和傾斜,以提高條碼掃描的精確度和速度。 ZXing 用於條碼掃描的功能有哪些? ZXing 提供條碼掃描的開放原始碼工具包,特別是針對一維和二維條碼。它包括一個核心影像解碼函式庫和一個 Android 用戶端 BarCode Scanner。 IronBarcode 支援哪些程式語言? IronBarcode 支援 C# 和 VB.NET,非常適合在 .NET Framework 內工作的開發人員。 Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 更新9月 25, 2025 如何在C#中選擇最佳條碼庫 在本指南中,我們將比較五個最廣泛使用的 .NET 條碼庫 — IronBarcode, http://ZXing.Net , Aspose.BarCode, BarcodeLib, 和 Dynamsoft Barcode Reader 閱讀更多 更新8月 31, 2025 ZXing.org QR碼庫和IronBarcode:全面的比較 ZXing是一個流行的開源庫,用於生成和解碼一維和二維條碼。 閱讀更多 更新8月 20, 2025 ZXing解碼器與IronBarcode的比較 在本教程中,我們將同時使用ZXing解碼器在線和IronBarcode來在C# .NET Web應用程式中解碼條碼。 閱讀更多 如何在C#中選擇最佳條碼庫ZXing.org QR碼庫和IronBarcode:...
更新9月 25, 2025 如何在C#中選擇最佳條碼庫 在本指南中,我們將比較五個最廣泛使用的 .NET 條碼庫 — IronBarcode, http://ZXing.Net , Aspose.BarCode, BarcodeLib, 和 Dynamsoft Barcode Reader 閱讀更多
更新8月 20, 2025 ZXing解碼器與IronBarcode的比較 在本教程中,我們將同時使用ZXing解碼器在線和IronBarcode來在C# .NET Web應用程式中解碼條碼。 閱讀更多