如何在 C# 中從資料建立條碼
從LEADTOOLS Barcode遷移到IronBarcode
本指南涵蓋了從LEADTOOLS Barcode遷移到IronBarcode的完整過程,適用於.NET開發人員。 它涉及去除LEADTOOLS NuGet套件和一個本地執行時依賴,將基於文件的LEADTOOLS授權初始化序列替換為字串金鑰,以及將LEADTOOLS的讀寫模式翻譯為IronBarcode的靜態流暢API。 為每個主要遷移場景提供了程式碼範例,還包括API映射表、結構化檢查清單,以及在過渡期間可能出現的問題的指導。
為什麼從LEADTOOLS Barcode遷移
基於文件的授權部署:LEADTOOLS授權需要在每台執行應用程式的計算機上磁碟上有一個.LIC文件,並且在已知路徑下。 在開發者工作站、CI構建代理、預備伺服器和生產容器上都需要配置該文件,然後應用程式才能初始化。這與基於字串的秘密根本不同,其可以乾淨地融入到環境變數、秘密管理器和CI/CD管道配置中。 每新增一個部署拓撲環境——例如新的雲區域、新的Kubernetes節點池、新的開發者上船——都需要一步文件配置,而基於字串的授權則不需要。
SDK足跡:安裝LEADTOOLS條碼需要Leadtools.Barcode NuGet套件(它會拉取Leadtools.Codecs和相關依賴)。 PDF條碼提取會增加Leadtools.Pdf。 在Windows上,MSVC++ 2017執行時是一個單獨的本地依賴。 LEADTOOLS條碼應用程式的發布輸出大約是148 MB。 只需要條碼讀取或生成的應用程式也會承擔一個全面的影像平台的全部負擔——OCR 基礎結構、格式編解碼器架構和文件查看功能——無論是否使用這些功能。
遺留API設計:LEADTOOLS條碼操作要求在執行任何工作之前按特定順序構造多個物件。 讀取條碼需要一個BarcodeSymbology陣列來聲明要尋找的格式。 生成條碼需要構建一個RasterCodecs保存。 該API反映了其時代的設計模式。 IronBarcode通過靜態方法調用直接接受文件路徑來執行相同的操作。
價格透明度:LEADTOOLS的生產伺服器部署授權通過LEADTOOLS銷售單獨報價,並未在其網站上發布。需要在開發開始前得到完整成本預估的團隊必須先與銷售接洽,然後才能確認LEADTOOLS是否符合預算。 IronBarcode提供覆蓋開發和部署的永久一次性授權,無需單獨的生產許可。
基本問題
LEADTOOLS初始化序列在建立BarcodeEngine之前需要20多行和四個驗證步驟。 IronBarcode使用單個賦值替換整個序列:
// LEADTOOLS: file path, expiry check, three feature lock checks, engine creation
using Leadtools;
using Leadtools.Barcode;
RasterSupport.SetLicense(
@"C:\LEADTOOLS23\Support\Common\License\LEADTOOLS.LIC",
"your-developer-key-here");
if (RasterSupport.KernelExpired)
throw new InvalidOperationException("LEADTOOLS license has expired");
if (RasterSupport.IsLocked(RasterSupportType.Barcode1DRead))
throw new InvalidOperationException("1D barcode reading is locked");
if (RasterSupport.IsLocked(RasterSupportType.Barcode2DRead))
throw new InvalidOperationException("2D barcode reading is locked");
if (RasterSupport.IsLocked(RasterSupportType.BarcodeWrite))
throw new InvalidOperationException("Barcode writing is locked");
var engine = new BarcodeEngine();
// LEADTOOLS: file path, expiry check, three feature lock checks, engine creation
using Leadtools;
using Leadtools.Barcode;
RasterSupport.SetLicense(
@"C:\LEADTOOLS23\Support\Common\License\LEADTOOLS.LIC",
"your-developer-key-here");
if (RasterSupport.KernelExpired)
throw new InvalidOperationException("LEADTOOLS license has expired");
if (RasterSupport.IsLocked(RasterSupportType.Barcode1DRead))
throw new InvalidOperationException("1D barcode reading is locked");
if (RasterSupport.IsLocked(RasterSupportType.Barcode2DRead))
throw new InvalidOperationException("2D barcode reading is locked");
if (RasterSupport.IsLocked(RasterSupportType.BarcodeWrite))
throw new InvalidOperationException("Barcode writing is locked");
var engine = new BarcodeEngine();
Imports Leadtools
Imports Leadtools.Barcode
' LEADTOOLS: file path, expiry check, three feature lock checks, engine creation
RasterSupport.SetLicense("C:\LEADTOOLS23\Support\Common\License\LEADTOOLS.LIC", "your-developer-key-here")
If RasterSupport.KernelExpired Then
Throw New InvalidOperationException("LEADTOOLS license has expired")
End If
If RasterSupport.IsLocked(RasterSupportType.Barcode1DRead) Then
Throw New InvalidOperationException("1D barcode reading is locked")
End If
If RasterSupport.IsLocked(RasterSupportType.Barcode2DRead) Then
Throw New InvalidOperationException("2D barcode reading is locked")
End If
If RasterSupport.IsLocked(RasterSupportType.BarcodeWrite) Then
Throw New InvalidOperationException("Barcode writing is locked")
End If
Dim engine As New BarcodeEngine()
// IronBarcode: one line, no file required
using IronBarCode;
IronBarCode.License.LicenseKey = Environment.GetEnvironmentVariable("IRONBARCODE_LICENSE");
// IronBarcode: one line, no file required
using IronBarCode;
IronBarCode.License.LicenseKey = Environment.GetEnvironmentVariable("IRONBARCODE_LICENSE");
Imports IronBarCode
IronBarCode.License.LicenseKey = Environment.GetEnvironmentVariable("IRONBARCODE_LICENSE")
IronBarcodevsLEADTOOLSBarcode:功能比較
| 功能 | LEADTOOLS Barcode | IronBarcode |
|---|---|---|
| 授權模型 | 文件+開發者金鑰 | 僅字串金鑰 |
| 需要的NuGet套件 | 5+ | 1 |
| 本地執行時依賴 | MSVC++ 2017 (Windows) | None |
| 發布輸出大小 | ~148 MB | ~39 MB |
| 支持的總符號數 | 40+ | 50+ |
| 自動檢測條碼格式 | 有限 | 是 |
| PDF條碼提取 | 需要單獨的包 | 內建 |
| 機器學習錯誤更正 | 否 | 是 |
| QR碼logo品牌化 | 否 | 是 |
| Docker部署 | 需要掛載文件 | 環境變數 |
| 初始化程式碼 | 20+行 | 1行 |
| API風格 | 遺留物件圖 | 靜態流暢 |
| 跨平台 | 部分(本地依賴) | 完整(.NET Standard) |
| 部署授權定價 | 聯繫銷售(未公開) | 包含在永久購買中 |
快速開始:從LEADTOOLS Barcode遷移到IronBarcode
步驟1:移除LEADTOOLS套件
從項目中移除所有LEADTOOLS套件:
dotnet remove package Leadtools.Barcode
dotnet remove package Leadtools
dotnet remove package Leadtools.Codecs
dotnet remove package Leadtools.Codecs.Png
dotnet remove package Leadtools.Codecs.Jpeg
dotnet remove package Leadtools.Barcode
dotnet remove package Leadtools
dotnet remove package Leadtools.Codecs
dotnet remove package Leadtools.Codecs.Png
dotnet remove package Leadtools.Codecs.Jpeg
如果PDF編解碼器已為從PDF文件中提取條碼而新增:
dotnet remove package Leadtools.Pdf
dotnet remove package Leadtools.Pdf
在Windows Server部署中,遷移完成後可從提供腳本中移除MSVC++ 2017執行時。 IronBarcode在任何平台上都沒有本地運行時依賴。
步驟2:安裝 IronBarcode
dotnet add package IronBarcode
dotnet add package IronBarcode
這個單一套件包括所有圖像格式支持、本機PDF條碼提取、機器學習錯誤更正和所有50+支持的符號學。 不需要額外的編解碼器套件。
步驟3:初始化授權
用一行代替整個LEADTOOLS初始化區塊。找到程式碼庫中每個對RasterSupport.SetLicense的調用——通常只有一個,在應用啟動時——替換整個周圍的初始化序列:
// Before: entireLEADTOOLSinitialization block (20+ lines)
// After:
using IronBarCode;
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";
// Before: entireLEADTOOLSinitialization block (20+ lines)
// After:
using IronBarCode;
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";
Imports IronBarCode
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY"
授權金鑰可以從任何支持字串的配置機制中獲取:環境變數、appsettings.json、Azure Key Vault、AWS Secrets Manager或Kubernetes秘密。 對於基於環境的授權配置、Docker部署模式和所有支持的配置方法,請參見授權金鑰設置文件。
程式碼遷移範例
從圖像中讀取條碼
LEADTOOLS將圖像載入和條碼掃描分為兩個不同的对象層。 一個BarcodeEngine實例進行掃描。 調用者還必須提供一個明確的BarcodeSymbology陣列; 未在陣列中列出的格式將不被檢測。
LEADTOOLS方法:
using Leadtools;
using Leadtools.Barcode;
using Leadtools.Codecs;
using var codecs = new RasterCodecs();
using var image = codecs.Load("scan.png");
var engine = new BarcodeEngine();
var symbologies = new[]
{
BarcodeSymbology.Code128,
BarcodeSymbology.Code39,
BarcodeSymbology.QR,
BarcodeSymbology.DataMatrix,
BarcodeSymbology.EAN13,
BarcodeSymbology.UPCA
};
var barcodes = engine.Reader.ReadBarcodes(
image,
LogicalRectangle.Empty,
0,
symbologies);
foreach (var barcode in barcodes)
Console.WriteLine($"{barcode.Symbology}: {barcode.Value}");
using Leadtools;
using Leadtools.Barcode;
using Leadtools.Codecs;
using var codecs = new RasterCodecs();
using var image = codecs.Load("scan.png");
var engine = new BarcodeEngine();
var symbologies = new[]
{
BarcodeSymbology.Code128,
BarcodeSymbology.Code39,
BarcodeSymbology.QR,
BarcodeSymbology.DataMatrix,
BarcodeSymbology.EAN13,
BarcodeSymbology.UPCA
};
var barcodes = engine.Reader.ReadBarcodes(
image,
LogicalRectangle.Empty,
0,
symbologies);
foreach (var barcode in barcodes)
Console.WriteLine($"{barcode.Symbology}: {barcode.Value}");
Imports Leadtools
Imports Leadtools.Barcode
Imports Leadtools.Codecs
Dim codecs As New RasterCodecs()
Using codecs
Dim image = codecs.Load("scan.png")
Using image
Dim engine As New BarcodeEngine()
Dim symbologies = New BarcodeSymbology() {
BarcodeSymbology.Code128,
BarcodeSymbology.Code39,
BarcodeSymbology.QR,
BarcodeSymbology.DataMatrix,
BarcodeSymbology.EAN13,
BarcodeSymbology.UPCA
}
Dim barcodes = engine.Reader.ReadBarcodes(
image,
LogicalRectangle.Empty,
0,
symbologies)
For Each barcode In barcodes
Console.WriteLine($"{barcode.Symbology}: {barcode.Value}")
Next
End Using
End Using
IronBarcode 方法:
using IronBarCode;
var results = BarcodeReader.Read("scan.png");
foreach (var result in results)
Console.WriteLine($"{result.Format}: {result.Value}");
using IronBarCode;
var results = BarcodeReader.Read("scan.png");
foreach (var result in results)
Console.WriteLine($"{result.Format}: {result.Value}");
Imports IronBarCode
Dim results = BarcodeReader.Read("scan.png")
For Each result In results
Console.WriteLine($"{result.Format}: {result.Value}")
Next
IronBarcode直接接受文件路徑,內部載入圖像,並自動檢測所有50+支持的符號學的格式,而不要求調用者列舉預期型別。對於高級配置——閱讀速度調整、多條碼檢測和低質量圖像處理——請參閱從圖像中讀取條碼指南。
生成一個Code 128條碼
LEADTOOLS生成需要構建一個RasterCodecs保存。 IronBarcode通過單一流暢的方法鏈執行所有這些步驟。
LEADTOOLS方法:
using Leadtools;
using Leadtools.Barcode;
using Leadtools.Codecs;
var engine = new BarcodeEngine();
var barcodeData = new BarcodeData()
{
Symbology = BarcodeSymbology.Code128,
Value = "ITEM-98765",
Bounds = new LeadRect(0, 0, 500, 120)
};
using var image = new RasterImage(
RasterMemoryFlags.Conventional,
500, 120, 24,
RasterByteOrder.Bgr,
RasterViewPerspective.TopLeft,
null, IntPtr.Zero, 0);
new FillCommand(RasterColor.White).Run(image);
engine.Writer.WriteBarcode(image, barcodeData, null);
using var codecs = new RasterCodecs();
codecs.Save(image, "item-barcode.png", RasterImageFormat.Png, 0);
using Leadtools;
using Leadtools.Barcode;
using Leadtools.Codecs;
var engine = new BarcodeEngine();
var barcodeData = new BarcodeData()
{
Symbology = BarcodeSymbology.Code128,
Value = "ITEM-98765",
Bounds = new LeadRect(0, 0, 500, 120)
};
using var image = new RasterImage(
RasterMemoryFlags.Conventional,
500, 120, 24,
RasterByteOrder.Bgr,
RasterViewPerspective.TopLeft,
null, IntPtr.Zero, 0);
new FillCommand(RasterColor.White).Run(image);
engine.Writer.WriteBarcode(image, barcodeData, null);
using var codecs = new RasterCodecs();
codecs.Save(image, "item-barcode.png", RasterImageFormat.Png, 0);
Imports Leadtools
Imports Leadtools.Barcode
Imports Leadtools.Codecs
Dim engine As New BarcodeEngine()
Dim barcodeData As New BarcodeData() With {
.Symbology = BarcodeSymbology.Code128,
.Value = "ITEM-98765",
.Bounds = New LeadRect(0, 0, 500, 120)
}
Using image As New RasterImage(
RasterMemoryFlags.Conventional,
500, 120, 24,
RasterByteOrder.Bgr,
RasterViewPerspective.TopLeft,
Nothing, IntPtr.Zero, 0)
Dim fillCommand As New FillCommand(RasterColor.White)
fillCommand.Run(image)
engine.Writer.WriteBarcode(image, barcodeData, Nothing)
Using codecs As New RasterCodecs()
codecs.Save(image, "item-barcode.png", RasterImageFormat.Png, 0)
End Using
End Using
IronBarcode 方法:
using IronBarCode;
BarcodeWriter.CreateBarcode("ITEM-98765", BarcodeEncoding.Code128)
.ResizeTo(500, 120)
.SaveAsPng("item-barcode.png");
using IronBarCode;
BarcodeWriter.CreateBarcode("ITEM-98765", BarcodeEncoding.Code128)
.ResizeTo(500, 120)
.SaveAsPng("item-barcode.png");
Imports IronBarCode
BarcodeWriter.CreateBarcode("ITEM-98765", BarcodeEncoding.Code128) _
.ResizeTo(500, 120) _
.SaveAsPng("item-barcode.png")
查看尺寸調整、樣式設置、邊距控制和支持的輸出格式的完整指南,請參見建立條碼圖像。
生成帶有Logo品牌的QR碼
LEADTOOLS QR碼生成遵循與其他條碼型別相同的多步AddBrandLogo方法,用於在QR碼的中心嵌入Logo。
LEADTOOLS方法:
using Leadtools;
using Leadtools.Barcode;
using Leadtools.Codecs;
var engine = new BarcodeEngine();
var qrData = new BarcodeData()
{
Symbology = BarcodeSymbology.QR,
Value = "https://example.com/product/12345",
Bounds = new LeadRect(0, 0, 400, 400)
};
using var image = new RasterImage(
RasterMemoryFlags.Conventional,
400, 400, 24,
RasterByteOrder.Bgr,
RasterViewPerspective.TopLeft,
null, IntPtr.Zero, 0);
new FillCommand(RasterColor.White).Run(image);
engine.Writer.WriteBarcode(image, qrData, null);
using var codecs = new RasterCodecs();
codecs.Save(image, "product-qr.png", RasterImageFormat.Png, 0);
using Leadtools;
using Leadtools.Barcode;
using Leadtools.Codecs;
var engine = new BarcodeEngine();
var qrData = new BarcodeData()
{
Symbology = BarcodeSymbology.QR,
Value = "https://example.com/product/12345",
Bounds = new LeadRect(0, 0, 400, 400)
};
using var image = new RasterImage(
RasterMemoryFlags.Conventional,
400, 400, 24,
RasterByteOrder.Bgr,
RasterViewPerspective.TopLeft,
null, IntPtr.Zero, 0);
new FillCommand(RasterColor.White).Run(image);
engine.Writer.WriteBarcode(image, qrData, null);
using var codecs = new RasterCodecs();
codecs.Save(image, "product-qr.png", RasterImageFormat.Png, 0);
Imports Leadtools
Imports Leadtools.Barcode
Imports Leadtools.Codecs
Dim engine As New BarcodeEngine()
Dim qrData As New BarcodeData() With {
.Symbology = BarcodeSymbology.QR,
.Value = "https://example.com/product/12345",
.Bounds = New LeadRect(0, 0, 400, 400)
}
Using image As New RasterImage(
RasterMemoryFlags.Conventional,
400, 400, 24,
RasterByteOrder.Bgr,
RasterViewPerspective.TopLeft,
Nothing, IntPtr.Zero, 0)
Dim fillCommand As New FillCommand(RasterColor.White)
fillCommand.Run(image)
engine.Writer.WriteBarcode(image, qrData, Nothing)
Using codecs As New RasterCodecs()
codecs.Save(image, "product-qr.png", RasterImageFormat.Png, 0)
End Using
End Using
IronBarcode 方法:
using IronBarCode;
QRCodeWriter.CreateQrCode("https://example.com/product/12345", 400, QRCodeWriter.QrErrorCorrectionLevel.Highest)
.AddBrandLogo("brand-logo.png")
.SaveAsPng("product-qr.png");
using IronBarCode;
QRCodeWriter.CreateQrCode("https://example.com/product/12345", 400, QRCodeWriter.QrErrorCorrectionLevel.Highest)
.AddBrandLogo("brand-logo.png")
.SaveAsPng("product-qr.png");
Imports IronBarCode
QRCodeWriter.CreateQrCode("https://example.com/product/12345", 400, QRCodeWriter.QrErrorCorrectionLevel.Highest) _
.AddBrandLogo("brand-logo.png") _
.SaveAsPng("product-qr.png")
從PDF文件中提取條碼
LEADTOOLS PDF條碼提取需要單獨的Leadtools.Pdf套件、明確的PDF載入配置、手動頁數查詢和每頁迴圈。 必須將每頁單獨載入為RasterImage才能掃描。 IronBarcode在基礎包中包括PDF支持,並內部處理頁面迭代。
LEADTOOLS方法:
using Leadtools;
using Leadtools.Barcode;
using Leadtools.Codecs;
// Requires: dotnet add package Leadtools.Pdf
using var codecs = new RasterCodecs();
codecs.Options.Pdf.Load.Resolution = 300;
codecs.Options.Pdf.Load.DisplayDepth = 24;
var engine = new BarcodeEngine();
var results = new List<string>();
int pageCount = codecs.GetTotalPages("shipment.pdf");
for (int page = 1; page <= pageCount; page++)
{
using var image = codecs.Load(
"shipment.pdf", 0,
CodecsLoadByteOrder.BgrOrGray,
page, page);
var barcodes = engine.Reader.ReadBarcodes(
image, LogicalRectangle.Empty, 0, null);
foreach (var barcode in barcodes)
results.Add($"Page {page}: {barcode.Value}");
}
using Leadtools;
using Leadtools.Barcode;
using Leadtools.Codecs;
// Requires: dotnet add package Leadtools.Pdf
using var codecs = new RasterCodecs();
codecs.Options.Pdf.Load.Resolution = 300;
codecs.Options.Pdf.Load.DisplayDepth = 24;
var engine = new BarcodeEngine();
var results = new List<string>();
int pageCount = codecs.GetTotalPages("shipment.pdf");
for (int page = 1; page <= pageCount; page++)
{
using var image = codecs.Load(
"shipment.pdf", 0,
CodecsLoadByteOrder.BgrOrGray,
page, page);
var barcodes = engine.Reader.ReadBarcodes(
image, LogicalRectangle.Empty, 0, null);
foreach (var barcode in barcodes)
results.Add($"Page {page}: {barcode.Value}");
}
Imports Leadtools
Imports Leadtools.Barcode
Imports Leadtools.Codecs
' Requires: dotnet add package Leadtools.Pdf
Using codecs As New RasterCodecs()
codecs.Options.Pdf.Load.Resolution = 300
codecs.Options.Pdf.Load.DisplayDepth = 24
Dim engine As New BarcodeEngine()
Dim results As New List(Of String)()
Dim pageCount As Integer = codecs.GetTotalPages("shipment.pdf")
For page As Integer = 1 To pageCount
Using image = codecs.Load("shipment.pdf", 0, CodecsLoadByteOrder.BgrOrGray, page, page)
Dim barcodes = engine.Reader.ReadBarcodes(image, LogicalRectangle.Empty, 0, Nothing)
For Each barcode In barcodes
results.Add($"Page {page}: {barcode.Value}")
Next
End Using
Next
End Using
IronBarcode 方法:
using IronBarCode;
var results = BarcodeReader.Read("shipment.pdf");
foreach (var result in results)
Console.WriteLine($"Page {result.PageNumber}: {result.Value}");
using IronBarCode;
var results = BarcodeReader.Read("shipment.pdf");
foreach (var result in results)
Console.WriteLine($"Page {result.PageNumber}: {result.Value}");
Imports IronBarCode
Dim results = BarcodeReader.Read("shipment.pdf")
For Each result In results
Console.WriteLine($"Page {result.PageNumber}: {result.Value}")
Next
Docker部署
當遷移到IronBarcode時,定義每個LEADTOOLS Docker部署的文件管理要求被完全消除。
LEADTOOLS方法:
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY publish/ .
# License file must be physically present in the container image
COPY LEADTOOLS.LIC /app/license/LEADTOOLS.LIC
ENV LEADTOOLS_LICENSE_PATH=/app/license/LEADTOOLS.LIC
ENV LEADTOOLS_DEVELOPER_KEY=your-developer-key
ENTRYPOINT ["dotnet", "YourApp.dll"]
IronBarcode 方法:
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY publish/ .
ENV IRONBARCODE_LICENSE=your-license-key
ENTRYPOINT ["dotnet", "YourApp.dll"]
授權金鑰在容器運行時通過Docker秘密、Kubernetes秘密或任何環境變數機制注入。 不需要文件配置步驟。 Docker和Linux部署指南涵蓋Alpine和Debian基礎映像、多階段構建和Kubernetes部署模式。
LEADTOOLS BarcodeAPI至IronBarcode映射參考
| LEADTOOLS | IronBarcode | 注意事項 |
|---|---|---|
RasterSupport.SetLicense(path, key) |
IronBarCode.License.LicenseKey = "key" |
僅金鑰——無需文件 |
RasterSupport.KernelExpired |
(removed) | 不需要到期檢查 |
RasterSupport.IsLocked(RasterSupportType.Barcode1DRead) |
(removed) | 所有功能均包括 |
RasterSupport.IsLocked(RasterSupportType.Barcode2DRead) |
(removed) | 所有功能均包括 |
RasterSupport.IsLocked(RasterSupportType.BarcodeWrite) |
(removed) | 所有功能均包括 |
new BarcodeEngine() |
靜態——無需實例 | BarcodeReader, BarcodeWriter是靜態的 |
new RasterCodecs() |
(removed) | 直接傳遞文件路徑 |
codecs.Load(imagePath) |
(removed) | 直接傳遞文件路徑 |
engine.Reader.ReadBarcodes(image, LogicalRectangle.Empty, 0, symbologies) |
BarcodeReader.Read(imagePath) |
自動檢測符號學 |
BarcodeData.Value |
result.Value |
相同的屬性名稱 |
BarcodeData.Symbology |
result.Format |
屬性已重命名 |
new BarcodeData() { Symbology = BarcodeSymbology.Code128 } |
BarcodeWriter.CreateBarcode(data, BarcodeEncoding.Code128) |
流暢建立 |
BarcodeSymbology.Code128 |
BarcodeEncoding.Code128 |
命名空間更改 |
BarcodeSymbology.QR |
BarcodeEncoding.QRCode |
名稱更改 |
BarcodeSymbology.DataMatrix |
BarcodeEncoding.DataMatrix |
相同名稱 |
BarcodeSymbology.PDF417 |
BarcodeEncoding.PDF417 |
相同名稱 |
BarcodeSymbology.EAN13 |
BarcodeEncoding.EAN13 |
相同名稱 |
BarcodeSymbology.UPCA |
BarcodeEncoding.UPCA |
相同名稱 |
BarcodeSymbology.Interleaved2of5 |
BarcodeEncoding.Interleaved2of5 |
相同名稱 |
BarcodeSymbology.Codabar |
BarcodeEncoding.Codabar |
相同名稱 |
engine.Writer.WriteBarcode(image, data, null) + codecs.Save(...) |
.SaveAsPng(path) |
單一方法鏈 |
new RasterImage(...) + new FillCommand(RasterColor.White).Run(image) |
(removed) | 內部至IronBarcode |
常見遷移問題及解決方案
問題1:.LIC文件在CI/CD中的引用
LEADTOOLS:構建或部署LEADTOOLS應用程式的CI/CD管道必須配置.LIC文件——可以將其檢入源程式碼控制、從base64祕密解碼,或作為卷工件掛載。 運行應用程式的每個管道階段都需要存取該文件。
解決方案:從管道中移除所有.LIC文件處理。替換為字串秘密:
# Example: GitHub Actions environment variable
env:
IRONBARCODE_LICENSE: ${{secrets.IRONBARCODE_LICENSE}}
# Example: GitHub Actions environment variable
env:
IRONBARCODE_LICENSE: ${{secrets.IRONBARCODE_LICENSE}}
字串值儲存在CI/CD祕密儲存中,與其他字串值的憑證放在一起。 不需要文件解碼、不需要工件配置、不需要卷配置。
問題2:Windows上的MSVC++運行時依賴
LEADTOOLS:Windows Server部署需要在主機上安裝MSVC++ 2017運行時。LEADTOOLS部署中使用的提供腳本、容器基礎映像和Windows AMI通常包括一個安裝此運行時的步驟。
解決方案:從所有Windows提供腳本和容器映像中移除MSVC++ 2017運行時安裝步驟。 IronBarcode沒有本地運行時依賴。 dotnet/aspnet基礎映像足以,不需要其他運行時包。
問題3:RasterSupportType功能鎖定檢查
LEADTOOLS:初始化序列包括BarcodeWrite。 這些檢查是LEADTOOLS架構要求的防禦程式碼,用於驗證授權文件覆蓋每個所需功能。
解決方案:移除所有RasterSupport.IsLocked()調用。 IronBarcode包括所有讀寫能力在單一授權中。 沒有每個功能的鎖定狀態,也不需要功能驗證步驟:
// Remove these lines entirely:
// if (RasterSupport.IsLocked(RasterSupportType.Barcode1DRead)) ...
// if (RasterSupport.IsLocked(RasterSupportType.Barcode2DRead)) ...
// if (RasterSupport.IsLocked(RasterSupportType.BarcodeWrite)) ...
// Remove these lines entirely:
// if (RasterSupport.IsLocked(RasterSupportType.Barcode1DRead)) ...
// if (RasterSupport.IsLocked(RasterSupportType.Barcode2DRead)) ...
// if (RasterSupport.IsLocked(RasterSupportType.BarcodeWrite)) ...
' Remove these lines entirely:
' If RasterSupport.IsLocked(RasterSupportType.Barcode1DRead) ...
' If RasterSupport.IsLocked(RasterSupportType.Barcode2DRead) ...
' If RasterSupport.IsLocked(RasterSupportType.BarcodeWrite) ...
問題4:BarcodeSymbology陣列移除
LEADTOOLS:對BarcodeSymbology[]參數。 未在陣列中列出的條碼符號學不會被檢測到,這可能導致當輸入文件中引入新的條碼型別時,出現無聲的錯過讀取。
解決方案:移除符號學陣列。 BarcodeReader.Read()自動檢測所有支持的格式。 如果對於已知格式集需要性能優化,BarcodeReaderOptions提供可選提示,而不需要詳盡列舉:
// Optional: hint the reader for performance in format-known scenarios
var options = new BarcodeReaderOptions
{
ExpectBarcodeTypes = BarcodeEncoding.Code128 | BarcodeEncoding.QRCode
};
var results = BarcodeReader.Read("scan.png", options);
// Optional: hint the reader for performance in format-known scenarios
var options = new BarcodeReaderOptions
{
ExpectBarcodeTypes = BarcodeEncoding.Code128 | BarcodeEncoding.QRCode
};
var results = BarcodeReader.Read("scan.png", options);
Imports System
' Optional: hint the reader for performance in format-known scenarios
Dim options As New BarcodeReaderOptions With {
.ExpectBarcodeTypes = BarcodeEncoding.Code128 Or BarcodeEncoding.QRCode
}
Dim results = BarcodeReader.Read("scan.png", options)
LEADTOOLS條碼遷移檢查清單
遷移前任務
在進行任何更改之前,審計程式碼庫中所有的LEADTOOLS使用情況:
grep -r "using Leadtools" --include="*.cs" .
grep -r "RasterSupport\|BarcodeEngine\|RasterCodecs" --include="*.cs" .
grep -r "BarcodeSymbology\|BarcodeData\|RasterImage" --include="*.cs" .
grep -r "LEADTOOLS.LIC\|SetLicense\|KernelExpired" --include="*.cs" .
grep -r "COPY LEADTOOLS.LIC\|LEADTOOLS_LICENSE_PATH" --include="*.dockerfile" --include="Dockerfile" .
grep -r "using Leadtools" --include="*.cs" .
grep -r "RasterSupport\|BarcodeEngine\|RasterCodecs" --include="*.cs" .
grep -r "BarcodeSymbology\|BarcodeData\|RasterImage" --include="*.cs" .
grep -r "LEADTOOLS.LIC\|SetLicense\|KernelExpired" --include="*.cs" .
grep -r "COPY LEADTOOLS.LIC\|LEADTOOLS_LICENSE_PATH" --include="*.dockerfile" --include="Dockerfile" .
在開始程式碼更改之前,記錄初始化塊、讀取調用站點、寫入調用站點和Dockerfile引用的數量和位置。
程式碼更新任務
- 移除所有五個LEADTOOLS NuGet套件(如果存在PDF編解碼器,也移除)
- 通過
dotnet add package IronBarcode - 用
using Leadtools.*命名空間導入 - 用
RasterSupport.SetLicense初始化區塊 - 移除所有
RasterSupport.KernelExpired檢查 - 移除所有
RasterSupport.IsLocked(RasterSupportType.*)檢查 - 移除所有
new BarcodeEngine()實例化 - 移除條碼程式碼路徑中所有
new RasterCodecs()實例化 - 用
codecs.Load(path)+engine.Reader.ReadBarcodes(image, ...) - 用
new BarcodeData(...)+new RasterImage(...)+FillCommand+WriteBarcode+codecs.Save() - 用
QRCode) - 從所有Dockerfile中移除
COPY LEADTOOLS.LIC行 - 從Dockerfile和部署配置中移除
LEADTOOLS_DEVELOPER_KEY環境變數聲明 - 向Dockerfile和秘密配置新增
IRONBARCODE_LICENSE環境變數 - 從Windows提供腳本中移除MSVC++ 2017運行時安裝
遷移後測試
- 驗證所有條碼讀取操作在先前由
BarcodeSymbology[]陣列覆蓋的所有符號學型別中返回預期值 - 驗證Code 128、QR碼和任何其他生成的條碼型別產生有效的、可掃描的輸出
- 確認PDF條碼提取在未使用
Leadtools.Pdf的情況下正確工作 - 在未掛載任何
.LIC文件的情況下構建並啟動容器映像,確認應用程式正確初始化 - 在沒有
.LIC文件配置步驟的情況下運行CI/CD管道,確認所有階段成功完成 - 驗證發布的輸出大小和容器映像提取時間得到改進
遷移到IronBarcode的主要優勢
簡化的部署架構:遷移後,每個環境——開發、CI、預備、生產——都通過相同的字串值秘密機制接收授權,用於API金鑰、資料庫密碼和其他憑證。 不需要文件配置、不需要工件處理,也不需要音量掛載。 新增一個新的部署環境的操作與新增任何其他秘密相同。
減少的依賴面積:一個NuGet包取代了五個,並且MSVC++ 2017運行時要求被消除。 條碼服務的發布輸出從大約148 MB降至大約39 MB。 較小的映像意味著更快的容器提取、降低的容器註冊表儲存成本以及在無伺服器和自動縮放環境中縮短冷啟動時間。
精簡的應用啟動:20行的LEADTOOLS初始化序列——文件路徑解析、到期驗證和三個每個功能鎖定檢查——被一行取代。應用啟動更快,初始化程式碼路徑沒有可能在新環境中失敗的文件系統依賴。
全面的格式檢測:IronBarcode支持跨所有50+符號學的讀取,而不需要調用者指定預期格式。 由於省略了LEADTOOLS中的符號學BarcodeSymbology[]陣列而導致的無聲漏讀風險被消除。 當在輸入文件中出現新的條碼型別時,IronBarcode會檢測到它,而不需要程式碼更改。
可預測的總成本:單一永久授權購買涵蓋開發、測試和無限生產部署,無需部署跟蹤,無需年度續訂,不需要單獨獲取生產許可。 團隊可以在開發開始前確認完整的程式庫成本,而無需銷售對話。
現代API設計:BarcodeWriter.CreateBarcode()是接受文件路徑的靜態入口。 LEADTOOLS的閱讀和寫入要求的多層物件構建——編解碼器實例、引擎實例、圖像分配、背景填充——由內部處理。 讀取和生成條碼的程式碼更短、更易於審核和測試。
常見問題
我應該為什麼從LEADTOOLS Barcode遷移到IronBarcode?
常見的理由包括簡化授權(去除SDK +運行時鑰匙的複雜性)、消除吞吐量限制、獲得原生PDF支持、改善Docker/CI/CD部署,並減少生產程式碼中的API樣板。
如何使用IronBarcode替換LEADTOOLS API調用?
用IronBarCode.License.LicenseKey = "key"替換實例建立和授權樣板。用BarcodeReader.Read(path)替換讀取呼叫,用BarcodeWriter.CreateBarcode(data, encoding)替換寫入呼叫。靜態方法不需要實例管理。
從LEADTOOLS Barcode遷移到IronBarcode時,程式碼會改變多少?
大多數遷移會導致較少的程式碼行。授權樣板、實例構造函式和顯式格式配置被移除。核心讀/寫操作映射為較短的IronBarcode等價物,且結果物件更清晰。
在遷移過程中,我需要同時安裝LEADTOOLS Barcode和IronBarcode嗎?
不需要。大多數遷移是直接替換而不是並行操作。一次遷移一個服務類,替換NuGet引用,並更新實例化和API呼叫模式後再移動到下一個類。
IronBarcode的NuGet包名稱是什麼?
包名是'IronBarCode'(大寫B和C)。可以用'Install-Package IronBarCode'或'dotnet add package IronBarCode'安裝。程式碼中的using指令是'using IronBarCode;'。
與LEADTOOLS Barcode相比,IronBarcode如何簡化Docker部署?
IronBarcode是沒有外部SDK文件或掛載許可配置的NuGet封裝。在Docker中,設置IRONBARCODE_LICENSE_KEY環境變數,包在啟動時處理許可驗證。
從LEADTOOLS遷移後,IronBarcode是否能自動檢測所有條碼格式?
是的。IronBarcode自動檢測所有支持的格式的符號學。不需要顯式的BarcodeTypes枚舉。如果格式已經知道並且性能很重要,BarcodeReaderOptions允許限制搜索空間作為優化。
IronBarcode是否可以不使用單獨的庫讀取PDF中的條碼?
可以。BarcodeReader.Read("document.pdf")原生處理PDF文件。結果包括每個條碼發現的頁碼、格式、值和置信度。不需要外部PDF渲染步驟。
IronBarcode如何處理並行條碼處理?
IronBarcode的靜態方法是無狀態且執行緒安全的。直接使用Parallel.ForEach遍歷文件列表,不需要每個執行緒的實例管理。BarcodeReaderOptions.MaxParallelThreads控制內部執行緒預算。
從LEADTOOLS Barcode遷移到IronBarcode時,哪些結果屬性會改變?
常見的重命名:BarcodeValue變為Value,BarcodeType變為Format。IronBarcode結果還新增了Confidence和PageNumber。整個解決方案的搜索和替換可處理現有結果處理程式碼中的重新命名。
我如何在CI/CD管道中設置IronBarcode授權?
將IRONBARCODE_LICENSE_KEY作為管道機密儲存,在應用程式啟動程式碼中分配IronBarCode.License.LicenseKey。一個機密涵蓋所有環境,包括開發、測試、分期和生產。
IronBarcode是否支持生成帶有自定義樣式的QR碼?
支持。QRCodeWriter.CreateQrCode()支持通過ChangeBarCodeColor()自定義顏色,通過AddBrandLogo()嵌入logo,可配置錯誤校正級別,以及包括PNG、JPG、PDF和流在內的多種輸出格式。

