跳至頁尾內容
使用IRONBARCODE

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

Xamarin條碼解決方案對於維護零售、物流和庫存管理的Android和iOS應用程式的行動開發者仍然至關重要。 即使Microsoft於2024年5月結束了對Xamarin的官方支援,數以百萬計的Xamarin應用程式仍在生產中,這些應用程式仍然需要可靠地建立、掃描和解碼條碼。 IronBarcode是一個.NET條碼SDK,只需幾行C#程式碼即可處理條碼生成和條碼讀取。 它作為條碼掃描器和生成器在Android、iOS和Windows平台上運行,並在專案遷移時直接轉換為.NET MAUI。

開始IronBarcode的免費試用,今天就將Xamarin條碼讀取新增到您的專案中。

Xamarin Apps中的條碼掃描如何運作?

Xamarin應用程式中的條碼掃描通過從相機畫面或裝置上的已保存文件中捕獲條碼資訊,然後將這些資料傳送到解碼編碼資訊的掃描庫來運作。 歷史上,許多Xamarin開發者使用了開源的ZXing庫(Zebra Crossing),建立了一個新的ZXingScannerPage實例,將其推入導航堆棧,並處理OnScanResult事件以處理條碼。 當這種方法用於Xamarin.Forms中的基本QR碼掃描用例時,ZXing多年間未收到實質性的錯誤修復,並且開發者在低光下或解碼Data Matrix等複雜格式時經常報告錯誤條件。

IronBarcode是一個現代條碼掃描SDK,採用不同的方法。 IronBarcode不提供具有實時相機介面的專用掃描頁面,而是運行在您的Xamarin應用程式可以提供的任何來源上,相機捕獲、使用者上傳的文件、螢幕截圖或PDF文件。 這使其成為適用於Xamarin條碼專案的靈活掃描庫,開發者需要對使用者介面和掃描過程擁有全權控制。 BarcodeReader類支持超過30種條碼和QR碼格式,SDK包含內建的預處理過濾器,在低光條件下或條碼損壞時提高條碼掃描性能。

將IronBarcode NuGet包安裝到您的Xamarin解決方案中即可開始:

Install-Package BarCode

BarCode包可在NuGet上獲取,並支持.NET Standard 2.0,使其與Xamarin.Forms、Xamarin原生專案和Microsoft的MAUI框架相相容。 核心SDK功能不需要額外的平台特定NuGet包或配置步驟,安裝包並立即在專案中測試。

如何在Xamarin應用中建立條碼和QR碼?

使用IronBarcode的BarcodeWriter類生成顯示在裝置螢幕上的條碼、標記或資料交換非常簡單。 以下範例是一個常見的Xamarin條碼應用範例,需要同時生成1D和2D條碼。

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");
Imports IronBarCode

' Barcode generation: create a Code 128 barcode for product scanning
' Comment: BarcodeWriter handles all supported encoding formats
Dim 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
Dim 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編寫平台特定程式碼。

有關新增徽標到QR碼或自定義條碼顏色等高級功能,請參閱條碼生成教程建立條碼程式碼範例頁面

如何從相機和文件源讀取和解碼條碼?

從裝置相機拍攝的圖像或從使用者的照片庫中選擇的圖像中讀取條碼,是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}");
}
Imports IronBarCode

' Comment: read and decode barcodes from a camera-captured file
Dim results = BarcodeReader.Read("scanned_photo.png")

' Iterate through each barcode scanning result
For Each result In results
    ' Comment: access the decoded barcode value and format type
    Dim value As String = result.Value
    Dim type As String = result.BarcodeType.ToString()
    ' Display the scanned barcode data to the user on screen
    Console.WriteLine($"Type: {type}, Value: {value}")
Next
$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}");
}
Imports IronBarCode

