跳至頁尾內容
與其他組件的比較

Cloudmersive 條碼 API 與 IronBarcode:C# 條碼庫比較

Google ML Kit 條碼掃描與 IronBarcode:.NET 開發者需要知道的事項

Google ML Kit 的條碼掃描在 Android 上的表現非常令人印象深刻。 它支持 Google Lens,可以處理損壞和部分遮蔽的程式碼,能夠在裝置上運行而不需要網路連接,並且支持多種 1D 和 2D 格式。 當 .NET 開發人員在條碼程式庫比較中遇到它時,這些事實是準確的。 比對文章中經常省略的事實是,ML Kit 沒有 .NET SDK、沒有 NuGet 套件,並且沒有 C# API。 本文針對那些在清單中找到 ML Kit 並需要了解在 .NET 專案中實際使用 ML Kit 的開發者,以及管理程式碼等效項的樣子。

Google ML Kit 條碼掃描是什麼

Google ML Kit 條碼掃描是一個原生的行動程式庫。 這不是需要解決的限制; 這是產品的設計。 ML Kit 有兩個版本:

  • Android: 通過 Google Maven 作為 com.google.mlkit:barcode-scanning 發佈(Kotlin/Java)。 需要在目標裝置上使用 Google Play 服務。處理由相機幀、檔案 URI 或位圖構建的 InputImage 物件。
  • iOS: 通過 CocoaPods 發佈為 GoogleMLKit/BarcodeScanning。 使用 Swift 或 Objective-C。

沒有 dotnet add package google-mlkit-barcode。 沒有 using Google.MLKit.BarcodeScanning;。 Google 沒有為 ML Kit 發佈 .NET 綁定。

社群維護的 Xamarin 和 MAUI 綁定(如 BarcodeScanning.Native.MauiXamarin.GooglePlayServices.MLKit.BarcodeScanning)在某些時候存在,但它們共有一個挑戰:ML Kit 經常更新其 Android 和 iOS SDK,而由個人或小團隊維護的綁定專案可能會在底層 API 發生變更時落後或中斷。 ML Kit 條碼掃描沒有 Google 的第一方 .NET NuGet 套件。

ML Kit 的優勢

瞭解為什麼 ML Kit 會出現在條碼比較中是有用的背景知識。 在原生 Android 上:

  • 裝置內部推理: 沒有伺服器回合。ML Kit 的模型使用 Google Play 服務本地運行,這意味著掃描時延遲低,沒有網路依賴。
  • 耐损性: ML 模型比許多基於閾值的解碼器更好地處理損壞、部分遮蔽或低解析度的條碼。 這對於面向消費者的應用掃描真實世界的程式碼來說是個真正的區別因素。
  • Google Lens 整合: ML Kit 的掃描器是支持 Google Lens 條碼偵測的相同堆疊。 這是一個強大的品質信號。
  • 行動上的格式廣度: 原生支持 QR Code、EAN-13、EAN-8、Code 128、Code 39、Code 93、Codabar、ITF、PDF417、Data Matrix、Aztec 和 UPC-A/UPC-E。
  • 零配置: 一段三行的 Kotlin 程式碼片段可以產生一個運行的掃描器,無需調整閾值或選擇解碼器策略。

這些是合法的優勢。 對於 .NET 開發者來說,問題不是 ML Kit 不好——而是 ML Kit 無法在不進行大量工作情況下供他們使用。

.NET 開發的現實

當 .NET 開發者需要掃描條碼時,ML Kit 的路徑在實踐中看起來像這樣:

沒有 async/await C# API。 ML Kit 使用 Android 的 Task API,並利用 addOnSuccessListeneraddOnFailureListener 回調。 這些無法映射到 .NET 的 Task<t>await。 任何綁定層都需要在基於回調的 Android 非同步和 .NET 的 TPL 之間進行調整——這是一個非簡單的翻譯,社群綁定處理不一致。

無 .NET 依賴注入整合。 BarcodeScanning.getClient(options) 是一個靜態工廠調用,返回一個 Android BarcodeScanner 物件。 沒有可以註冊的 .NET 介面,沒有 IServiceCollection.AddBarcodeScanner(),無法將其注入到 ASP.NET Core 中間件或 Azure Functions。

無 ASP.NET Core 或 Azure Function 支援。 ML Kit 需要 Android 或 iOS 運行時環境。 無法在 Linux 的 web API 過程中運行、在 Azure Function 上運行、在 Windows Server 上運行或在 Docker 容器中運行。 如果您的使用案例是伺服器端條碼處理——比如接受圖像並返回條碼資料的 REST 端點、文件處理管道、大批量任務——不管是否提供綁定,ML Kit 在架構上都不相容。

