跳過到頁腳內容
使用IRONBARCODE

使用現代C#掃描程式庫創建和讀取Xamarin條碼圖像

Xamarin 條碼解決方案對於維護零售、物流和庫存管理領域 Android 和 iOS 應用的行動開發人員來說仍然至關重要。 儘管微軟已於 2024 年 5 月停止對 Xamarin 的官方支持,但仍有數百萬個 Xamarin 應用仍在生產環境中運行,這些應用仍然需要可靠地創建、掃描和解碼條碼。 IronBarcode是一個.NET條碼 SDK,只需幾行 C# 程式碼即可處理條碼產生和條碼讀取。 它可以作為條碼掃描器和生成器在 Android、iOS 和 Windows 平台上運行,並且在專案遷移時可以直接過渡到.NET MAUI 。

立即開始IronBarcode的免費試用,並將 Xamarin 條碼讀取功能新增到您的專案中。

Xamarin 應用程式中的條碼掃描是如何運作的?

Xamarin 應用程式中的條碼掃描的工作原理是從相機畫面或裝置上儲存的檔案中擷取條碼訊息,然後將該資料傳遞給掃描庫來解碼編碼訊息。 從歷史上看,許多 Xamarin 開發人員使用開源的 ZXing 庫(Zebra Crossing),創建一個新的 ZXingScannerPage 實例,將其推入導航堆疊,並處理 OnScanResult 事件來處理條碼。 雖然這種方法在 Xamarin.Forms 中對基本的二維碼掃描用例有效,但 ZXing 多年來沒有得到有意義的錯誤修復,開發人員經常報告在光線較暗的情況下掃描條碼或解碼 Data Matrix 等複雜格式時出現錯誤情況。

IronBarcode是一款採用不同方法的現代條碼掃描器 SDK。 IronBarcode不提供具有即時相機介面的專用掃描頁面,而是處理 Xamarin 應用程式可以提供的任何來源,例如相機拍攝的影像、使用者上傳的檔案、螢幕截圖或 PDF 文件。 這使其成為 Xamarin 條碼專案的一個靈活的掃描庫,開發人員需要完全控制使用者介面和掃描過程。 BarcodeReader 類別支援 30 多種條碼和二維碼格式,SDK 包含內建的預處理過濾器,可提高在光線較暗或條碼損壞情況下的條碼掃描效能。

首先,請將IronBarcode NuGet套件安裝到您的 Xamarin 解決方案中:

Install-Package BarCode

BarCode 套件可在NuGet上取得,並支援.NET Standard 2.0,因此與 Xamarin.Forms、Xamarin 原生專案和 Microsoft 的 MAUI 框架相容。 核心 SDK 功能不需要額外的平台特定NuGet套件或設定步驟,安裝套件後即可立即在您的專案中進行測試。

如何在 Xamarin 應用程式中建立條碼和二維碼?

使用 IronBarcode 的 BarcodeWriter 類,可以輕鬆產生用於在裝置螢幕上顯示、貼標籤或進行資料交換的條碼。 下面的範例是 Xamarin 條碼應用程式的常見範例,這些應用程式需要產生一維和二維條碼。

using IronBarCode;
// Barcode generation: create a Code 128 barcode for product scanning
// Comment: BarcodeWriter handles all supported encoding formats
var barcode = BarcodeWriter.CreateBarcode(
    "PROD-2025-XMR",              // Value to encode in the barcode
    BarcodeWriterEncoding.Code128, // Barcode format for the scanner
    400, 150                       // Width and height in pixels
);
// Customize the barcode: add visible text below the barcode image
// Comment: this text helps users verify the encoded value
barcode.AddBarcodeValueTextBelowBarcode();
barcode.SaveAsPng("product_barcode.png");
// Generate a QR code that users can scan with any mobile device camera
// Comment: QR codes are ideal for URLs, contact data, and Wi-Fi config
var qrCode = BarcodeWriter.CreateBarcode(
    "https://ironsoftware.com",
    BarcodeWriterEncoding.QRCode,
    300, 300
);
// Configure margins and export the QR code as image lines of pixels
qrCode.SetMargins(10);
qrCode.SaveAsPng("qr_code.png");
using IronBarCode;
// Barcode generation: create a Code 128 barcode for product scanning
// Comment: BarcodeWriter handles all supported encoding formats
var barcode = BarcodeWriter.CreateBarcode(
    "PROD-2025-XMR",              // Value to encode in the barcode
    BarcodeWriterEncoding.Code128, // Barcode format for the scanner
    400, 150                       // Width and height in pixels
);
// Customize the barcode: add visible text below the barcode image
// Comment: this text helps users verify the encoded value
barcode.AddBarcodeValueTextBelowBarcode();
barcode.SaveAsPng("product_barcode.png");
// Generate a QR code that users can scan with any mobile device camera
// Comment: QR codes are ideal for URLs, contact data, and Wi-Fi config
var qrCode = BarcodeWriter.CreateBarcode(
    "https://ironsoftware.com",
    BarcodeWriterEncoding.QRCode,
    300, 300
);
// Configure margins and export the QR code as image lines of pixels
qrCode.SetMargins(10);
qrCode.SaveAsPng("qr_code.png");
$vbLabelText   $csharpLabel

