跳過到頁腳內容
使用IRONBARCODE

如何使用IronBarcode創建MAUI條碼掃描器

現代行動應用程式越來越依賴條碼掃描來進行庫存管理、銷售點系統和產品追蹤。 建立 MAUI 條碼掃描器,可以將條碼偵測直接整合到 .NET MAUI 應用程式中,結合攝影機畫面和影像檔案處理來偵測二維碼、資料矩陣和其他條碼格式。 雖然許多條碼庫專注於相機預覽,但 IronBarcode 即使在具有挑戰性的條件下也能準確讀取和掃描條碼,表現出色。

在本指南中,我將向您展示如何使用 IronBarcode 在 .NET MAUI 專案中實作條碼掃描功能。 到最後,您將能夠掃描單個影像檔案中的多個條碼或從裝置的攝影機掃描條碼,讀取任何給定條碼的數據,並自信地在您自己的 MAUI 專案中使用 IronBarcode。

建構MAUI條碼掃描器需要哪些先決條件?

在開始之前,請確保您已安裝 Visual Studio Code 或 Visual Studio 2022 以及 .NET MAUI 工作負載,並具備 C# 的基本知識。 您還需要 .NET 8 SDK 以獲得最佳效能和對跨平台應用程式的支援。

如何設定 MAUI 條碼掃描項目?

在 Visual Studio 2022 中建立一個新的 .NET MAUI 應用程式專案。將其命名為"BarcodeScannerApp",並選擇 .NET 8 作為目標框架。

透過 NuGet 套件管理器控制台安裝 IronBarcode:

Install-Package BarCode

這將安裝一個 .NET 程式庫,支援多種條碼格式,包括二維碼、Code 128 和 Data Matrix。 IronBarcode 可離線運作,並可處理原生不支援的反向條碼,從而確保連續掃描和更好的掃描一致性。

要啟動 IronBarcode,請取得免費試用許可證並將其新增至您的程式碼:

License.LicenseKey = "YOUR-LICENSE-KEY";
License.LicenseKey = "YOUR-LICENSE-KEY";
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

如何設定安卓和iOS系統的相機權限?

平台特定的相機權限對於條碼掃描功能至關重要。 每個平台都需要在其清單檔案中進行特定配置。

對於 Android 系統,請編輯 Platforms/Android/AndroidManifest.xml 檔案:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

這些權限允許存取相機並聲明使用相機硬件,確保您的應用程式可以在 Android 裝置上擷取條碼影像。

對於 iOS 系統,請修改 Platforms/iOS/Info.plist 檔案:

<key>NSCameraUsageDescription</key>
<string>This app requires camera access to scan barcodes</string>
<key>NSCameraUsageDescription</key>
<string>This app requires camera access to scan barcodes</string>
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

此配置提供了 iOS 在向使用者要求相機權限時顯示的必要隱私描述。

如何建立條碼掃描器介面?

在 MainPage.xaml 中設計一個簡潔易用的介面:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="BarcodeScannerApp.MainPage">
    <VerticalStackLayout Padding="20" Spacing="20">
        <Label Text="Barcode Scanner" 
               FontSize="24" 
               HorizontalOptions="Center" />
        <Image x:Name="CapturedImage" 
               HeightRequest="300"
               Aspect="AspectFit" />
        <Label x:Name="ResultLabel" 
               Text="Tap button to scan"
               HorizontalOptions="Center" />
        <Button Text="Scan Barcode" 
                Clicked="OnScanClicked"
                BackgroundColor="#007ACC"
                TextColor="White" />
    </VerticalStackLayout>
</ContentPage>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="BarcodeScannerApp.MainPage">
    <VerticalStackLayout Padding="20" Spacing="20">
        <Label Text="Barcode Scanner" 
               FontSize="24" 
               HorizontalOptions="Center" />
        <Image x:Name="CapturedImage" 
               HeightRequest="300"
               Aspect="AspectFit" />
        <Label x:Name="ResultLabel" 
               Text="Tap button to scan"
               HorizontalOptions="Center" />
        <Button Text="Scan Barcode" 
                Clicked="OnScanClicked"
                BackgroundColor="#007ACC"
                TextColor="White" />
    </VerticalStackLayout>
