如何從系統繪圖物件讀取條碼
從Cloudmersive Barcode API遷移到IronBarcode
這是在.NET條碼歷史上最簡單的遷移。 移除HTTP客戶端。 新增一個NuGet包。 刪除API金鑰管理。 其餘的是查找和替換。
Cloudmersive的.NET SDK是一個生成的REST API客戶端——在HttpClient調用周圍的薄包裝。 您應用程式中每一段特定於Cloudmersive的程式碼都是該客戶端的配置、通過該客戶端的調用,或者是網路請求失敗時的錯誤處理。 IronBarcode不需要任何這樣的基礎設施,因為它在本地處理條碼,不依賴網路。
快速開始:三步驟
步驟1:交換套件
dotnet remove package Cloudmersive.APIClient.NET.Barcode
dotnet add package BarCode
dotnet remove package Cloudmersive.APIClient.NET.Barcode
dotnet add package BarCode
步驟2:替換命名空間
// Remove these
using Cloudmersive.APIClient.NET.Barcode.Api;
using Cloudmersive.APIClient.NET.Barcode.Client;
// Add this
using IronBarCode;
// Remove these
using Cloudmersive.APIClient.NET.Barcode.Api;
using Cloudmersive.APIClient.NET.Barcode.Client;
// Add this
using IronBarCode;
Imports 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";
// 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";
' 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
using IronBarCode;
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY";
var builder = WebApplication.CreateBuilder(args);
// ... rest of startup
Imports IronBarCode
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim builder = WebApplication.CreateBuilder(args)
' ... rest of startup
成本:您之前的花費與您將要花費的
在遷移之前,值得計算Cloudmersive整合實際的成本。 計算非常簡單:
| 您的日消耗量 | 每月請求數 | Cloudmersive級別 | IronBarcode(一次性) |
|---|---|---|---|
| 每天100次 | 約3,000次 | 基本套餐$19.99/月 | $749 |
| 每天1,000次 | 約30,000次 | 商業高端$99.99/月 | $749 |
| 每天5,000次 | 約150,000次 | 中型企業$499.99/月 | $749 |
| 每天10,000次 | 約300,000次 | 企業(自定義) | $749 |
| 每天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);
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);
Imports Cloudmersive.APIClient.NET.Barcode.Api
Imports Cloudmersive.APIClient.NET.Barcode.Client
Configuration.Default.ApiKey.Add("Apikey", "YOUR-CLOUDMERSIVE-API-KEY")
Dim apiInstance As New GenerateBarcodeApi()
Dim result As Byte() = 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");
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");
Imports IronBarCode
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim result As Byte() = 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);
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);
Imports Cloudmersive.APIClient.NET.Barcode.Api
Imports Cloudmersive.APIClient.NET.Barcode.Client
Configuration.Default.ApiKey.Add("Apikey", "YOUR-CLOUDMERSIVE-API-KEY")
Dim apiInstance As New GenerateBarcodeApi()
Dim result As Byte() = 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();
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();
Imports IronBarCode
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY"
BarcodeWriter.CreateBarcode("SHIP-2024031500428", BarcodeEncoding.Code128) _
.SaveAsPng("barcode.png")
' Or get bytes directly
Dim result As Byte() = 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}");
}
}
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}");
}
}
Imports Cloudmersive.APIClient.NET.Barcode.Api
Imports Cloudmersive.APIClient.NET.Barcode.Client
Configuration.Default.ApiKey.Add("Apikey", "YOUR-CLOUDMERSIVE-API-KEY")
Dim scanApi As New BarcodeScanApi()
Using stream = File.OpenRead("barcode.png")
Dim result = scanApi.BarcodeScanImage(stream)
If result.Successful = True Then
Console.WriteLine($"Value: {result.RawText}")
Console.WriteLine($"Type: {result.BarcodeType}")
End If
End Using
之後(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}");
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}");
Imports IronBarCode
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim results = BarcodeReader.Read("barcode.png")
Dim 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;
}
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;
}
Imports Cloudmersive.APIClient.NET.Barcode.Api
Imports Cloudmersive.APIClient.NET.Barcode.Client
Configuration.Default.ApiKey.Add("Apikey", "YOUR-CLOUDMERSIVE-API-KEY")
Dim scanApi As New BarcodeScanApi()
Using stream = File.OpenRead("barcode.png")
Dim result = Await scanApi.BarcodeScanImageAsync(stream)
Return If(result.Successful = True, result.RawText, Nothing)
End Using
之後(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;
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;
Imports IronBarCode
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY"
' IronBarcode is synchronous; wrap in Task.Run to free the calling thread for CPU-bound work
Dim results = Await Task.Run(Function() 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}");
}
}
// 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}");
}
}
Imports System.IO
Imports Cloudmersive.APIClient.NET.BarcodeScan
' Requires a separate PDF library to extract pages as images,
' then one Cloudmersive API call per extracted page image.
Dim scanApi As New BarcodeScanApi()
For Each pageImagePath In ExtractPdfPages("document.pdf")
Using stream As FileStream = File.OpenRead(pageImagePath)
Dim result = Await scanApi.BarcodeScanImageAsync(stream)
If result.Successful = True Then
Console.WriteLine($"Found: {result.RawText}")
End If
End Using
Next
之後(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}");
}
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}");
}
Imports IronBarCode
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY"
' One call — all pages, all barcodes, no image extraction step
Dim results = BarcodeReader.Read("document.pdf")
For Each result In results
Console.WriteLine($"Page {result.PageNumber}: {result.Value}")
Next
多條碼檢測
如果您有程式碼可以掃描包含多個條碼的文件:
之前(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);
}
// 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);
}
Imports System.IO
Imports System.Collections.Generic
Imports System.Threading.Tasks
' Cloudmersive returns one barcode per scan — multiple calls for multiple barcodes
Dim scanApi As New BarcodeScanApi()
Dim barcodeValues As New List(Of String)()
For Each croppedRegion In CropBarcodeRegions("invoice.png")
Using stream As New MemoryStream(croppedRegion)
Dim result = Await scanApi.BarcodeScanImageAsync(stream)
If result.Successful = True Then
barcodeValues.Add(result.RawText)
End If
End Using
Next
之後(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();
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();
Imports IronBarCode
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim options As New BarcodeReaderOptions With {
.Speed = ReadingSpeed.Balanced,
.ExpectMultipleBarcodes = True
}
' One call — returns all barcodes in the image
Dim results = BarcodeReader.Read("invoice.png", options)
Dim barcodeValues = results.Select(Function(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;
}
// 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;
}
Imports System
Imports System.Threading.Tasks
' Remove all of this
Try
Dim result = Await scanApi.BarcodeScanImageAsync(stream)
' ...
Catch ex As ApiException When ex.ErrorCode = 429
' Rate limited — wait and retry
Await Task.Delay(TimeSpan.FromSeconds(2))
' retry logic...
Catch ex As ApiException When ex.ErrorCode = 503
' Service unavailable — Cloudmersive is down
_logger.LogError("Cloudmersive unavailable: {Message}", ex.Message)
Throw
Catch ex As HttpRequestException
' Network error
_logger.LogError("Network failure: {Message}", ex.Message)
Throw
Catch ex As TaskCanceledException
' Timeout
_logger.LogError("Cloudmersive request timed out")
Throw
End Try
IronBarcode不會拋出網路異常。 如果無法從輸入解碼條碼,它會拋出BarcodeException,這是內容問題而不是基礎設施問題。 簡化的錯誤處理:
using IronBarCode;
var results = BarcodeReader.Read("barcode.png");
if (!results.Any())
{
_logger.LogWarning("No barcode detected in image");
}
using IronBarCode;
var results = BarcodeReader.Read("barcode.png");
if (!results.Any())
{
_logger.LogWarning("No barcode detected in image");
}
Imports IronBarCode
Dim results = BarcodeReader.Read("barcode.png")
If Not results.Any() Then
_logger.LogWarning("No barcode detected in image")
End If
移除重試邏輯
如果您的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");
// 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");
' Remove retry infrastructure
' Dim retryPolicy = Policy _
' .Handle(Of ApiException)() _
' .WaitAndRetryAsync(3, Function(retryAttempt) _
' TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))
' IronBarcode — no retry needed
Dim 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");
// 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");
' Remove quota tracking
' Private Shared _monthlyCallCount As Integer = 0
' Private Const MonthlyLimit As Integer = 30000
'
' If Interlocked.Increment(_monthlyCallCount) > MonthlyLimit Then
' Throw New InvalidOperationException("Monthly Cloudmersive quota exceeded")
' IronBarcode — no quota
Dim 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
// Remove timeout configuration
// Configuration.Default.Timeout = 30000; // 30 second timeout
// Configuration.Default.ConnectionTimeout = 5000;
// IronBarcode has no network timeout — no network
' 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"
// 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"
' Simplify this pattern
' Private Function GetCloudmersiveApiKey() As String
' Return _secretManager.GetSecret("cloudmersive-api-key-current")
' End Function
'
' 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 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();
Imports Cloudmersive.APIClient
Imports IronBarCode
' Cloudmersive returned byte() directly
Dim cloudmersiveResult As Byte() = apiInstance.GenerateBarcodeQRCode("data")
' IronBarcode — call ToPngBinaryData() for the equivalent byte()
Dim ironBarcodeResult As Byte() = BarcodeWriter.CreateBarcode("data", BarcodeEncoding.QRCode) _
.ToPngBinaryData()
' Or save directly (often simpler)
BarcodeWriter.CreateBarcode("data", BarcodeEncoding.QRCode) _
.SaveAsPng("output.png")
' Or get other formats
Dim jpegBytes As Byte() = 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 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 result model
If cloudmersiveResult.Successful = True Then
Dim value As String = cloudmersiveResult.RawText
Dim type As String = cloudmersiveResult.BarcodeType
End If
' IronBarcode result — use .Value and .Format directly
Dim results = BarcodeReader.Read("barcode.png")
If results.Any() Then
Dim value As String = results.First().Value
Dim format As String = results.First().Format.ToString()
End If
多種類的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 — 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 — two separate API instances
Dim generateApi As New GenerateBarcodeApi()
Dim scanApi As 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" .
# 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" .
遷移後,所有搜索應返回零結果。 然後確認IronBarcode被正確引用:
grep -r "IronBarCode" --include="*.cs" .
grep -r "BarcodeReader" --include="*.cs" .
grep -r "BarcodeWriter" --include="*.cs" .
grep -r "IronBarCode" --include="*.cs" .
grep -r "BarcodeReader" --include="*.cs" .
grep -r "BarcodeWriter" --include="*.cs" .
配置檢查清單
- 從所有
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異常管理已不再需要。 替代的是直接的方法調用。
遷移是機械性的。 其優點是結構性的——消除對工作流不需要的外部網路服務的依賴。
常見問題
為什麼我應該從Cloudmersive Barcode API遷移到IronBarcode?
常見的理由包括簡化授權(去除SDK +運行時鑰匙的複雜性)、消除吞吐量限制、獲得原生PDF支持、改善Docker/CI/CD部署,並減少生產程式碼中的API樣板。
如何用IronBarcode取代Cloudmersive API調用?
用IronBarCode.License.LicenseKey = "key"替換實例建立和授權樣板。用BarcodeReader.Read(path)替換讀取呼叫,用BarcodeWriter.CreateBarcode(data, encoding)替換寫入呼叫。靜態方法不需要實例管理。
從Cloudmersive Barcode API遷移到IronBarcode需要更改多少程式碼?
大多數遷移會導致較少的程式碼行。授權樣板、實例構造函式和顯式格式配置被移除。核心讀/寫操作映射為較短的IronBarcode等價物,且結果物件更清晰。
在遷移期間,我需要同時保持Cloudmersive Barcode API和IronBarcode安裝嗎?
不需要。大多數遷移是直接替換而不是並行操作。一次遷移一個服務類,替換NuGet引用,並更新實例化和API呼叫模式後再移動到下一個類。
IronBarcode的NuGet包名稱是什麼?
包名是'IronBarCode'(大寫B和C)。可以用'Install-Package IronBarCode'或'dotnet add package IronBarCode'安裝。程式碼中的using指令是'using IronBarCode;'。
與Cloudmersive Barcode API相比,IronBarcode如何簡化Docker部署?
IronBarcode是沒有外部SDK文件或掛載許可配置的NuGet封裝。在Docker中,設置IRONBARCODE_LICENSE_KEY環境變數,包在啟動時處理許可驗證。
從Cloudmersive遷移後,IronBarcode會自動檢測所有條碼格式嗎?
是的。IronBarcode自動檢測所有支持的格式的符號學。不需要顯式的BarcodeTypes枚舉。如果格式已經知道並且性能很重要,BarcodeReaderOptions允許限制搜索空間作為優化。
IronBarcode是否可以不使用單獨的庫讀取PDF中的條碼?
可以。BarcodeReader.Read("document.pdf")原生處理PDF文件。結果包括每個條碼發現的頁碼、格式、值和置信度。不需要外部PDF渲染步驟。
IronBarcode如何處理並行條碼處理?
IronBarcode的靜態方法是無狀態且執行緒安全的。直接使用Parallel.ForEach遍歷文件列表,不需要每個執行緒的實例管理。BarcodeReaderOptions.MaxParallelThreads控制內部執行緒預算。
從Cloudmersive Barcode API遷移到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和流在內的多種輸出格式。