輸出條碼影像

使用現代 C# 掃描庫建立和讀取 Xamarin 條碼影像:圖片 1 - 產生的條碼

上面的程式碼示範如何使用 IronBarcode 的條碼產生功能,只需幾行程式碼即可完成。程式碼範例中的每個註解都闡明了每個步驟的目的。它接受要編碼的資料、來自 BarcodeWriterEncoding 枚舉的條碼格式以及輸出尺寸。 IronBarcode 的 SDK 支援多種編碼類型,包括 EAN-13、Code 39、QR 碼、Data Matrix、PDF417 和 Aztec。 建立 GeneratedBarcode 實例後,您可以對其進行廣泛的自訂:使用 ResizeTo() 調整尺寸,使用 SetMargins() 配置內邊距,使用 AddAnnotationTextAboveBarcode() 新增可見標籤。 您可以將產生的條碼匯出為 PNG、JPEG、GIF、TIFF 或 PDF 格式,而無需為 Android 或 iOS 編寫特定於平台的程式碼。

對於諸如向二維碼添加徽標或自訂條碼顏色等高級功能,請瀏覽條碼生成教學建立條碼範例頁面

如何讀取和解碼來自相機和檔案來源的條碼?

IronBarcode作為 Xamarin 條碼解決方案,其最大價值在於能夠讀取裝置相機拍攝的影像或從使用者照片庫中選擇的影像中的條碼。 掃描過程採用一致的方法,可以處理從清晰的數位條碼到具有挑戰性的現實世界相機拍攝的所有影像。

using IronBarCode;
// Comment: read and decode barcodes from a camera-captured file
var results = BarcodeReader.Read("scanned_photo.png");
// Iterate through each barcode scanning result
foreach (var result in results)
{
    // Comment: access the decoded barcode value and format type
    string value = result.Value;
    string type = result.BarcodeType.ToString();
    // Display the scanned barcode data to the user on screen
    Console.WriteLine($"Type: {type}, Value: {value}");
}
using IronBarCode;
// Comment: read and decode barcodes from a camera-captured file
var results = BarcodeReader.Read("scanned_photo.png");
// Iterate through each barcode scanning result
foreach (var result in results)
{
    // Comment: access the decoded barcode value and format type
    string value = result.Value;
    string type = result.BarcodeType.ToString();
    // Display the scanned barcode data to the user on screen
    Console.WriteLine($"Type: {type}, Value: {value}");
}
$vbLabelText   $csharpLabel

讀取條碼數據

使用現代 C# 掃描庫建立和讀取 Xamarin 條碼圖像:圖像 2 - 從上傳的條碼讀取的列印值

這段程式碼片段將一個檔案傳遞給讀取器,並循環遍歷結果。 每個 BarcodeResult 實例都提供對條碼類型、解碼文字、二進位資料、位置座標和置信度評分的訪問,為開發人員提供生產應用程式環境所需的一切。 有關支援的條碼類型的完整列表,請參閱條碼格式支援頁面

對於條碼掃描效能至關重要的實際應用情境-例如在光線昏暗的倉庫中進行掃描、讀取行動傳送帶上損壞的條碼,或解碼單頁上的多個條碼,BarcodeReaderOptions 類別可讓您設定讀取過程的各個方面:

using IronBarCode;
// Comment: configure the barcode reader for challenging conditions
var options = new BarcodeReaderOptions
{
    // Comment: balance between scanning speed and accuracy on the device
    Speed = ReadingSpeed.Balanced,
    // Expect multiple barcodes per page or camera capture
    ExpectMultipleBarcodes = true,
    // Limit scanning to specific barcode types for faster results
    ExpectBarcodeTypes = BarcodeEncoding.QRCode
        | BarcodeEncoding.Code128
        | BarcodeEncoding.DataMatrix,
    // Auto-rotate barcodes captured at any camera angle
    AutoRotate = true,
    // Apply image filters to improve scanning in poor conditions
    ImageFilters = new ImageFilterCollection
    {
        new SharpenFilter(),       // Sharpen blurry camera captures
        new ContrastFilter(1.5f)   // Boost contrast for low light
    }
};
// Comment: read barcodes from a sample image using configured options
var results = BarcodeReader.Read("warehouse_scan.jpg", options);
// Iterate and display results for the user
foreach (var barcode in results)
{
    // Output each decoded barcode value from the scanner
    Console.WriteLine($"Found: {barcode.Value}");
}
using IronBarCode;
// Comment: configure the barcode reader for challenging conditions
var options = new BarcodeReaderOptions
{
    // Comment: balance between scanning speed and accuracy on the device
    Speed = ReadingSpeed.Balanced,
    // Expect multiple barcodes per page or camera capture
    ExpectMultipleBarcodes = true,
    // Limit scanning to specific barcode types for faster results
    ExpectBarcodeTypes = BarcodeEncoding.QRCode
        | BarcodeEncoding.Code128
        | BarcodeEncoding.DataMatrix,
    // Auto-rotate barcodes captured at any camera angle
    AutoRotate = true,
    // Apply image filters to improve scanning in poor conditions
    ImageFilters = new ImageFilterCollection
    {
        new SharpenFilter(),       // Sharpen blurry camera captures
        new ContrastFilter(1.5f)   // Boost contrast for low light
    }
};
// Comment: read barcodes from a sample image using configured options
var results = BarcodeReader.Read("warehouse_scan.jpg", options);
// Iterate and display results for the user
foreach (var barcode in results)
{
    // Output each decoded barcode value from the scanner
    Console.WriteLine($"Found: {barcode.Value}");
}
$vbLabelText   $csharpLabel

Speed 屬性控制速度和準確性之間的權衡,對於大批量掃描,請使用 ReadingSpeed.Faster;對於難以檢測的條碼,請使用 ReadingSpeed.Detailed。 ExpectBarcodeTypes 配置限制了掃描器 SDK 檢查的格式,從而避免了誤報錯誤結果並提高了效能。 自動旋轉功能可自動校正裝置相機以奇怪角度拍攝的條碼,而銳利化濾鏡和對比度濾鏡可改善在昏暗條件下掃描條碼的效果。 開發人員還可以透過 CropArea 屬性使用縮放式裁剪,將掃描器配置為專注於擷取頁面的特定區域。

有關所有讀取器配置的完整步驟,請參閱從圖像中讀取條碼的指南BarcodeReaderOptions API 參考。 需要解碼 PDF 文件中條碼的開發人員也應該查看PDF 條碼讀取方法

行動條碼解決方案的最佳應用場景有哪些?

Xamarin 條碼和二維碼掃描解決方案支援各行各業的數十種應用場景。 以下是IronBarcode在行動裝置上提供條碼掃描器和生成器並發揮其價值的最常見場景:

