跳至页脚内容
视频

How to Use Barcode Scanner in Blazor | IronBarcode

从Spire.Barcode迁移到IronBarcode

Spire.Barcode 的 BarcodeScanner.Scan() 在每次调用时需要一个 BarCodeType 参数。 随着代码库不断增长,需要处理各种文档来源、新的供应商标签和新的条形码格式,这种设计会产生类型猜测循环,随着时间的推移,维护成本会不断增加。本指南涵盖了从 Spire.Barcode 到IronBarcode 的完整路径:团队进行此迁移的原因、执行迁移的具体步骤,以及每个常见操作的迁移前后代码模式。

为什么要从 Spire.Barcode 迁移?

格式规范开销: Spire.Barcode 中的每次调用 BarcodeScanner.Scan() 都需要一个调用代码必须提供的 BarCodeType 枚举值。 处理来自未知或混合格式来源的条形码的应用程序必须维护一个可能类型的候选列表,并遍历该列表,直到找到匹配项。 添加到工作流程中的每种格式都需要更新候选数组、重新测试检测逻辑,并确认迭代顺序不会导致一种格式掩盖另一种格式。 这种额外开销会随着格式多样性的增加而成比例增长。

免费层评估限制:与商业产品相比,FreeSpire.Barcode 有意降低了读取性能并限制了可用的符号集。 在 FreeSpire.Barcode 评估期间进行的基准测试和格式覆盖率测试并不代表商业版 Spire.Barcode 的行为。 如果团队基于免费试用版进行购买,则购买后可能会发现该产品在生产环境中的表现有所不同——尤其是在吞吐量和支持的符号体系方面。

PDF 需要两个库: Spire.Barcode 无法直接读取 PDF 文件中的条形码。 基于PDF的工作流程需要有自己许可的独立Spire.PDF 包,加上开发人员编写的代码来迭代页面,从每个页面提取图像,然后将这些图像传递给条形码扫描仪。 从概念上讲,条形码读取属于一项任务,但它却被拆分到两个产品、两份许可协议和大量的基础设施代码中。

返回类型复杂性: BarcodeScanner.Scan() 返回 string[],其中仅包含解码值。 返回结果中未包含检测到的条形码格式。 需要按格式路由结果的应用程序(例如,将二维码值发送到一个处理器,将 Code128 值发送到另一个处理器)必须维护一个单独的类型变量,该变量派生自产生结果的任何迭代步骤,从而在扫描本身之外创建额外的状态管理。

基本问题

类型猜测循环是强制性BarCodeType约束的最明显症状。 在 Spire.Barcode 代码库处理来自多个来源的文档时,这种模式会反复出现:

// Spire.Barcode: growing candidate loop with silent misses
BarcodeScanner scanner = new BarcodeScanner();
var candidates = new[] { BarCodeType.Code128, BarCodeType.QRCode, BarCodeType.DataMatrix, BarCodeType.EAN13, BarCodeType.PDF417 };

string foundValue = null;
foreach (var type in candidates)
{
    string[] found = scanner.Scan("barcode.png", type);
    if (found.Length > 0)
    {
        foundValue = found[0];
        break;
    }
}
// Spire.Barcode: growing candidate loop with silent misses
BarcodeScanner scanner = new BarcodeScanner();
var candidates = new[] { BarCodeType.Code128, BarCodeType.QRCode, BarCodeType.DataMatrix, BarCodeType.EAN13, BarCodeType.PDF417 };

string foundValue = null;
foreach (var type in candidates)
{
    string[] found = scanner.Scan("barcode.png", type);
    if (found.Length > 0)
    {
        foundValue = found[0];
        break;
    }
}
Imports Spire.Barcode

Dim scanner As New BarcodeScanner()
Dim candidates = {BarCodeType.Code128, BarCodeType.QRCode, BarCodeType.DataMatrix, BarCodeType.EAN13, BarCodeType.PDF417}

Dim foundValue As String = Nothing
For Each type In candidates
    Dim found As String() = scanner.Scan("barcode.png", type)
    If found.Length > 0 Then
        foundValue = found(0)
        Exit For
    End If