僅限攝像頭或幀輸入——無檔、無 PDF。 ML Kit 的 InputImage 可以從 ByteBuffer 或 Android 文件系統上的檔案 URI 構建。 不存在處理 PDF 文件、遍歷頁面或處理多頁 TIFF 的概念。 伺服器端的文件處理完全不在產品的範圍內。

Google Play 服務依賴(無捆綁模式)。 無捆綁的 ML 模型變體 (com.google.android.gms:play-services-mlkit-barcode-scanning) 通過 Google Play 服務運行。 沒有 Play 服務的裝置——自訂 Android 構建、某些企業裝置、Amazon Fire 平板電腦——無法使用該變體。 捆綁模型選項 (com.google.mlkit:barcode-scanning) 在沒有 Play 服務的情況下運行,但會增加約 2.4 MB 的 APK 大小。

移植 Android ML Kit 程式碼到 .NET

如果您已有一個具備 ML Kit 條碼掃描的 Android 應用,並且您要將其移植到 .NET MAUI 或 .NET 9,以下是翻譯。 Kotlin 程式碼使用 ML Kit 的回調模式; C# 程式碼使用IronBarcode的同步 API。

// Android Kotlin: ML Kit
val options = BarcodeScannerOptions.Builder()
    .setBarcodeFormats(Barcode.FORMAT_QR_CODE, Barcode.FORMAT_CODE_128)
    .build()
val scanner = BarcodeScanning.getClient(options)
val inputImage = InputImage.fromFilePath(context, uri)
scanner.process(inputImage)
    .addOnSuccessListener { barcodes ->
        for (barcode in barcodes) {
            val rawValue = barcode.rawValue
            val format = barcode.format
        }
    }
    .addOnFailureListener { e -> Log.e("MLKit", e.message ?: "") }
// .NET C#: IronBarcode
// NuGet: dotnet add package BarCode
using IronBarCode;

var results = BarcodeReader.Read("captured-image.jpg");
foreach (var barcode in results)
{
    Console.WriteLine($"{barcode.Format}: {barcode.Value}");
}
// .NET C#: IronBarcode
// NuGet: dotnet add package BarCode
using IronBarCode;

var results = BarcodeReader.Read("captured-image.jpg");
foreach (var barcode in results)
{
    Console.WriteLine($"{barcode.Format}: {barcode.Value}");
}
Imports IronBarCode

Dim results = BarcodeReader.Read("captured-image.jpg")
For Each barcode In results
    Console.WriteLine($"{barcode.Format}: {barcode.Value}")
Next
$vbLabelText   $csharpLabel

結構上的差異不僅僅是語法問題。 ML Kit 版本會設置一個掃描器物件、構建 InputImage、調用 scanner.process(),並註冊成功和失敗回調。 如果您需要在繼續之前獲得結果,您必須協調回調的執行與其餘邏輯——通常通過 Java 中的 CountDownLatch 或 Kotlin 中的協程。

IronBarcode 的 BarcodeReader.Read() 會同步返回一個結果集合。 您可以立即遍歷它。 沒有回調註冊、沒有執行緒同步、沒有需要管理的獨立掃描器物件。

對於移植 Android 程式碼的團隊,多條碼場景的模式翻譯如下:

// .NET C#:IronBarcode— reading multiple barcodes with options
using IronBarCode;

var options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = true,
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128
};

var results = BarcodeReader.Read("image.jpg", options);
foreach (var barcode in results)
{
    Console.WriteLine($"Format: {barcode.Format}, Value: {barcode.Value}");
}
// .NET C#:IronBarcode— reading multiple barcodes with options
using IronBarCode;

var options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = true,
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128
};

var results = BarcodeReader.Read("image.jpg", options);
foreach (var barcode in results)
{
    Console.WriteLine($"Format: {barcode.Format}, Value: {barcode.Value}");
}
Imports IronBarCode

Dim options As New BarcodeReaderOptions With {
    .Speed = ReadingSpeed.Balanced,
    .ExpectMultipleBarcodes = True,
    .ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128
}

Dim results = BarcodeReader.Read("image.jpg", options)
For Each barcode In results
    Console.WriteLine($"Format: {barcode.Format}, Value: {barcode.Value}")
Next
$vbLabelText   $csharpLabel

ExpectBarcodeTypes 標誌是 ML Kit 的 setBarcodeFormats() 的等價物。 設置它會縮小搜索範圍並提高性能,但與 ML Kit 不同的是,未設置狀態不會打破讀取——IronBarcode 會嘗試所有受支持的格式。

格式覆蓋比較

