構建Barcode SDK C#:通過一個程式庫生成、讀取和掃描條碼
大多數條碼 SDK C# 專案一開始都會遇到相同的難題:將用於產生、讀取和匯出的獨立函式庫拼接在一起,然後努力解決條碼類型和平台之間的相容性問題。 IronBarcode 完全消除了這種摩擦。 這是一個單一的 .NET 庫,可以處理開發人員通常需要的專用條碼掃描器 SDK 的所有條碼操作,從創建線性條碼和二維碼到從不完美的圖像和 PDF 文件中掃描多個條碼。
在本文中,我們將帶您了解 IronBarcode 的核心功能,使其成為條碼 SDK 專案的一體化解決方案:產生條碼影像、從檔案中讀取條碼資料以及配置進階掃描設定以實現生產級精度。 以下每個程式碼範例均可在 .NET 控制台應用程式中直接執行。
!{--010011000100100101000010010100100100000101010010010110010101111101001110010101010101010101010101010101010101010 0100010111110100100101001101010100010000010100110001001100010111110100001001001100010011110010101010
條碼 SDK 在 .NET 專案中應該處理哪些內容?
一個功能強大的條碼掃描器 SDK 需要涵蓋三個基本操作:產生條碼、從影像和文件中讀取條碼資料以及處理實際掃描品質問題。 最好的條碼閱讀器 SDK 選項還支援多種條碼體系,而無需為每種條碼體系單獨開發庫。
IronBarcode 支援線性條碼(Code 128、Code 39、UPC-A、UPC-E、EAN-8、EAN-13、GS1 DataBar)和二維條碼(QR Code、Data Matrix、PDF417、Aztec、MaxiCode)。 這意味著 .NET 開發人員可以使用單一條碼 DLL 處理從零售 UPC-A 標籤到倉庫 QR 碼的所有內容,無需額外的依賴項,也無需特定於平台的本機二進位檔案。 免費試用許可證可提供 30 天的完整功能使用權限。
該程式庫可在 Windows、macOS 和 Linux 作業系統上運行,並支援 .NET MAUI 和 Android 應用程式部署。 它還可以與 Crystal Reports 等報表工具集成,用於需要在業務文件中產生嵌入式條碼的場景。 無論是控制台應用程式、.NET MAUI 行動掃描器,還是處理數千張影像的伺服器端條碼讀取器,同一個條碼掃描器 SDK API 都能處理所有這些。
How Can Developers Generate Barcode Images in C#?
BarcodeWriter.CreateBarcode 方法透過一次調用,根據字串值和指定的符號體系產生條碼圖像。 傳回的 GeneratedBarcode 物件可以儲存為 PNG、BMP、JPEG、PDF、HTML 文件,或匯出為 SVG 等向量格式。
using IronBarCode;
// Generate a Code 128 barcode image from string data
var barcode = BarcodeWriter.CreateBarcode("PKG-2025-88421", BarcodeEncoding.Code128);
barcode.AddAnnotationTextAboveBarcode("Shipping Label");
barcode.AddBarcodeValueTextBelowBarcode();
barcode.ResizeTo(500, 150);
barcode.SaveAsPng("shipping-label.png");
// Create a styled QR code with a logo
var qrCode = QRCodeWriter.CreateQrCode("https://example.com/track/88421", 300);
qrCode.ChangeBarCodeColor(IronSoftware.Drawing.Color.DarkSlateGray);
qrCode.SaveAsPng("tracking-qr.png");
using IronBarCode;
// Generate a Code 128 barcode image from string data
var barcode = BarcodeWriter.CreateBarcode("PKG-2025-88421", BarcodeEncoding.Code128);
barcode.AddAnnotationTextAboveBarcode("Shipping Label");
barcode.AddBarcodeValueTextBelowBarcode();
barcode.ResizeTo(500, 150);
barcode.SaveAsPng("shipping-label.png");
// Create a styled QR code with a logo
var qrCode = QRCodeWriter.CreateQrCode("https://example.com/track/88421", 300);
qrCode.ChangeBarCodeColor(IronSoftware.Drawing.Color.DarkSlateGray);
qrCode.SaveAsPng("tracking-qr.png");
Imports IronBarCode
' Generate a Code 128 barcode image from string data
Dim barcode = BarcodeWriter.CreateBarcode("PKG-2025-88421", BarcodeEncoding.Code128)
barcode.AddAnnotationTextAboveBarcode("Shipping Label")
barcode.AddBarcodeValueTextBelowBarcode()
barcode.ResizeTo(500, 150)
barcode.SaveAsPng("shipping-label.png")
' Create a styled QR code with a logo
Dim qrCode = QRCodeWriter.CreateQrCode("https://example.com/track/88421", 300)
qrCode.ChangeBarCodeColor(IronSoftware.Drawing.Color.DarkSlateGray)
qrCode.SaveAsPng("tracking-qr.png")
產生的條碼圖像
建立條碼 SDK C#:使用一個函式庫產生、讀取和掃描條碼:圖 1 - 輸出條碼和二維碼影像
BarcodeWriter 類別接受字串、位元組數組或流作為數據,從而可以靈活地處理不同的資料來源。 BarcodeEncoding 枚舉控制符號體系,傳遞 BarcodeEncoding.QRCode 表示二維碼,傳遞 BarcodeEncoding.UPCA 表示 UPC-A 零售標籤,或任何受支援的條碼格式。 對於樣式化的二維碼,專用的 QRCodeWriter 類別支援糾錯等級和標誌嵌入,從而可以輕鬆建立品牌二維碼。
GeneratedBarcode 物件也提供了自訂條碼屬性(如邊距、顏色和註解文字)的方法。 條碼圖片可以匯出為光柵圖片格式(PNG、BMP、JPEG)和向量格式,或直接渲染為 HTML 以用於 Web 應用程式場景。 有關其他輸出選項,請參閱條碼建立範例。
條碼閱讀器如何讀取影像和 PDF 檔案?
BarcodeReader.Read 方法接受影像檔案路徑、位元組數組、點陣圖或串流,並傳回一個 BarcodeResults 集合,其中包含在輸入中找到的每個條碼。 每個 BarcodeResult 都公開條碼值、編碼類型、頁碼、二進位資料和條碼影像區域。
using IronBarCode;
// Use the barcode reader to decode all barcodes from an image file
var results = BarcodeReader.Read("multiple-barcodes.png");
foreach (var result in results)
{
Console.WriteLine($"Type: {result.BarcodeType} | Value: {result.Value}");
}
// The barcode reader also scans multi-page PDF documents
var pdfResults = BarcodeReader.ReadPdf("invoice-batch.pdf");
foreach (var item in pdfResults)
{
Console.WriteLine($"Page {item.PageNumber}: {item.Value}");
}
using IronBarCode;
// Use the barcode reader to decode all barcodes from an image file
var results = BarcodeReader.Read("multiple-barcodes.png");
foreach (var result in results)
{
Console.WriteLine($"Type: {result.BarcodeType} | Value: {result.Value}");
}
// The barcode reader also scans multi-page PDF documents
var pdfResults = BarcodeReader.ReadPdf("invoice-batch.pdf");
foreach (var item in pdfResults)
{
Console.WriteLine($"Page {item.PageNumber}: {item.Value}");
}
Imports IronBarCode
' Use the barcode reader to decode all barcodes from an image file
Dim results = BarcodeReader.Read("multiple-barcodes.png")
For Each result In results
Console.WriteLine($"Type: {result.BarcodeType} | Value: {result.Value}")
Next
' The barcode reader also scans multi-page PDF documents
Dim pdfResults = BarcodeReader.ReadPdf("invoice-batch.pdf")
For Each item In pdfResults
Console.WriteLine($"Page {item.PageNumber}: {item.Value}")
Next
讀取條碼輸出
建構條碼 SDK C#:使用一個函式庫產生、讀取和掃描條碼:圖 2 - 讀取條碼資料輸出
條碼閱讀器預設對所有主要條碼進行識別,自動偵測線性條碼、二維條碼和二維碼,而無需開發人員指定要掃描的類型。 掃描 PDF 時,ReadPdf 會處理每一頁並傳回帶有頁碼的結果,非常適合文件索引和歸檔工作流程。
集合中的每個 BarcodeResult 都提供了對解碼後的條碼資料的訪問,既可以作為字串,也可以作為位元組數組。 這在處理資料矩陣碼或其他對二進位資料進行編碼的符號系統時尤其有用。 條碼讀取器的結果還包括條碼的位置座標,使應用程式能夠映射每個條碼在來源影像檔案中出現的位置。若要對整個資料夾中的映像進行批次處理,請將檔案路徑清單(IEnumerable<string>)傳遞給啟用多執行緒並行執行的條碼讀取器。
條碼掃描器 SDK 如何處理真實世界的影像品質?
來自倉庫攝影機、行動裝置拍攝或掃描文件的真實條碼影像很少能做到像素完美。 BarcodeReaderOptions 類別可對掃描速度、預期符號系統、影像校正濾鏡和多執行緒批次進行精細控制,即使在輸入損壞或傾斜的情況下也能實現高度精確的條碼識別。
using IronBarCode;
// Configure the barcode reader for challenging, real-world image quality
var options = new BarcodeReaderOptions
{
Speed = ReadingSpeed.Detailed,
ExpectMultipleBarcodes = true,
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional | BarcodeEncoding.QRCode,
Multithreaded = true,
MaxParallelThreads = 4,
ImageFilters = new ImageFilterCollection
{
new SharpenFilter(),
new ContrastFilter()
}
};
// Scan multiple barcodes from a noisy image with high accuracy
var results = BarcodeReader.Read("camera-capture.jpg", options);
foreach (var barcode in results)
{
Console.WriteLine($"Detected: {barcode.BarcodeType} — {barcode.Value}");
}
using IronBarCode;
// Configure the barcode reader for challenging, real-world image quality
var options = new BarcodeReaderOptions
{
Speed = ReadingSpeed.Detailed,
ExpectMultipleBarcodes = true,
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional | BarcodeEncoding.QRCode,
Multithreaded = true,
MaxParallelThreads = 4,
ImageFilters = new ImageFilterCollection
{
new SharpenFilter(),
new ContrastFilter()
}
};
// Scan multiple barcodes from a noisy image with high accuracy
var results = BarcodeReader.Read("camera-capture.jpg", options);
foreach (var barcode in results)
{
Console.WriteLine($"Detected: {barcode.BarcodeType} — {barcode.Value}");
}
Imports IronBarCode
' Configure the barcode reader for challenging, real-world image quality
Dim options As New BarcodeReaderOptions With {
.Speed = ReadingSpeed.Detailed,
.ExpectMultipleBarcodes = True,
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional Or BarcodeEncoding.QRCode,
.Multithreaded = True,
.MaxParallelThreads = 4,
.ImageFilters = New ImageFilterCollection From {
New SharpenFilter(),
New ContrastFilter()
}
}
' Scan multiple barcodes from a noisy image with high accuracy
Dim results = BarcodeReader.Read("camera-capture.jpg", options)
For Each barcode In results
Console.WriteLine($"Detected: {barcode.BarcodeType} — {barcode.Value}")
Next
雜訊條碼影像掃描的輸出
建立條碼 SDK C#:使用一個庫產生、讀取和掃描條碼:圖 3 - 掃描雜訊條碼影像的範例輸出
將 ExpectBarcodeTypes 設定為特定的條碼類型子集(而不是掃描所有類型)可以顯著提高速度和準確性。 ReadingSpeed 枚舉提供了四個層級——更快、更平衡、更詳細和更精細,讓開發人員可以調整處理時間和條碼識別徹底性之間的權衡。 ImageFilterCollection 在掃描引擎處理條碼影像之前,套用銳利化、對比度調整和自適應閾值等預處理濾波器。
ExpectMultipleBarcodes 標誌告訴引擎在找到第一個匹配項後繼續掃描,這在需要掃描單個標籤或文件頁面上的多個條碼時至關重要。 結合 Multithreaded = true,該程式庫可將批次處理分佈到 CPU 核心上,以適應高吞吐量掃描場景。 要深入了解這些設置,讀取條碼教學涵蓋了每個配置選項以及範例程式碼。
支援哪些條碼類型和平台?
IronBarcode涵蓋了零售、物流、醫療保健和企業應用中最廣泛使用的條碼符號系統。 下表總結了支援的條碼類型和目標平台。
| 類別 | 支援的格式 |
|---|---|
| 線性條碼 | 代碼 128、代碼 39、代碼 93、UPC-A、UPC-E、EAN-8、EAN-13、GS1 DataBar、ITF、MSI、Codabar |
| QR 圖條碼 | QR碼、資料矩陣碼、PDF417碼、Aztec碼、MaxiCode碼 |
| 影像格式 | PNG、JPEG、BMP、GIF、TIFF、SVG |
| 文件格式 | PDF(多頁)、HTML |
| .NET 平台 | .NET 8/7/6、.NET Core、.NET Framework 4.6.2+、.NET Standard 2.0+ |
| 應用程式類型 | 控制台、Windows 表單、WPF、ASP.NET、.NET MAUI、Blazor |
| 作業系統 | Windows、macOS、Linux、Android(透過 .NET MAUI) |
該函式庫可以作為單一NuGet 套件(BarCode) 安裝,也可以透過直接下載作為獨立的條碼 DLL 安裝。 無需任何原生 SDK 依賴項,整個條碼掃描器 SDK 以託管的 .NET 程式碼形式提供。 可以透過 NuGet 套件管理器在 Visual Studio 中安裝,或從 CLI 執行 dotnet add package BarCode。 對於需要 DLL 等級控制的部署方案, IronBarcode DLL 下載提供了一個 ZIP 包,可進行手動整合。
IronBarcode 也支援 Crystal Reports 整合和其他需要產生嵌入式條碼的報告工具。 對於 .NET MAUI 和 Android 應用開發,條碼掃描器 SDK 提供跨平台條碼讀取功能,無需特定於平台的相機 SDK——只需將裝置相機拍攝的影像檔案傳遞給條碼讀取器即可。 .NET MAUI 條碼掃描器教學詳細介紹了 .NET MAUI 工作流程,包括 Android 權限和行動掃描的範例程式碼。
接下來該何去何從
IronBarcode 為 .NET 開發人員提供了一個完整的條碼庫,適用於 SDK 層級的項目,其中包含產生、讀取、大量掃描和匯出等功能,而無需管理多個套件的複雜性。 最新版本增加了 ML 驅動的影像預處理和用於批量 PDF 處理的 ReadPdfs 方法,繼續拓展單一庫的功能。
立即開始免費試用,在您自己的專案中測試所有功能,無浮水印或限制。 當您準備好投入生產時,請探索IronBarcode 的許可選項,個人開發者可從 $799 開始,並可獲得 Iron Software 工程團隊的免費支援。
!{--01001100010010010100001001010010010000010101001001011001010111110100011101000101010101 01000101111101010011010101000100000101010010010101000100010101000100010111110101011101001000110 1010101000100100001011111010100000101001001001111010001000101010101010000110101010100101010101011 10101010001010010010010010010000010100110001011111010000100100110001001111101000011010010111111010000110100101110--
常見問題解答
IronBarcode在C#專案中用於什麼?
IronBarcode是一個.NET程式庫,透過允許開發者在C#專案中生成、讀取和掃描各種條碼類型,簡化了條碼操作,不再需要多個程式庫。
IronBarcode能處理多種條碼類型嗎?
是的,IronBarcode支援廣泛的條碼類型,包括線性條碼和QR碼,確保在不同應用程式中的相容性。
IronBarcode如何改善C#中的條碼掃描?
IronBarcode通過提供強大的功能以從不完美的圖像和PDF檔案中掃描多個條碼來提高條碼掃描的準確性和效率。
IronBarcode的使用是否有示例程式碼?
是的,包含的示例代碼可以幫助開發者迅速將條碼生成和掃描功能整合到其C#專案中。
為什麼選擇IronBarcode而不是分別的程式庫來進行條碼操作?
IronBarcode將條碼生成、讀取和掃描操作整合到一個程式庫中,減少了使用多個程式庫時的複雜性和相容性問題。