Next
$vbLabelText   $csharpLabel

IronBarcode完全消除了循环。 格式检测功能已内置于库中:

// IronBarcode: single call, all formats, type included in result
var results = BarcodeReader.Read("barcode.png");

foreach (var result in results)
{
    Console.WriteLine($"{result.Format}: {result.Value}");
}
// IronBarcode: single call, all formats, type included in result
var results = BarcodeReader.Read("barcode.png");

foreach (var result in results)
{
    Console.WriteLine($"{result.Format}: {result.Value}");
}
Imports IronBarcode

' IronBarcode: single call, all formats, type included in result
Dim results = BarcodeReader.Read("barcode.png")

For Each result In results
    Console.WriteLine($"{result.Format}: {result.Value}")
Next
$vbLabelText   $csharpLabel

IronBarcode与 Spire.Barcode:功能对比

特征 Spire.Barcode IronBarcode
自动格式检测
BarCodeType 参数是必需的
结果中的格式元数据
原生 PDF 阅读 否(需要 Spire.PDF)
PDF 附加库 Spire.PDF(单独授权) None
免费层级阅读性能 故意降低 全速
免费水印 大号,覆盖条形码 小,仅边缘
免费等级符号 约20种 50多种类型
免费注册
生成 API 模型 设置对象 + 生成器 具有流畅链的静态工厂
带有自定义徽标的二维码 仅限商业层级 所有级别
从读取返回类型 string[] BarcodeResults(带有类型元数据)
符号计数 39+(商业) 50岁以上
许可模式 按席位永久使用权 + 订阅 永久有效,可续期。

快速入门

步骤 1:替换 NuGet 软件包

移除当前正在使用的 Spire 软件包:

# Remove whichever variant is installed
dotnet remove package FreeSpire.Barcode
# or
dotnet remove package Spire.Barcode

# If Spire.PDF was installed only for barcode extraction, remove it as well
dotnet remove package Spire.PDF
# Remove whichever variant is installed
dotnet remove package FreeSpire.Barcode
# or
dotnet remove package Spire.Barcode

# If Spire.PDF was installed only for barcode extraction, remove it as well
dotnet remove package Spire.PDF
SHELL

如果Spire.PDF 用于除条码提取之外的其他文档操作,请保留它并仅删除条码相关的代码路径。

安装IronBarcode:

dotnet add package IronBarcode
dotnet add package IronBarcode
SHELL

步骤 2:更新命名空间

用IronBarcode命名空间替换所有Spire命名空间导入:

// Remove:
using Spire.Barcode;
using Spire.Pdf;

// Add:
using IronBarCode;
// Remove:
using Spire.Barcode;
using Spire.Pdf;

// Add:
using IronBarCode;
Imports IronBarCode
$vbLabelText   $csharpLabel

步骤 3:初始化许可证

在应用程序启动时,用单个IronBarcode属性赋值替换 Spire 许可证初始化。有关密钥格式和部署选项,请参阅IronBarcode许可页面

// Remove (FreeSpire registration):
Spire.Barcode.BarcodeSettings.ApplyKey("your-free-key");

// Remove (commercial Spire license):
Spire.License.LicenseProvider.SetLicenseKey("your-commercial-key");

// Add (IronBarcode):
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";
// Remove (FreeSpire registration):
Spire.Barcode.BarcodeSettings.ApplyKey("your-free-key");

// Remove (commercial Spire license):
Spire.License.LicenseProvider.SetLicenseKey("your-commercial-key");

// Add (IronBarcode):
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";
' Remove (FreeSpire registration):
Spire.Barcode.BarcodeSettings.ApplyKey("your-free-key")

' Remove (commercial Spire license):
Spire.License.LicenseProvider.SetLicenseKey("your-commercial-key")

' Add (IronBarcode):
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY"
$vbLabelText   $csharpLabel

对于 Docker 和 CI 环境,可以通过环境变量提供许可证密钥:

IronBarCode.License.LicenseKey = Environment.GetEnvironmentVariable("IRONBARCODE_LICENSE");
IronBarCode.License.LicenseKey = Environment.GetEnvironmentVariable("IRONBARCODE_LICENSE");
Imports System

IronBarCode.License.LicenseKey = Environment.GetEnvironmentVariable("IRONBARCODE_LICENSE")
$vbLabelText   $csharpLabel

代码迁移示例

读取已知格式的条形码

Spire.Barcode 中的单格式读取显式传递类型并接收 string[]

Spire.条形码方法:

using Spire.Barcode;

BarcodeScanner scanner = new BarcodeScanner();
string[] results = scanner.Scan("shipping-label.png", BarCodeType.Code128);

if (results.Length > 0)
{
    ProcessShipment(results[0]);
}
using Spire.Barcode;

BarcodeScanner scanner = new BarcodeScanner();
string[] results = scanner.Scan("shipping-label.png", BarCodeType.Code128);

if (results.Length > 0)
{
    ProcessShipment(results[0]);
}
Imports Spire.Barcode

Dim scanner As New BarcodeScanner()
Dim results As String() = scanner.Scan("shipping-label.png", BarCodeType.Code128)

If results.Length > 0 Then
    ProcessShipment(results(0))
End If
$vbLabelText   $csharpLabel

IronBarcode方法:

using IronBarCode;

var results = BarcodeReader.Read("shipping-label.png");

if (results.Any())
{
    ProcessShipment(results.First().Value);
}
using IronBarCode;

var results = BarcodeReader.Read("shipping-label.png");

if (results.Any())
{
    ProcessShipment(results.First().Value);
}
Imports IronBarCode

Dim results = BarcodeReader.Read("shipping-label.png")

If results.Any() Then
    ProcessShipment(results.First().Value)
End If
$vbLabelText   $csharpLabel

去除 BarCodeType 参数。 如果下游逻辑需要确认检测到的格式,results.First().Format 提供它而无需单独的状态变量。

折叠混合格式类型循环

当 Spire.Barcode 代码库处理可能包含多种格式的文档时,随着新格式进入工作流程,类型猜测循环会随着时间的推移而累积。

Spire.条形码方法:

using Spire.Barcode;

BarcodeScanner scanner = new BarcodeScanner();
BarCodeType[] candidates = new[]
{
    BarCodeType.Code128,
    BarCodeType.QRCode,
    BarCodeType.DataMatrix,
    BarCodeType.EAN13,
    BarCodeType.PDF417
};

List<string> allValues = new List<string>();
foreach (BarCodeType type in candidates)
{
    string[] found = scanner.Scan("document-scan.png", type);
    allValues.AddRange(found);
}
using Spire.Barcode;

BarcodeScanner scanner = new BarcodeScanner();
BarCodeType[] candidates = new[]
{
    BarCodeType.Code128,
    BarCodeType.QRCode,
    BarCodeType.DataMatrix,
    BarCodeType.EAN13,
    BarCodeType.PDF417
};

List<string> allValues = new List<string>();
foreach (BarCodeType type in candidates)
{
    string[] found = scanner.Scan("document-scan.png", type);
    allValues.AddRange(found);
}
Imports Spire.Barcode

Dim scanner As New BarcodeScanner()
Dim candidates As BarCodeType() = {
    BarCodeType.Code128,
    BarCodeType.QRCode,
    BarCodeType.DataMatrix,
    BarCodeType.EAN13,
    BarCodeType.PDF417
}

Dim allValues As New List(Of String)()
For Each type As BarCodeType In candidates
    Dim found As String() = scanner.Scan("document-scan.png", type)
    allValues.AddRange(found)
Next
$vbLabelText   $csharpLabel

IronBarcode方法:

using IronBarCode;

// The entire loop is replaced by a single call
var results = BarcodeReader.Read("document-scan.png");
List<string> allValues = results.Select(r => r.Value).ToList();
using IronBarCode;

// The entire loop is replaced by a single call
var results = BarcodeReader.Read("document-scan.png");
List<string> allValues = results.Select(r => r.Value).ToList();
Imports IronBarCode