兩者工具覆蓋了主流的 2D 和 1D 格式。 重疊是實質性的。

格式 ML Kit (Android) IronBarcode
QR Code
EAN-13
EAN-8
UPC-A
UPC-E
Code 128
Code 39
Code 93
Codabar
ITF
PDF417
Data Matrix
Aztec
Code 11
MSI Plessey
Pharmacode
交錯 2 of 5 通過 ITF
RSS-14 / GS1 Databar
微型 QR
MaxiCode

IronBarcode 支援 50 多種符號,跨越閱讀和生成。 ML Kit 的格式列表固定為 Google 與模型一同發佈的內容——您無法新增自訂的符號。

IronBarcode的附加功能

除了閱讀之外,IronBarcode 覆蓋了完整的條碼工作流程:

生成。 ML Kit 根本沒有生成 API——它讀取條碼,而非建立條碼。IronBarcode生成任何支援的格式到 PNG、JPEG、SVG、HTML 或二進制資料。

// Generate aCode 128barcode to file
BarcodeWriter.CreateBarcode("SHIP-7834", BarcodeEncoding.Code128)
    .SaveAsPng("shipping-label.png");
// Generate aCode 128barcode to file
BarcodeWriter.CreateBarcode("SHIP-7834", BarcodeEncoding.Code128)
    .SaveAsPng("shipping-label.png");
' Generate a Code 128 barcode to file
BarcodeWriter.CreateBarcode("SHIP-7834", BarcodeEncoding.Code128) _
    .SaveAsPng("shipping-label.png")
$vbLabelText   $csharpLabel

QR code 客製化。 QRCodeWriter 支援嵌入標誌、更換顏色和錯誤修正級別——這些功能在 ML Kit 中沒有等效項,因為 ML Kit 並不生成任何東西。

// QR code with embedded logo and custom color
QRCodeWriter.CreateQrCode("https://example.com/product/4821", 500)
    .AddBrandLogo("company-logo.png")
    .ChangeBarCodeColor(System.Drawing.Color.DarkBlue)
    .SaveAsPng("product-qr.png");
// QR code with embedded logo and custom color
QRCodeWriter.CreateQrCode("https://example.com/product/4821", 500)
    .AddBrandLogo("company-logo.png")
    .ChangeBarCodeColor(System.Drawing.Color.DarkBlue)
    .SaveAsPng("product-qr.png");
' QR code with embedded logo and custom color
QRCodeWriter.CreateQrCode("https://example.com/product/4821", 500) _
    .AddBrandLogo("company-logo.png") _
    .ChangeBarCodeColor(System.Drawing.Color.DarkBlue) _
    .SaveAsPng("product-qr.png")
$vbLabelText   $csharpLabel

PDF 處理。 BarcodeReader.Read("document.pdf") 原生地從 PDF 的每一頁中讀取條碼。 無需圖像提取步驟,無需使用獨立的 PDF 程式遍歷頁面迴圈——它直接處理 PDF。

// Read all barcodes from every page of a PDF
var results = BarcodeReader.Read("invoice-batch.pdf");
foreach (var barcode in results)
{
    Console.WriteLine($"Page {barcode.PageNumber}: {barcode.Value}");
}
// Read all barcodes from every page of a PDF
var results = BarcodeReader.Read("invoice-batch.pdf");
foreach (var barcode in results)
{
    Console.WriteLine($"Page {barcode.PageNumber}: {barcode.Value}");
}
Imports System

' Read all barcodes from every page of a PDF
Dim results = BarcodeReader.Read("invoice-batch.pdf")
For Each barcode In results
    Console.WriteLine($"Page {barcode.PageNumber}: {barcode.Value}")
Next
$vbLabelText   $csharpLabel

伺服器端和雲端。IronBarcode可以運行在 Windows、Linux、macOS、Docker、Azure Functions 和 AWS Lambda 上。 沒有 Google Play 服務要求,無攝影機,無行動運行時。

二進制輸出。 .ToPngBinaryData() 直接返回 byte[]——對於不將條碼圖像寫入磁碟而作為 HTTP 回應返回 API 的情境非常實用。

// Generate barcode and return as byte array (e.g., in a web API)
byte[] barcodeBytes = BarcodeWriter.CreateBarcode("ORDER-8821", BarcodeEncoding.QRCode)
    .ToPngBinaryData();
// Generate barcode and return as byte array (e.g., in a web API)
byte[] barcodeBytes = BarcodeWriter.CreateBarcode("ORDER-8821", BarcodeEncoding.QRCode)
    .ToPngBinaryData();
