IRONSOFTWAREHOME
影片

從Cloudmersive Barcode API遷移到IronBarcode

Curtis Chau
Curtis Chau
Updated: 2026年5月19日

這是在.NET條碼歷史上最簡單的遷移。 移除HTTP客戶端。 新增一個NuGet包。 刪除API金鑰管理。 其餘的是查找和替換。

Cloudmersive的.NET SDK是一個生成的REST API客戶端——在HttpClient調用周圍的薄包裝。 您應用程式中每一段特定於Cloudmersive的程式碼都是該客戶端的配置、通過該客戶端的調用,或者是網路請求失敗時的錯誤處理。 IronBarcode不需要任何這樣的基礎設施,因為它在本地處理條碼,不依賴網路。


快速開始:三步驟

步驟1:交換套件

dotnet remove package Cloudmersive.APIClient.NET.Barcode
dotnet add package BarCode
SHELL

步驟2:替換命名空間

// Remove these
using Cloudmersive.APIClient.NET.Barcode.Api;
using Cloudmersive.APIClient.NET.Barcode.Client;

// Add this
using IronBarCode;

步驟3:用授權金鑰替換API金鑰設置

// Remove this
Configuration.Default.ApiKey.Add("Apikey", "YOUR-CLOUDMERSIVE-API-KEY");

// Replace with this — set once at application startup
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";

在ASP.NET Core應用程式中,許可金鑰放在builder.Build()之前:

using IronBarCode;

IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";

var builder = WebApplication.CreateBuilder(args);
// ... rest of startup

成本:您之前的花費與您將要花費的

在遷移之前,值得計算Cloudmersive整合實際的成本。 計算非常簡單:

您的日消耗量每月請求數Cloudmersive級別IronBarcode(一次性)
每天100次約3,000次基本套餐$19.99/月$999
每天1,000次約30,000次商業高端$99.99/月$999
每天5,000次約150,000次中型企業$499.99/月$999
每天10,000次約300,000次企業(自定義)$999
每天25,000次約750,000次企業(自定義)$1,499(加,3開發者)

如果您為Cloudmersive支付超過約$63/月,IronBarcode的Lite授權在第一年內就能回本。 達到平衡點後,IronBarcode是一種永久授權,無需續費且無每次請求費用。


程式碼遷移範例

QR 碼生成

最常見的Cloudmersive生成調用可以直接映射到IronBarcode:

之前(Cloudmersive):

using Cloudmersive.APIClient.NET.Barcode.Api;
using Cloudmersive.APIClient.NET.Barcode.Client;

Configuration.Default.ApiKey.Add("Apikey", "YOUR-CLOUDMERSIVE-API-KEY");

var apiInstance = new GenerateBarcodeApi();

byte[] result = apiInstance.GenerateBarcodeQRCode("https://example.com");
File.WriteAllBytes("qr.png", result);

之後(IronBarcode):

using IronBarCode;

IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";

byte[] result = QRCodeWriter.CreateQrCode("https://example.com", 500)
    .ToPngBinaryData();
File.WriteAllBytes("qr.png", result);

// Or save directly
QRCodeWriter.CreateQrCode("https://example.com", 500)
    .SaveAsPng("qr.png");

Code128生成

之前(Cloudmersive):

using Cloudmersive.APIClient.NET.Barcode.Api;
using Cloudmersive.APIClient.NET.Barcode.Client;

Configuration.Default.ApiKey.Add("Apikey", "YOUR-CLOUDMERSIVE-API-KEY");
var apiInstance = new GenerateBarcodeApi();

byte[] result = apiInstance.GenerateBarcodeCode128("SHIP-2024031500428");
File.WriteAllBytes("barcode.png", result);

之後(IronBarcode):

using IronBarCode;

IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";

BarcodeWriter.CreateBarcode("SHIP-2024031500428", BarcodeEncoding.Code128)
    .SaveAsPng("barcode.png");