' The entire loop is replaced by a single call
Dim results = BarcodeReader.Read("document-scan.png")
Dim allValues As List(Of String) = results.Select(Function(r) r.Value).ToList()
$vbLabelText   $csharpLabel

IronBarcode 的图像读取功能一次即可处理所有 50 多种条码。 当需要为高容量批处理调整读取速度时,BarcodeReaderOptions 提供了无需添加格式特定分支的控制:

var options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = true
};
var results = BarcodeReader.Read("document-scan.png", options);
var options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = true
};
var results = BarcodeReader.Read("document-scan.png", options);
Dim options As New BarcodeReaderOptions With {
    .Speed = ReadingSpeed.Balanced,
    .ExpectMultipleBarcodes = True
}
Dim results = BarcodeReader.Read("document-scan.png", options)
$vbLabelText   $csharpLabel

有关选择合适的ReadingSpeed 值的指导,请参阅 读取速度选项文档

生成条形码

生成 Spire.Barcode 需要实例化一个 BarcodeSettings 对象,逐个分配每个属性,将设置传递给BarCodeGenerator,然后在任何文件写入之前调用 GenerateImage()

Spire.条形码方法:

using Spire.Barcode;

BarcodeSettings settings = new BarcodeSettings();
settings.Type = BarCodeType.Code128;
settings.Data = "SHIP-9842-XZ";
settings.ShowText = true;
settings.TextMargin = 5;
settings.BarHeight = 60;
settings.Unit = GraphicsUnit.Pixel;

BarCodeGenerator generator = new BarCodeGenerator(settings);
Image barcodeImage = generator.GenerateImage();
barcodeImage.Save("shipping-label.png", ImageFormat.Png);
using Spire.Barcode;

BarcodeSettings settings = new BarcodeSettings();
settings.Type = BarCodeType.Code128;
settings.Data = "SHIP-9842-XZ";
settings.ShowText = true;
settings.TextMargin = 5;
settings.BarHeight = 60;
settings.Unit = GraphicsUnit.Pixel;

BarCodeGenerator generator = new BarCodeGenerator(settings);
Image barcodeImage = generator.GenerateImage();
barcodeImage.Save("shipping-label.png", ImageFormat.Png);
Imports Spire.Barcode
Imports System.Drawing
Imports System.Drawing.Imaging

Dim settings As New BarcodeSettings()
settings.Type = BarCodeType.Code128
settings.Data = "SHIP-9842-XZ"
settings.ShowText = True
settings.TextMargin = 5
settings.BarHeight = 60
settings.Unit = GraphicsUnit.Pixel

Dim generator As New BarCodeGenerator(settings)
Dim barcodeImage As Image = generator.GenerateImage()
barcodeImage.Save("shipping-label.png", ImageFormat.Png)
$vbLabelText   $csharpLabel

IronBarcode方法:

using IronBarCode;

BarcodeWriter.CreateBarcode("SHIP-9842-XZ", BarcodeEncoding.Code128)
    .ResizeTo(400, 100)
    .SaveAsPng("shipping-label.png");
using IronBarCode;

BarcodeWriter.CreateBarcode("SHIP-9842-XZ", BarcodeEncoding.Code128)
    .ResizeTo(400, 100)
    .SaveAsPng("shipping-label.png");
Imports IronBarCode

BarcodeWriter.CreateBarcode("SHIP-9842-XZ", BarcodeEncoding.Code128) _
    .ResizeTo(400, 100) _
    .SaveAsPng("shipping-label.png")
$vbLabelText   $csharpLabel

可变的 BarcodeSettings 对象和单独的 BarCodeGenerator 实例被用单个静态工厂调用和一个流畅链代替。 没有共享设置在并发调用中意外被共享的风险,因为每个 CreateBarcode() 调用都是独立的。

从 PDF 文档中读取条形码

Spire.Barcode 无法直接读取 PDF 文件。 基于 Spire 的方法需要一个独立的 Spire.PDF 包以打开文件,迭代其页面,从每个页面提取光栅图像,然后将这些图像逐个传递给条码扫描仪。