' Comment: configure the barcode reader for challenging conditions
Dim options As New BarcodeReaderOptions With {
    ' 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 Or BarcodeEncoding.Code128 Or BarcodeEncoding.DataMatrix,
    ' Auto-rotate barcodes captured at any camera angle
    .AutoRotate = True,
    ' Apply image filters to improve scanning in poor conditions
    .ImageFilters = New ImageFilterCollection From {
        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
Dim results = BarcodeReader.Read("warehouse_scan.jpg", options)

' Iterate and display results for the user
For Each barcode In results
    ' Output each decoded barcode value from the scanner
    Console.WriteLine($"Found: {barcode.Value}")
Next
$vbLabelText   $csharpLabel

Speed屬性控制速度與準確性的權衡,使用ReadingSpeed.Faster進行高容量掃描或使用ReadingSpeed.Detailed以最大化檢測困難條碼。 ExpectBarcodeTypes配置限制掃描器SDK檢查的格式,避免了誤報錯誤結果並提高性能。 AutoRotate功能自動校正由裝置相機捕捉到的奇怪角度的條碼,而SharpenFilter和ContrastFilter在昏暗條件下改進掃描條碼。 開發者還可以通過CropArea屬性使用縮放樣式裁剪來配置掃描器專注於捕獲頁面的特定區域。

有關所有讀者配置的完整指南,請參見從圖像讀取條碼的指南BarcodeReaderOptions API參考。 需要從PDF文件解碼條碼的開發者也應查看PDF條碼讀取方法

行動條碼解決方案的最佳用例是什麼?

Xamarin條碼和QR碼掃描解決方案支持跨行業的數十種用例。 以下是IronBarcode在行動裝置上提供條碼掃描器和生成器的最常見場景:

  • 庫存和倉庫管理:Android和iOS裝置使用者掃描產品和貨架上的條碼,以實時跟踪庫存,減少手動輸入錯誤率並提高速度。 任何倉庫應用程式都受益於快速、可靠的條碼掃描。
  • 零售POS:在結帳時掃描QR碼可加速產品查詢、優惠券兌換和行動支付處理,所有這些都可在應用程式螢幕上可見。 掃描器功能處理1D和2D格式。
  • 物流和運輸:掃描包裹上的條碼以進行路線驗證和跨Fleet的Android和iOS裝置的交貨確認。 IronBarcode即使在折疊或部分遮蔽的標籤上也可以解碼條碼,直接顯示在掃描頁面視圖。
  • 活動入場:在會議大門解碼QR碼的 Xamarin 應用程式提供即時參會者驗證 —— 不需要預設紙質票或手動帳戶查詢。 應用程式在不到一秒內讀取每個程式碼。
  • 醫療保健:在患者腕帶、藥物標籤和實驗室樣本上的條碼由行動裝置掃描,以防止錯誤狀況並確保使用者的准確治療。 應用程式中的每個讀取頁面都可以針對特定條碼型別量身定制。

IronBarcode作為一個全方位的條碼掃描器,支持所有這些Xamarin條碼用例,包括.NET Standard、.NET Core和.NET MAUI。 對於在生產中維護Xamarin條碼解決方案的開發者,IronBarcode提供持續的支援、定期更新和明確的授權選項。 當從Xamarin遷移到.NET MAUI在路線圖上時,IronBarcode可以無縫轉換,不需要程式碼重寫。 查看.NET MAUI條碼掃描教程以獲取逐步範例。

Microsoft的Xamarin專案遷移指南提供了過渡到最新跨平台框架的詳細步驟。 您還可以在這篇Stack Overflow帖子中找到社區解決方案的有用連結,涵蓋常見Xamarin條碼問題,並且這個連結到Reddit上的Xamarin開發者社區中提供了有關跨平台行動條碼讀取方法的更多討論。

如何從今天開始使用Xamarin條碼掃描?

IronBarcode使得Xamarin條碼生成和條碼掃描快速、可靠且對開發者友好。 它支持超過30種條碼格式、進階功能如自動旋轉和可自定義的預處理,以及流暢的C#接口,使程式碼行既清晰又易讀。 無論該解決方案是要求構建一個簡單的QR碼樣本應用程式、從PDF文件處理條碼,還是為Android應用程式或iOS應用程式構建完整的條碼讀取器,IronBarcode在一個完整記錄的庫中都能處理所有這些。 API中的每個功能設計旨在為生產應用環境提供清晰且易於使用的功能。

在您的Xamarin專案中測試IronBarcode,註冊免費試用。 當您為生產做好準備時,探索授權選項,連結到價格頁面以獲取詳細資訊,起價為$999。 對於購買後的支援,開發者可以通過授權附加頁自定義其覆蓋範圍。

現在開始使用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平台,使其成為跨平台條碼應用的多功能工具。

Curtis Chau
技術作家

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

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

Iron 支援團隊

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