跳至页脚内容
USING IRONBARCODE

How to Create a MAUI Barcode Scanner Using IronBarcode

现代移动应用程序越来越依赖条码扫描进行库存管理、销售点系统和产品跟踪。 构建一个MAUI条码扫描器可以让您将条码检测直接集成到您的.NET MAUI应用程序中,将相机馈送与图像文件处理结合起来,以检测二维码、数据矩阵和其他条码格式。 虽然许多库专注于相机预览,但IronBarcode在准确读取条码和在困难条件下扫描条码方面表现出色。

在本指南中,我将向您展示如何在.NET MAUI项目中使用IronBarcode实现条码扫描功能。 到最后,您将能够在一个图像文件或设备的相机中扫描多个条码,读取任何给定条码的数据,并自信地在您自己的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和数据矩阵。 IronBarcode离线工作并处理本地不支持的反转条码,确保持续扫描和更好的扫描一致性。

要激活IronBarcode,请获取免费试用许可证并将其添加到您的代码中:

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

如何为Android和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中包含适当的QR码绑定属性。 建议在iPhone和iPad设备上进行测试,以确保iOS设备上的条码扫描功能一致。

结论

IronBarcode通过其强大的图像处理引擎和机器学习功能改造了MAUI条码扫描。 与实时相机预览库不同,IronBarcode的方法确保了即使在不利条件下也能可靠扫描。 该库的离线功能和广泛的格式支持使其非常适合企业应用程序。 现在,您将能够自信地运用我们今天教给您的知识创建您自己的.NET MAUI条码扫描器。 想了解更多? 请务必阅读IronBarcode的详尽文档

开始开发用于生产部署的免费试用。 IronBarcode以其准确性、易于实施和全面功能的组合,使其成为MAUI条码扫描应用程序的最佳选择。

常见问题解答

使用 IronBarcode 进行 MAUI 条码扫描器的优势是什么?

IronBarcode 擅长准确读取条码,即使在恶劣条件下进行扫描,也使其成为 MAUI 应用集成的理想选择。

IronBarcode 能否在 MAUI 应用中处理多种条码格式?

是的,IronBarcode 可以检测包括二维码和数据矩阵在内的各种条码格式,使其适用于不同的应用需求。

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 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。