Spire.条形码方法:

using Spire.Pdf;
using Spire.Barcode;

var pdf = new PdfDocument();
pdf.LoadFromFile("invoices.pdf");

var scanner = new BarcodeScanner();
var extractedValues = new List<string>();

foreach (PdfPageBase page in pdf.Pages)
{
    Image[] images = page.ExtractImages();
    foreach (Image image in images)
    {
        string[] found = scanner.Scan(image, BarCodeType.QRCode);
        extractedValues.AddRange(found);
    }
}
using Spire.Pdf;
using Spire.Barcode;

var pdf = new PdfDocument();
pdf.LoadFromFile("invoices.pdf");

var scanner = new BarcodeScanner();
var extractedValues = new List<string>();

foreach (PdfPageBase page in pdf.Pages)
{
    Image[] images = page.ExtractImages();
    foreach (Image image in images)
    {
        string[] found = scanner.Scan(image, BarCodeType.QRCode);
        extractedValues.AddRange(found);
    }
}
Imports Spire.Pdf
Imports Spire.Barcode

Dim pdf As New PdfDocument()
pdf.LoadFromFile("invoices.pdf")

Dim scanner As New BarcodeScanner()
Dim extractedValues As New List(Of String)()

For Each page As PdfPageBase In pdf.Pages
    Dim images() As Image = page.ExtractImages()
    For Each image As Image In images
        Dim found() As String = scanner.Scan(image, BarCodeType.QRCode)
        extractedValues.AddRange(found)
    Next
Next
$vbLabelText   $csharpLabel

IronBarcode方法:

using IronBarCode;

var results = BarcodeReader.Read("invoices.pdf");

foreach (var barcode in results)
{
    Console.WriteLine($"Page {barcode.PageNumber}: {barcode.Format} = {barcode.Value}");
}
using IronBarCode;

var results = BarcodeReader.Read("invoices.pdf");

foreach (var barcode in results)
{
    Console.WriteLine($"Page {barcode.PageNumber}: {barcode.Format} = {barcode.Value}");
}
Imports IronBarCode

Dim results = BarcodeReader.Read("invoices.pdf")

For Each barcode In results
    Console.WriteLine($"Page {barcode.PageNumber}: {barcode.Format} = {barcode.Value}")
Next
$vbLabelText   $csharpLabel

IronBarcode 直接读取 PDF 文档,无需Spire.PDF 或任何手动页面管理。 结果对象中提供了提取每个条形码的页码。 一旦替换了 PDF 阅读代码,即可从项目中删除 Spire.PDF 包引用和所有 using Spire.Pdf; 导入。

Spire.Barcode API 到IronBarcode映射参考

Spire.Barcode IronBarcode 备注
new BarcodeScanner() BarcodeReader.Read()(静态) 无需扫描仪实例
scanner.Scan(path, BarCodeType.X) BarcodeReader.Read(path) 类型参数已移除
BarCodeType.Code128(必需) 自动检测 无需等效物
BarCodeType.QRCode(必需) 自动检测 无需等效物
string[] results 来自 Scan() BarcodeResults 集合 通过 .Value 访问值
results[0](字符串) results[0].Value
结果格式未知 result.Format 结果对象中包含的类型
new BarcodeSettings() CreateBarcode() 参数 无需设置对象
settings.Type = BarCodeType.X BarcodeEncoding.X 作为第二个参数
settings.Data = "value" CreateBarcode() 的第一个参数
new BarCodeGenerator(settings) 不需要 静态工厂取代了它
generator.GenerateImage() .SaveAsPng() / .SaveAsJpeg() 特定格式的输出方法
BarcodeSettings.ApplyKey("key") IronBarCode.License.LicenseKey = "key" 启动时设置一次
Spire.License.LicenseProvider.SetLicenseKey("key") IronBarCode.License.LicenseKey = "key" 相同的单线替换
Spire.PDF(用于 PDF 阅读) 不需要 内置原生 PDF 支持

常见迁移问题和解决方案

问题 1:移除 BarCodeType 参数

