構建Barcode SDK C#:通過一個程式庫生成、讀取和掃描條碼
大多數條碼SDK C#專案都從同樣的頭痛開始:拼湊生成、讀取和匯出的單獨程式庫,然後處理各種條碼型別和平台的相容性問題。 IronBarcode完全消除了這種摩擦。 它是一個單一的.NET程式庫,能夠處理開發者通常需要從專用條碼掃描SDK中獲得的每一個條碼操作,從建立線性條碼和QR碼到從不完美的圖像和PDF文件中掃描多個條碼。
本文將帶您了解使IronBarcode成為條碼SDK專案全方位解決方案的核心功能:生成條碼圖像,從文件中讀取條碼資料,以及針對生產質量精度配置先進的掃描設置。 每個後續的程式碼範例都可以在.NET控制台應用中原樣運行。
條碼SDK在.NET專案中應該處理哪些問題?
一個有能力的條碼掃描SDK需要涵蓋三個基本操作:生成條碼,從圖片和文件中讀取條碼資料,以及處理現實世界中的掃描質量問題。 最好的條碼讀取SDK選項還支持多種符號學,而不需要為每一種符號學單獨的程式庫。
IronBarcode支持線性條碼(Code 128、Code 39、UPC-A、UPC-E、EAN-8、EAN-13、GS1 DataBar)和2D條碼(QR碼、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")
生成的條碼圖像

BarcodeWriter類允許將資料作為字串、位元組陣列或流輸入,提供靈活的資料來源選擇。 BarcodeEncoding列舉控制符號學,對於QR碼傳遞BarcodeEncoding.QRCode,對於UPC-A零售標籤傳遞BarcodeEncoding.UPCA,或任何受支持的條碼格式。 對於樣式化的QR碼,專用的QRCodeWriter類支持錯誤更正級別和Logo嵌入,使之能夠簡單地建立品牌程式碼。
GeneratedBarcode物件還提供自訂條碼屬性的方法,如邊距、顏色和註釋文字。 條碼圖像可以以光柵圖像格式(PNG、BMP、JPEG)和向量格式匯出,或直接渲染到HTML中以符合網頁應用情境。 請參看條碼建立範例以獲取更多輸出選項。
條碼讀取器如何在圖像和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
讀取條碼輸出

預設情況下,條碼讀取器在所有主要符號學中執行條碼認識,自動檢測線性條碼、2D條碼和QR碼,而不需要開發者指定要掃描的型別。 在掃描PDF時,ReadPdf會處理每一頁,並返回帶有頁碼的結果,非常適合文件索引和檔案工作流程。
集合中的每個BarcodeResult都提供解碼條碼資料的字串和位元組陣列兩種形式的存取。 這在處理Data Matrix程式碼或其他編碼二進位資料的符號學時特別有用。 條碼讀取器的結果還包括條碼的位置座標,使應用程式能夠映射每個程式碼在源圖像文件中出現的位置。為了在整個文件夾中的圖片批量處理,可將文件路徑的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
噪點條碼圖像掃描的輸出

將ExpectBarcodeTypes設置為特定子集的符號學(而不是掃描所有型別)可以顯著提高速度和準確性。 ReadingSpeed列舉提供四個等級 —— 快速,平衡,詳細和極其詳細,允許開發者調整處理時間和條碼識別徹底性之間的取捨。 ImageFilterCollection在掃描引擎處理條碼圖像之前應用預處理濾鏡,如銳化、對比度調整和自適應閾值。
ExpectMultipleBarcodes標誌告訴引擎在找到第一個匹配後繼續掃描,這在需要掃描單個標籤或文件頁中的多個條碼時至關重要。 與Multithreaded = true結合,該程式庫在CPU內核之間分配批次處理,用於高吞吐量掃描場景。 要深入了解這些設置,讀取條碼教程覆蓋了每個配置選項並附帶範例程式碼。
支持哪些條碼型別和平台?
IronBarcode涵蓋了在零售、物流、醫療和企業應用中最廣泛使用的條碼符號學。 下表總結了支持的條碼型別和目標平台。
| 類別 | 支持的格式 |
|---|---|
| 線性條碼 | Code 128、Code 39、Code 93、UPC-A、UPC-E、EAN-8、EAN-13、GS1 DataBar、ITF、MSI、Codabar |
| 2D條碼 | QR碼、Data Matrix、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 Forms、WPF、ASP.NET、.NET MAUI、Blazor |
| 操作系統 | Windows、macOS、Linux、Android(通過.NET MAUI) |
該程式庫作為單個NuGet包(BarCode)安裝,或通過直接下載作為獨立條碼DLL使用。 不需要本地SDK依賴,整個條碼掃描SDK作為受管理的.NET程式碼提供。 通過NuGet包管理器在Visual Studio中安裝它,或者在CLI中運行.NET 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授權選項,價格$999起,適用於個人開發者 —— 包括Iron Software工程團隊的免費支援。
常見問題
IronBarcode在C#專案中用於什麼用途?
IronBarcode是一個.NET程式庫,可簡化條碼操作,允許開發者在C#專案中產生、讀取和掃描各種條碼型別,消除需要多個程式庫的需求。
IronBarcode能處理多種型別的條碼嗎?
是的,IronBarcode支援廣泛的條碼型別,包括線性條碼和QR碼,確保在不同應用間的相容性。
IronBarcode如何改善C#中的條碼掃描?
IronBarcode通過提供強大的功能來從不完美的影像和PDF檔案中掃描多個條碼,改善準確性和效率以增強條碼掃描。
是否有使用IronBarcode的範例程式碼可用?
是的,包含範例程式碼以幫助開發者快速將條碼產生和掃描功能整合到他們的C#專案中使用IronBarcode。
為何選擇IronBarcode而非單獨的程式庫來進行條碼操作?
IronBarcode將條碼產生、讀取和掃描操作整合到一個程式庫中,減少使用多個程式庫相關的複雜性和相容性問題。