*庫存和倉庫管理*:Android 和 iOS 裝置使用者掃描產品和貨架上的條碼,即時追蹤庫存,減少人工輸入錯誤率,提高速度。 快速、可靠的條碼掃描功能對任何倉庫管理應用都大有裨益。 零售銷售點:結帳時掃描二維碼可實現快速產品查找、優惠券兌換和行動支付處理,所有這些都會在應用程式螢幕上對用戶可見。 掃描功能可處理一維和二維格式。 物流和運輸:透過 Android 和 iOS 裝置掃描包裹上的條碼,以驗證路線和確認交付。 IronBarcode即使在掃描頁面視圖中,也能解碼有摺痕或部分遮擋的標籤上的條碼。 活動簽到**:一款 Xamarin 應用,可解碼會議入口處的二維碼,提供即時與會者驗證——無需默認的紙質門票或手動帳戶查找。 該應用程式讀取每個代碼只需不到一秒的時間。 *醫療保健:行動裝置掃描病人腕帶、藥品標籤和實驗室樣本上的條碼,以防止出現錯誤情況,並確保使用者進行準確治療。 應用程式中的每個閱讀頁面都可以根據特定的條碼類型進行自訂。

IronBarcode為所有這些 Xamarin 條碼用例提供支持,它是一個跨平台的綜合條碼掃描器,包括.NET Standard、 .NET Core和.NET MAUI。 對於在生產環境中維護 Xamarin 條碼解決方案的開發人員, IronBarcode提供持續支援、定期更新和明確的許可選項。 當從 Xamarin 遷移到.NET MAUI列入計劃時, IronBarcode可以無縫過渡,無需重寫程式碼。 請參閱.NET MAUI條碼掃描器教學課程,以取得逐步範例。

微軟發布的Xamarin 專案遷移指南提供了遷移到最新跨平台框架的詳細步驟。 您還可以在 Stack Overflow 上的這篇文章中找到社區解決方案的有用鏈接,這篇文章涵蓋了常見的 Xamarin 條碼問題;此外,Reddit 上的 Xamarin 開發者社區也提供了關於跨平台移動條碼讀取方法的更多帖子討論。

如何立即開始使用 Xamarin 條碼掃描?

IronBarcode可讓 Xamarin 條碼產生和條碼掃描快速、可靠且對開發人員友好。 它支援 30 多種條碼格式,具有自動旋轉和可自訂預處理等高級功能,以及流暢的 C# 接口,使程式碼行清晰易讀。 無論解決方案是建立一個簡單的二維碼範例應用程式、處理 PDF 文件中的條碼,還是為 Android 應用程式或 iOS 應用程式建立完整的條碼閱讀器, IronBarcode都能在一個文件齊全的庫中處理所有這些。 API 中的每個函數都經過精心設計,旨在確保在生產應用程式環境中清晰易用。

在您的 Xamarin 專案中測試IronBarcode ,註冊免費試用版。 準備好投入生產後,請探索許可選項,點擊連結訪問定價頁面以了解詳情,起價為 749 美元。如需售後支持,開發人員可以透過許可附加元件頁面自訂其服務範圍。

立即開始在您的項目中使用 IronBarcode 並免費試用。

第一步:
green arrow pointer

常見問題解答

IronBarcode如何支持Xamarin應用開發?

IronBarcode使Xamarin應用開發者能夠僅用幾行C#代碼創建、閱讀和解碼條碼。它支持Android、iOS和Windows平台,確保移動應用中的條碼功能流暢無阻。

IronBarcode可以用於條碼掃描和生成嗎?

是的,IronBarcode同時作為條碼掃描儀和生成器運行,使開發者能輕鬆地在其Xamarin應用中創建和閱讀條碼。

IronBarcode支持哪些條碼類型?

IronBarcode支持多種條碼類型,包括Code 128、Data Matrix和QR碼等等,滿足不同應用需求。

IronBarcode與.NET MAUI相容嗎?

是的,IronBarcode旨在順利過渡至.NET MAUI,確保從Xamarin遷移時提供持續支持和功能。

IronBarcode為現有的Xamarin應用重要嗎?

隨著微軟終止對Xamarin的官方支持,IronBarcode提供了一個可靠的解決方案,維護在不同行業(如零售和物流)中現有Xamarin應用的條碼功能。

IronBarcode如何幫助物流和庫存管理?

IronBarcode通過有效生成和解碼條碼來協助物流和庫存管理,簡化了如跟蹤和庫存控制等流程。

IronBarcode支持哪些平台?

IronBarcode支持Android、iOS和Windows平台,使得它成為跨平台條碼應用的一個靈活工具。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担產品测测试,產品開發和研究的责任時,Jordi 為持续的產品改進增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me