Spire.Barcode: scanner.Scan(path, BarCodeType.Code128) — 方法签名需要类型参数。 删除它会导致编译错误。

解决方案:BarcodeReader.Read(path) 完全替换调用。 在代码库中搜索 BarCodeType. 以找到每个出现的位置。 类型猜测循环(遍历 BarCodeType[] 候选数组的 foreach 块)可全部删除; 单个 BarcodeReader.Read() 调用替代循环。

问题 2:string[] 返回类型

Spire.Barcode: Scan() 返回 string[]。 将其分配给 string[] 变量或传递给期望 string[] 的方法的下游代码迁移后无法编译。

解决方案: 更新调用站点以使用 BarcodeResults。 要获取简单的字符串数组,请使用 .Select(r => r.Value).ToArray()。 要访问第一个结果,请使用 .First()?.Value。 如果下游需要检测到的格式,结果对象上可通过 .Format 获得 - 无需单独的状态变量。

// If a plain string array is needed:
string[] texts = BarcodeReader.Read("barcode.png")
    .Select(r => r.Value)
    .ToArray();

// If only the first value is needed:
string first = BarcodeReader.Read("barcode.png").First()?.Value;
// If a plain string array is needed:
string[] texts = BarcodeReader.Read("barcode.png")
    .Select(r => r.Value)
    .ToArray();

// If only the first value is needed:
string first = BarcodeReader.Read("barcode.png").First()?.Value;
Imports System.Linq

' If a plain string array is needed:
Dim texts As String() = BarcodeReader.Read("barcode.png") _
    .Select(Function(r) r.Value) _
    .ToArray()

' If only the first value is needed:
Dim first As String = BarcodeReader.Read("barcode.png").FirstOrDefault()?.Value
$vbLabelText   $csharpLabel

问题 3:Spire.PDF 包清理

Spire.Barcode: PDF 阅读工作流程依赖 Spire.PDF 进行页面访问和图像提取。 该项目引用了包,并包含 using Spire.Pdf; 导入和 PdfDocument / PdfPageBase 在多个文件中使用。

解决方案: 在用 BarcodeReader.Read("file.pdf") 替换所有 PDF 条码提取代码之后,运行 grep 审核以确认没有剩余的 Spire.Pdf 引用存在,然后再删除包:

grep -r "using Spire.Pdf" --include="*.cs" .
grep -r "Spire\.Pdf\|PdfPageBase\|ExtractImages" --include="*.cs" .
grep -r "using Spire.Pdf" --include="*.cs" .
grep -r "Spire\.Pdf\|PdfPageBase\|ExtractImages" --include="*.cs" .
SHELL

仅当所有引用都被清除后才移除该软件包:

dotnet remove package Spire.PDF
dotnet remove package Spire.PDF
SHELL

问题 4:命名空间变更

Spire.Barcode: Spire.Barcode 命名空间包含 BarCodeGeneratorBarCodeTypeIronBarCode 命名空间包含 BarcodeEncodingBarcodeReaderOptions

解决方案: 全局用 using IronBarCode; 替换 using Spire.Barcode;。 生成中使用的 BarCodeType 枚举被 BarcodeEncoding 代替。 读取中使用的 BarCodeType 枚举没有IronBarcode等效项 - 简单地删除,因为格式检测是自动的。

Spire.条形码迁移清单

迁移前任务

在进行任何更改之前,请审核代码库,找出所有 Spire.Barcode 的使用情况:

grep -r "using Spire.Barcode" --include="*.cs" .
grep -r "using Spire.Pdf" --include="*.cs" .
grep -r "BarcodeScanner\|BarcodeSettings\|BarCodeGenerator\|BarCodeType\." --include="*.cs" .
grep -r "ApplyKey\|SetLicenseKey" --include="*.cs" .
grep -r "ExtractImages\|PdfPageBase" --include="*.cs" .
grep -r "using Spire.Barcode" --include="*.cs" .
grep -r "using Spire.Pdf" --include="*.cs" .
grep -r "BarcodeScanner\|BarcodeSettings\|BarCodeGenerator\|BarCodeType\." --include="*.cs" .
grep -r "ApplyKey\|SetLicenseKey" --include="*.cs" .
grep -r "ExtractImages\|PdfPageBase" --include="*.cs" .
SHELL
  • 记录所有包含 BarCodeType 候选数组的文件(要删除的类型猜测循环)
  • 找出任何接收 Scan() 结果的 string[] 变量并需要类型更新
  • 确认 Spire.PDF 是否用于条码提取以外的操作