</ContentPage>
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

此佈局創建了一個簡潔的介面,包括圖像預覽區、結果顯示標籤和掃描按鈕。 VerticalStackLayout 提供一致的間距和內邊距,讓外觀更專業。

如何實現條碼讀取器功能?

在 MainPage.xaml.cs 中使用 IronBarcode 的影像處理功能實現掃描邏輯:

using IronBarCode;
using IronSoftware.Drawing;
namespace BarcodeScannerApp;
public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        License.LicenseKey = "YOUR-LICENSE-KEY";
    }
    private async void OnScanClicked(object sender, EventArgs e)
    {
        try
        {
            // Capture photo using device camera
            var photo = await MediaPicker.Default.CapturePhotoAsync();
            if (photo == null) return;
            // Convert photo to byte array
            using var stream = await photo.OpenReadAsync();
            using var memoryStream = new MemoryStream();
            await stream.CopyToAsync(memoryStream);
            var imageBytes = memoryStream.ToArray();
            // Display captured image
            CapturedImage.Source = ImageSource.FromStream(() => 
                new MemoryStream(imageBytes));
            // Process with IronBarcode
            var bitmap = AnyBitmap.FromBytes(imageBytes);
            var results = await BarcodeReader.ReadAsync(bitmap);
            // Display results
            if (results.Any())
            {
                var barcodeValue = results.First().Value;
                ResultLabel.Text = $"Scanned: {barcodeValue}";
            }
            else
            {
                ResultLabel.Text = "No barcode detected";
            }
        }
        catch (Exception ex)
        {
            await DisplayAlert("Error", 
                $"Scanning failed: {ex.Message}", "OK");
        }
    }
}
using IronBarCode;
using IronSoftware.Drawing;
namespace BarcodeScannerApp;
public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        License.LicenseKey = "YOUR-LICENSE-KEY";
    }
    private async void OnScanClicked(object sender, EventArgs e)
    {
        try
        {
            // Capture photo using device camera
            var photo = await MediaPicker.Default.CapturePhotoAsync();
            if (photo == null) return;
            // Convert photo to byte array
            using var stream = await photo.OpenReadAsync();
            using var memoryStream = new MemoryStream();
            await stream.CopyToAsync(memoryStream);
            var imageBytes = memoryStream.ToArray();
            // Display captured image
            CapturedImage.Source = ImageSource.FromStream(() => 
                new MemoryStream(imageBytes));
            // Process with IronBarcode
            var bitmap = AnyBitmap.FromBytes(imageBytes);
            var results = await BarcodeReader.ReadAsync(bitmap);
            // Display results
            if (results.Any())
            {
                var barcodeValue = results.First().Value;
                ResultLabel.Text = $"Scanned: {barcodeValue}";
            }
            else
            {
                ResultLabel.Text = "No barcode detected";
            }
        }
        catch (Exception ex)
        {
            await DisplayAlert("Error", 
                $"Scanning failed: {ex.Message}", "OK");
        }
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

此實作透過 MediaPicker 擷取影像,將其轉換為位元組數組進行處理,並使用 IronBarcode 的 BarcodeReader.ReadAsync 方法進行檢測。 AnyBitmap.FromBytes 方法可以自動處理各種影像格式。 錯誤處理機制確保以用戶友好的訊息實現優雅的故障復原。

有了這個代碼,我們就可以掃描這個條碼了:

如何使用 IronBarcode 建立 MAUI 條碼掃描器:圖 2 - 輸入測試條碼

你應該可以在螢幕上看到條碼的數據:

如何使用 IronBarcode 建立 MAUI 條碼掃描器:圖 3 - 掃描的條碼值

IronBarcode提供哪些進階功能?

IronBarcode 提供多種進階功能,可提高掃描可靠性。 該函式庫的機器學習演算法會自動調整置信度閾值,進而提高對複雜條碼的辨識準確率。 影像校正濾鏡無需額外配置即可處理旋轉、傾斜或光線不足的條碼。

根據具體的掃描需求,自訂閱讀器選項:

var options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = true,
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128
};
var results = await BarcodeReader.ReadAsync(bitmap, opcanbvations);
var options = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = true,
    ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128
};
var results = await BarcodeReader.ReadAsync(bitmap, opcanbvations);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