' Generate barcode and return as byte array (e.g., in a web API)
Dim barcodeBytes As Byte() = BarcodeWriter.CreateBarcode("ORDER-8821", BarcodeEncoding.QRCode) _
    .ToPngBinaryData()
$vbLabelText   $csharpLabel

特性比較

特性 Google ML Kit 條碼 IronBarcode
.NET / C# API 無(無 NuGet 套件) 是的——using IronBarCode;
NuGet套件 None BarCode
條碼讀取 是的(僅 Android/iOS) 是的(所有平台)
條碼生成 是的(50 多種格式)
QR碼生成
QR 標誌嵌入
PDF 輸入支援 是的(原生)
檔案輸入(PNG/JPEG) 是的(通過 InputImage URI)
攝像頭/幀輸入 是的(主要使用案例) 通過圖像檔案
伺服器端處理
ASP.NET Core 支援
Azure 函式
Docker/Linux
Windows 應用支援 否(僅 Android/iOS)
Google Play 服務依賴 是的(無捆綁模式)
Firebase 依賴 否(自 2020 年獨立)
Async/await C# API 同步
一張圖片中的多條碼
社群 .NET 綁定 社群維護,不是第一方 不適用(原生 .NET)
定價 免費(裝置上的 API) 從 $749 永久使用

API 概念映射

由於沒有官方的 .NET ML Kit API,此映射是概念性的——它顯示了每個 ML Kit 操作的意圖以及IronBarcode的等價物。