代码更新任务

  1. 删除 FreeSpire.BarcodeSpire.Barcode NuGet 包
  2. 如果条码提取是它的唯一用途,删除 Spire.PDF NuGet 包
  3. 安装 IronBarcode NuGet 包
  4. using IronBarCode; 替换所有 using Spire.Barcode;
  5. 删除不再需要的 using Spire.Pdf; 导入
  6. 替换许可证初始化:ApplyKey()SetLicenseKey()IronBarCode.License.LicenseKey = "key"
  7. 删除所有 new BarcodeScanner() 实例化
  8. BarcodeReader.Read(path) 替换所有 scanner.Scan(path, BarCodeType.X) 调用
  9. 删除类型猜测的 foreach 循环; 用单个 BarcodeReader.Read() 调用替换每个调用
  10. 删除 PDF 页面迭代和图像提取模块; 用 BarcodeReader.Read("file.pdf") 替换
  11. 更新 string[] 结果分配以使用 .Value.Select(r => r.Value).ToArray()
  12. BarcodeWriter.CreateBarcode(data, encoding) 链替换 new BarcodeSettings()
  13. 删除 new BarCodeGenerator(settings) 实例化
  14. .SaveAsPng().SaveAsJpeg() 替换 .GenerateImage() + image.Save()
  15. BarcodeEncoding.X 替换 BarCodeType.X 生成中的枚举值

迁移后测试

  • 确认先前通过候选循环扫描的所有格式被 BarcodeReader.Read() 正确检测
  • 测量批量处理吞吐量并确认其满足生产要求
  • 验证 PDF 条形码提取结果是否正确,以及页码是否正确
  • 使用物理或软件扫描器确认生成的条形码输出质量和可扫描性
  • 检查许可证密钥是否已正确加载到所有部署环境(本地、测试环境、生产环境、CI/CD)中。
  • 运行迁移前任务中的 grep 审计命令,以确认不再存在 Spire 引用。

迁移到IronBarcode的主要优势

消除格式维护开销:迁移后,无需维护候选类型列表,也无需在新条形码格式进入工作流程时更新类型猜测循环。 BarcodeReader.Read() 自动处理完整的符号集,并在每个结果对象中包含检测到的格式以进行下游路由。

承诺前进行准确评估: IronBarcode试用版与授权产品使用相同的二进制文件,以全速读取,并包含完整的符号集。 试用评估期间进行的性能基准测试和格式覆盖率测试将直接转移到生产环境中。 团队可以充满信心地做出购买决定,因为评估的行为就是部署的行为。

单包 PDF 支持: 从 PDF 文件中提取条码由用于图像的同一个 BarcodeReader.Read() 调用处理。 无需辅助库、无需额外许可证、也无需手动编写页面迭代代码。 之前维护 Spire.BarcodeSpire.PDF 的团队减少为整个条码工作流程的单一依赖。

带格式元数据的结果对象: BarcodeResults 集合中的每个条目都携带解码值、检测到的符号、PDF 输入的源页码以及源图像中的边界区域。 需要按格式进行路由、过滤或日志记录的应用程序,无需额外的状态管理,即可在结果中获取该信息。

简化的生成 API: BarcodeWriter.CreateBarcode() 的静态工厂模式消除了可变的 BarcodeSettings 对象和独立的 BarCodeGenerator 实例。 每次生成调用都是独立的,降低了并发操作之间配置泄露的风险。

向前兼容性: IronBarcode会定期更新,与.NET运行时版本保持一致。 随着.NET 10 的普及,到 2026 年,兼容性更新可确保迁移的代码库能够继续构建和运行,而无需在每个新的.NET版本上更改库。