// Or get bytes directly
byte[] result = BarcodeWriter.CreateBarcode("SHIP-2024031500428", BarcodeEncoding.Code128)
    .ToPngBinaryData();

條碼閱讀

之前(Cloudmersive):

using Cloudmersive.APIClient.NET.Barcode.Api;
using Cloudmersive.APIClient.NET.Barcode.Client;

Configuration.Default.ApiKey.Add("Apikey", "YOUR-CLOUDMERSIVE-API-KEY");
var scanApi = new BarcodeScanApi();

using (var stream = File.OpenRead("barcode.png"))
{
    var result = scanApi.BarcodeScanImage(stream);
    if (result.Successful == true)
    {
        Console.WriteLine($"Value: {result.RawText}");
        Console.WriteLine($"Type: {result.BarcodeType}");
    }
}

之後(IronBarcode):

using IronBarCode;

IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";

var results = BarcodeReader.Read("barcode.png");
var result = results.First();
Console.WriteLine($"Value: {result.Value}");
Console.WriteLine($"Type: {result.Format}");

非同步條碼讀取

如果您的Cloudmersive整合使用非同步API:

之前(Cloudmersive):

using Cloudmersive.APIClient.NET.Barcode.Api;
using Cloudmersive.APIClient.NET.Barcode.Client;

Configuration.Default.ApiKey.Add("Apikey", "YOUR-CLOUDMERSIVE-API-KEY");
var scanApi = new BarcodeScanApi();

using (var stream = File.OpenRead("barcode.png"))
{
    var result = await scanApi.BarcodeScanImageAsync(stream);
    return result.Successful == true ? result.RawText : null;
}

之後(IronBarcode):

using IronBarCode;

IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";

// IronBarcode is synchronous; wrap in Task.Run to free the calling thread for CPU-bound work
var results = await Task.Run(() => BarcodeReader.Read("barcode.png"));
return results.FirstOrDefault()?.Value;

從PDF中讀取條碼

Cloudmersive沒有原生PDF支持。 如果您現有的程式碼在掃描之前從PDF中提取圖像,請將整個流程替換為單個IronBarcode調用:

之前(Cloudmersive——需要單獨PDF提取):

// Requires a separate PDF library to extract pages as images,
// then one Cloudmersive API call per extracted page image.
var scanApi = new BarcodeScanApi();
foreach (var pageImagePath in ExtractPdfPages("document.pdf"))
{
    using var stream = File.OpenRead(pageImagePath);
    var result = await scanApi.BarcodeScanImageAsync(stream);
    if (result.Successful == true)
    {
        Console.WriteLine($"Found: {result.RawText}");
    }
}

之後(IronBarcode):

using IronBarCode;

IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";

// One call — all pages, all barcodes, no image extraction step
var results = BarcodeReader.Read("document.pdf");
foreach (var result in results)
{
    Console.WriteLine($"Page {result.PageNumber}: {result.Value}");
}

多條碼檢測

如果您有程式碼可以掃描包含多個條碼的文件:

之前(Cloudmersive——需要多次調用):

// Cloudmersive returns one barcode per scan — multiple calls for multiple barcodes
var scanApi = new BarcodeScanApi();
var barcodeValues = new List<string>();

foreach (var croppedRegion in CropBarcodeRegions("invoice.png"))
{
    using var stream = new MemoryStream(croppedRegion);
    var result = await scanApi.BarcodeScanImageAsync(stream);
    if (result.Successful == true)
        barcodeValues.Add(result.RawText);
}

之後(IronBarcode):

using IronBarCode;

IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";

var options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = true,
};

// One call — returns all barcodes in the image
var results = BarcodeReader.Read("invoice.png", options);
var barcodeValues = results.Select(r => r.Value).ToList();

移除不再需要的基礎設施

這是您刪除更多程式碼而非編寫新程式碼的遷移部分。

移除HTTP異常處理

Cloudmersive操作可能因網路錯誤、HTTP 4xx/5xx響應和超時異常而失敗。 這通常需要IronBarcode不需要的try/catch基礎設施:

// Remove all of this
try
{
    var result = await scanApi.BarcodeScanImageAsync(stream);
    // ...
}
catch (ApiException ex) when (ex.ErrorCode == 429)
{
    // Rate limited — wait and retry
    await Task.Delay(TimeSpan.FromSeconds(2));
    // retry logic...
}
catch (ApiException ex) when (ex.ErrorCode == 503)
{
    // Service unavailable — Cloudmersive is down
    _logger.LogError("Cloudmersive unavailable: {Message}", ex.Message);
    throw;
}
catch (HttpRequestException ex)
{
    // Network error
    _logger.LogError("Network failure: {Message}", ex.Message);
    throw;
}
catch (TaskCanceledException)
{
    // Timeout
    _logger.LogError("Cloudmersive request timed out");
    throw;
}

IronBarcode不會拋出網路異常。 如果無法從輸入解碼條碼,它會拋出BarcodeException,這是內容問題而不是基礎設施問題。 簡化的錯誤處理:

using IronBarCode;

var results = BarcodeReader.Read("barcode.png");
if (!results.Any())
{
    _logger.LogWarning("No barcode detected in image");
}

移除重試邏輯

如果您的Cloudmersive整合包含重試政策——Polly,自定義重試迴圈或指數退避——完全刪除它們:

// Remove retry infrastructure
// var retryPolicy = Policy
//     .Handle<ApiException>()
//     .WaitAndRetryAsync(3, retryAttempt =>
//         TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));

// IronBarcode — no retry needed
var results = BarcodeReader.Read("barcode.png");

移除速率限制追蹤

如果您的程式碼追蹤API調用計數以保持在Cloudmersive的每月限額內:

// Remove quota tracking
// private static int _monthlyCallCount = 0;
// private const int MonthlyLimit = 30000;
//
// if (Interlocked.Increment(ref _monthlyCallCount) > MonthlyLimit)
//     throw new InvalidOperationException("Monthly Cloudmersive quota exceeded");

// IronBarcode — no quota
var results = BarcodeReader.Read("barcode.png");

移除網路超時配置

Cloudmersive客戶端通常包含超時配置。 移除它:

// Remove timeout configuration
// Configuration.Default.Timeout = 30000; // 30 second timeout
// Configuration.Default.ConnectionTimeout = 5000;

// IronBarcode has no network timeout — no network

移除API金鑰輪換

如果您的應用程式按照計劃輪換API金鑰或將它們儲存在秘密管理中:

// Simplify this pattern
// private string GetCloudmersiveApiKey() =>
//     _secretManager.GetSecret("cloudmersive-api-key-current");
//
// Configuration.Default.ApiKey["Apikey"] = GetCloudmersiveApiKey();

// IronBarcode — set once at startup
IronBarCode.License.LicenseKey = _configuration["IronBarcode:LicenseKey"];
// or literal: "YOUR-LICENSE-KEY"

常見遷移問題

Cloudmersive返回byte[]進行生成

Cloudmersive的生成方法直接返回byte[]。 IronBarcode返回一個GeneratedBarcode物件,為您提供更多選項:

// Cloudmersive returned byte[] directly
byte[] cloudmersiveResult = apiInstance.GenerateBarcodeQRCode("data");

// IronBarcode — call ToPngBinaryData() for the equivalent byte[]
byte[] ironBarcodeResult = BarcodeWriter.CreateBarcode("data", BarcodeEncoding.QRCode)
    .ToPngBinaryData();

// Or save directly (often simpler)
BarcodeWriter.CreateBarcode("data", BarcodeEncoding.QRCode)
    .SaveAsPng("output.png");

// Or get other formats
byte[] jpegBytes = BarcodeWriter.CreateBarcode("data", BarcodeEncoding.QRCode)
    .ToJpegBinaryData();

Cloudmersive讀取返回JSON響應模型

Cloudmersive的掃描結果是一個響應模型,具有BarcodeType等屬性。 IronBarcode返回型別化結果物件:

// Cloudmersive result model
if (cloudmersiveResult.Successful == true)
{
    string value = cloudmersiveResult.RawText;
    string type = cloudmersiveResult.BarcodeType;
}

// IronBarcode result — use .Value and .Format directly
var results = BarcodeReader.Read("barcode.png");
if (results.Any())
{
    string value = results.First().Value;
    string format = results.First().Format.ToString();
}

多種類的Cloudmersive API實例

Cloudmersive將生成和掃描分為BarcodeScanApi實例。 IronBarcode使用靜態類——不需要實例:

// Cloudmersive — two separate API instances
var generateApi = new GenerateBarcodeApi();
var scanApi = new BarcodeScanApi();

// IronBarcode — static, no instances
// BarcodeWriter for generation
// BarcodeReader for reading
// QRCodeWriter for QR codes specifically

遷移檢查清單

在遷移之前和之後在您的程式碼庫中運行這些搜索,以確認您已經捕獲到所有Cloudmersive引用:

# Find all Cloudmersive references
grep -r "Cloudmersive" --include="*.cs" .
grep -r "GenerateBarcodeApi" --include="*.cs" .
grep -r "BarcodeScanApi" --include="*.cs" .
grep -r "Configuration.Default.ApiKey" --include="*.cs" .
grep -r '"Apikey"' --include="*.cs" .
grep -r "GenerateBarcodeQRCode" --include="*.cs" .
grep -r "GenerateBarcodeCode128" --include="*.cs" .
grep -r "BarcodeScanImage" --include="*.cs" .
grep -r "cloudmersive" --include="*.csproj" .
grep -r "cloudmersive" --include="*.json" .
SHELL

遷移後,所有搜索應返回零結果。 然後確認IronBarcode被正確引用:

grep -r "IronBarCode" --include="*.cs" .
grep -r "BarcodeReader" --include="*.cs" .
grep -r "BarcodeWriter" --include="*.cs" .
SHELL

配置檢查清單

  • 從所有Cloudmersive.APIClient.NET.Barcode
  • appsettings.json中、環境變數和秘密儲存移除Cloudmersive API金鑰
  • 新增IronBarcode NuGet包
  • 在應用程式啟動時新增IronBarCode.License.LicenseKey初始化
  • 移除Cloudmersive呼叫的重試政策和入圍斷路器
  • 移除速率限制追蹤程式碼
  • 移除網路超時配置

測試檢查清單

完成遷移後:

  • 確認QR碼生成產生有效可掃描輸出
  • 確認Code128和其他線性條碼生成
  • 確認從常見圖像格式(PNG、JPEG、BMP)讀取條碼
  • 如果處理PDF文件,測試PDF讀取
  • 如果適用,測試多條碼檢測
  • 確認網路流量中沒有Cloudmersive API調用出現
  • 確認應用程式在沒有互聯網連接的情況下運行正常

遷移後的收益

除了節省成本,遷移消除了整個操作性問題類:

**不再需要依賴網路的條碼處理。**您的文件處理流程無論Cloudmersive的可用性、您的互聯網連接質量或網路的延遲峰值如何,均能運行。

**無需再進行配額管理。**文件量月末峰值不會造成意外的超支或處理失敗。

**無需再擔心資料離開您的網路。**包含敏感客戶、財務或健康資訊的條碼圖像留在您的基礎設施中。

**簡化的程式碼。**生產Cloudmersive整合需要的重試基礎設施、超時處理和HTTP異常管理已不再需要。 替代的是直接的方法調用。

遷移是機械性的。 其優點是結構性的——消除對工作流不需要的外部網路服務的依賴。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

相關文章

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預訂您的免費即時演示
Booking Badge

全球數百萬工程師信賴

Iron Software的客戶標誌
獲取您的無義務諮詢
完成下方表單或發送電子郵件至sales@ironsoftware.com
您的詳細資訊將始終保密。
全球數百萬工程師信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立