此配置優化了特定條碼類型的掃描,並可在單一影像中偵測多個條碼,從而在保持準確性的同時減少處理時間。

如何使用 IronBarcode 建立 MAUI 條碼掃描器:圖 4 - 從相同影像掃描多個條碼

常見故障排除技巧

即使設定了強大的 MAUI 條碼掃描器,由於裝置設定、影像品質或平台特定限制,偶爾也會出現問題。 以下提示旨在解決在 .NET MAUI 應用程式中實現條碼掃描功能時遇到的最常見問題:

*攝影機無法開啟:*請確保在清單節點中正確設定下列權限,然後重新部署應用程式。此外,如果您在具有多個攝影機或自訂攝影機配置的裝置上進行測試,請檢查是否支援多攝影機設定。 掃描結果不佳:**調整影像解析度或光照,或對條碼進行變換。 此外,啟用連續掃描並提高 BarcodeReaderOptions 中的"速度"選項可以提高掃描一致性。 *記憶體問題:使用 using 語句正確釋放影像流。 這樣可以確保條碼掃描功能保持反應迅速,並防止在 Android 裝置或 Microsoft Windows 電腦上出現意外崩潰或運行緩慢的情況。

  • iOS 問題:確認 Info.plist 檔案包含正確的二維碼綁定屬性。 建議在 iPhone 和 iPad 裝置上進行測試,以確保 iOS 裝置上的條碼掃描功能一致。

結論

IronBarcode以其強大的影像處理引擎和機器學習功能,徹底改變了MAUI條碼掃描方式。 與即時相機預覽庫不同,IronBarcode 的方法即使在具有挑戰性的條件下也能確保可靠的掃描。 該庫的離線功能和廣泛的格式支援使其成為企業應用程式的理想選擇。 現在,您可以自信地運用今天我們教給您的知識,創建您自己的 .NET MAUI 條碼掃描器。 想了解更多? 請務必閱讀 IronBarcodes 的詳細文件

立即開始開發,免費試用版即可用於生產部署。 IronBarcode 兼具準確性、易用性和全面性的功能,是 MAUI 條碼掃描應用的最佳選擇。

常見問題解答

MAUI 條碼掃描器使用 IronBarcode 的優勢是什麼?

IronBarcode 擅於精確讀取條碼,即使在嚴苛的條件下也能掃描條碼,因此非常適合整合到 MAUI 應用程式中。

IronBarcode 可以在 MAUI 應用程式中處理多種條碼格式嗎?

是的,IronBarcode 可以檢測包括 QR 碼和 Data Matrix 在內的各種條碼格式,使其能滿足不同的應用需求。

IronBarcode 如何與 MAUI 條碼掃描器的攝像頭整合?

IronBarcode 可與攝影機訊號無縫整合,直接在 MAUI 應用程式中實現即時條碼偵測與處理。

IronBarcode 是否支持 MAUI 应用程序中的离线条码扫描?

是的,IronBarcode 支援離線條碼掃描,允許它在沒有網際網路連線的情況下運作,這對行動應用程式非常有利。

IronBarcode 用於 MAUI 條碼掃描的主要功能是什麼?

主要功能包括精確的條碼偵測、支援各種條碼格式,以及即使在具挑戰性的條件下仍能維持穩健的效能。

IronBarcode 如何處理具有挑戰性的掃描條件?

IronBarcode 的設計可在惡劣光線或傾斜角度等嚴苛條件下準確讀取條碼,確保性能可靠。

在 MAUI 中使用 IronBarcode 設定條碼掃描權限容易嗎?

是的,IronBarcode 為條碼掃描提供了明確的設定必要權限的指導,使其可以直接在 MAUI 中實施。

IronBarcode 可以用於 MAUI 的庫存管理應用嗎?

絕對的,IronBarcode 非常適合庫存管理,提供高效率的條碼掃描功能,可整合至 MAUI 應用程式。

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