请注意Spire 是其相应所有者的注册商标。 本网站与 e-iceblue 无关、未获得其认可或赞助。 所有产品名称、徽标和品牌均为各自所有者的财产。 比较仅供参考,反映撰写时公开可用的信息。

常见问题解答

我为什么要从 Spire.Barcode 迁移到 IronBarcode?

常见原因包括简化许可(消除 SDK + 运行时密钥的复杂性)、消除吞吐量限制、获得原生 PDF 支持、改进 Docker/CI/CD 部署以及减少生产代码中的 API 样板代码。

如何用 IronBarcode 替换 Spire.Barcode API 调用?

用 `IronBarCode.License.LicenseKey = "key"` 替换实例创建和许可相关的样板代码。用 `BarcodeReader.Read(path)` 替换读取器调用,用 `BarcodeWriter.CreateBarcode(data, encoding)` 替换写入器调用。静态方法无需实例管理。

从 Spire.Barcode 迁移到 IronBarcode 时,代码需要做多少更改?

大多数迁移都能减少代码行数。许可样板代码、实例构造函数和显式格式配置都被移除。核心读/写操作映射到更简洁的 IronBarcode 等效代码,并生成更清晰的结果对象。

迁移过程中是否需要同时安装 Spire.Barcode 和 IronBarcode?

不。大多数迁移都是直接替换,而不是并行操作。一次迁移一个服务类,替换 NuGet 引用,并更新实例化和 API 调用模式,然后再迁移下一个类。

IronBarcode 的 NuGet 包名称是什么?

该软件包名为“IronBarCode”(B 和 C 大写)。使用“Install-Package IronBarCode”或“dotnet add package IronBarCode”进行安装。代码中的 using 指令为“using IronBarCode;”。

与 Spire.Barcode 相比,IronBarcode 如何简化 Docker 部署?

IronBarcode 是一个 NuGet 包,不包含任何外部 SDK 文件或已挂载的许可证配置。在 Docker 环境中,设置 IRONBARCODE_LICENSE_KEY 环境变量后,该包会在启动时自动处理许可证验证。

从 Spire.Barcode 迁移后,IronBarcode 是否能自动检测所有条形码格式?

是的。IronBarcode 可以自动检测所有支持格式的条码符号,无需显式枚举 BarcodeTypes。如果已知条码格式且性能至关重要,BarcodeReaderOptions 允许缩小搜索范围以进行优化。

IronBarcode 能否在不使用单独库的情况下读取 PDF 中的条形码?

是的。`BarcodeReader.Read("document.pdf")` 可以直接处理 PDF 文件。结果包括每个条形码的页码、格式、值和置信度。无需外部 PDF 渲染步骤。

IronBarcode如何处理并行条码处理?

IronBarcode 的静态方法是无状态且线程安全的。可以直接对文件列表使用 Parallel.ForEach,无需进行线程级实例管理。BarcodeReaderOptions.MaxParallelThreads 控制内部线程预算。

从 Spire.Barcode 迁移到 IronBarcode 时,结果属性会发生哪些变化?

常见重命名:BarcodeValue 变为 Value,BarcodeType 变为 Format。IronBarcode 结果还会添加 Confidence 和 PageNumber。解决方案范围内的查找替换功能会处理现有结果处理代码中的重命名。

如何在 CI/CD 流水线中设置 IronBarcode 许可?

将 IRONBARCODE_LICENSE_KEY 存储为管道密钥,并在应用程序启动代码中赋值 IronBarCode.License.LicenseKey。一个密钥即可覆盖所有环境,包括开发、测试、预发布和生产环境。

IronBarcode是否支持生成自定义样式的二维码?

是的。QRCodeWriter.CreateQrCode() 支持通过 ChangeBarCodeColor() 自定义颜色、通过 AddBrandLogo() 嵌入徽标、可配置纠错级别以及多种输出格式,包括 PNG、JPG、PDF 和流媒体。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

钢铁支援团队

我们每周 5 天,每天 24 小时在线。
聊天
电子邮件
打电话给我