ML Kit(Kotlin/Java 概念) IronBarcode(.NET C#)
BarcodeScannerOptions.Builder() new BarcodeReaderOptions { }
.setBarcodeFormats(FORMAT_QR_CODE) ExpectBarcodeTypes = BarcodeEncoding.QRCode
BarcodeScanning.getClient(options) 靜態——不需要掃描器物件
InputImage.fromFilePath(context, uri) 文件路徑字串傳遞給 BarcodeReader.Read()
InputImage.fromBitmap(bitmap, rotation) BarcodeReader.Read(stream) 或字節陣列重載
scanner.process(inputImage) BarcodeReader.Read(path, options)
.addOnSuccessListener { barcodes -> } Read() 的返回值——直接迭代
.addOnFailureListener { e -> } 標準 try/catch
barcode.rawValue barcode.Value
barcode.format barcode.Format
barcode.boundingBox 結果物件上的區域資料
Barcode.FORMAT_QR_CODE BarcodeEncoding.QRCode
Barcode.FORMAT_CODE_128 BarcodeEncoding.Code128
無生成 API BarcodeWriter.CreateBarcode()
無生成 API QRCodeWriter.CreateQrCode()
Speed / 模型選擇 ReadingSpeed.Balanced / .Faster / .Detailed

何時考慮 IronBarcode

將 Android 應用移植到 .NET MAUI 或 .NET 9。 如果您的 Android 應用使用 ML Kit 進行條碼掃描,並且您正在構建 .NET 等效項,IronBarcode 是自然的目標。 概念轉換是直接的——讀取文件,獲得結果,迭代值。

伺服器端條碼處理。 任何涉及掃描條碼的 HTTP 端點、處理上傳圖像的後台作業或從 PDF 中提取程式碼的文件工作流程的場景。 ML Kit 不能參與任何這些。

Windows 桌面應用程式。 WinForms、WPF 或在 Windows 上的 .NET MAUI。 ML Kit 根本無法在 Windows 上運行。

沒有 Google Play 服務的環境。 如果您的 Android 部署包括不具 Google Play 服務的裝置——企業硬體、自訂 AOSP 構建、Amazon Fire 裝置——無捆綁的 ML Kit 模型無法使用,只有更大的捆綁變異可用。IronBarcode沒有這種依賴。

讀取與生成同時進行。 如果您需要生成和讀取條碼——例如,印刷送貨標籤並驗證掃描——IronBarcode 在同一個程式庫中處理兩者。 ML Kit 不處理生成或伺服器端讀取。

Azure 或 AWS 部署。 Azure Functions、Linux 上的 Azure App Service、AWS Lambda——這些都無法承載 ML Kit。IronBarcode目標為所有這些。

授權與定價

Google ML Kit 條碼掃描的裝置 API 對於 Android 和 iOS 應用是免費的。沒有每次掃描的費用,也不需要商業授權。 這樣的定價模式適合其預期的背景——通過 Maven 和 CocoaPods 發佈的原生行動程式庫。

IronBarcode 是一個商業 .NET 程式庫,具有永久性授權:

  • Lite: $749——1 開發者,1 專案位置
  • Plus: $1,499——最多3名開發者
  • Professional: $2,999——最多 10 名開發者
  • Unlimited: $5,999——不限開發者

提供免費試用; 試用會在生成的條碼上新增浮水印。 對於已經運行伺服器基礎架構來處理文件的團隊而言,維護一個非官方的 ML Kit 綁定所需的工程工作量的成本通常只是授權成本的一小部分。

結論

Google ML Kit 條碼掃描是一個設計精良的程式庫,能夠在 Android 和 iOS 裝置上快速準確地執行其預期任務:掃描條碼。 與IronBarcode的比較不是在於哪個程式庫更好,更多是關於哪個程式庫存在於您需要的上下文中。

如果您在為 Android 編寫 Kotlin 並從攝影機掃描,ML Kit 是絕佳的選擇。如果您在為 .NET 編寫 C#——無論是用於 web API、文件處理服務、Windows 應用、雲功能或跨平台的 MAUI 應用——Google ML Kit 都沒有提供給您。 沒有 NuGet 套件,沒有 C# API,Google 對其在管理程式碼中的使用不提供支援。IronBarcode是一個原生 .NET 程式庫,涵蓋 .NET 支援的每個平台上的讀取和生成。

常見問題

什麼是Google ML Kit條碼掃描?

Google ML Kit條碼掃描是一個.NET條碼庫,用於在C#應用程式中生成和讀取條碼。這是開發人員在為.NET專案選擇條碼解決方案時評估的多個替代項之一。

Google ML Kit條碼掃描和IronBarcode之間的主要區別是什麼?

IronBarcode使用靜態、無狀態的API,不需要實例管理,而Google ML Kit條碼掃描通常需要建立和配置實例後才能使用。IronBarcode還提供本地PDF支援、自動格式檢測以及所有環境中的單鍵授權。

IronBarcode比Google ML Kit條碼掃描更容易授權嗎?

IronBarcode使用一個覆蓋開發和生產部署的單一授權金鑰。相比於將SDK金鑰與運行時金鑰分開的授權系統,這簡化了CI/CD管道和Docker配置。

IronBarcode支持所有Google ML Kit條碼掃描支持的條碼格式嗎?

IronBarcode支持超過30種條碼符號,包括QR Code、Code 128、Code 39、DataMatrix、PDF417、Aztec、EAN-13、UPC-A、GS1等。格式自動檢測意味著不需要顯式的格式枚舉。

IronBarcode支持原生PDF條碼讀取嗎?

是的。IronBarcode可以直接從PDF文件中讀取條碼,使用BarcodeReader.Read("document.pdf"),無需單獨的PDF渲染程式庫。每頁結果包括頁碼、條碼格式、值和置信分數。

與Google ML Kit條碼掃描相比,IronBarcode如何處理批量處理?

IronBarcode的靜態方法無狀態且天然執行緒安全,可以直接使用Parallel.ForEach而無需每個執行緒的實例管理。任何價格級別下都沒有吞吐量上限。

IronBarcode支持哪些.NET版本?

IronBarcode支持.NET Framework 4.6.2+、.NET Core 3.1,以及.NET 5、6、7、8和9,單一NuGet包。平台目標包括Windows x64/x86、Linux x64和macOS x64/ARM。

如何在.NET專案中安裝IronBarcode?

通過NuGet安裝IronBarcode:在Package Manager Console中運行 'Install-Package IronBarCode',或在CLI中運行 'dotnet add package IronBarCode'。不需要額外的SDK安裝程式或運行時文件。

我可以評估IronBarcode再購買之前,與Google ML Kit不同嗎?

可以。IronBarcode的試用模式返回完整解碼的條碼值——只有生成的輸出圖像上有水印。您可以在自己的文件上評估讀取準確性,然後再決定購買。

Google ML Kit條碼掃描與IronBarcode之間的定價差異是什麼?

IronBarcode起價為$749,為單開發者所適用的永久授權,覆蓋開發和生產。價格詳情和批量選項可在IronBarcode授權頁面獲得。無需單獨的運行時授權。

從Google ML Kit條碼掃描遷移到IronBarcode是否直接了當?

從Google ML Kit條碼掃描遷移到IronBarcode的過程主要涉及用IronBarcode的靜態方法替換基於實例的API調用,去除授權樣板,並更新結果屬性名稱。大多數遷移涉及減少程式碼而不是新增程式碼。

IronBarcode可以生成帶有Logo的QR碼嗎?

可以。QRCodeWriter.CreateQrCode().AddBrandLogo("logo.png") 可以原生嵌入品牌圖像進QR碼,並可配置錯誤更正。也支持通過ChangeBarCodeColor() 生成彩色QR碼。

Curtis Chau
技術作家

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

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話