在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
随着移动应用程序利用QR码实现信息快速检索的兴起,对高效且易于集成的QR码扫描器以及用于扫描条形码的.NET MAUI条形码扫描器的需求不断增长。.NET MAUI(多平台App UI),微软的跨平台框架,为构建跨iOS、Android、macOS和Windows的应用程序提供了统一的环境。 在.NET MAUI应用程序中扫描QR码时,开发人员需要一个直观而强大的库来管理该过程。
IronQR 是一个受欢迎的库,允许开发人员快速、准确且可靠地生成和解码二维码。 本文将指导您如何将IronQR与.NET MAUI集成,以构建一个可以跨多个平台无缝工作的QR/条形码扫描仪。
创建 .NET MAUI 项目。
安装IronQR NuGet包。
设置相机和文件存储权限。
IronQR 是一个功能强大且易于使用的库,可简化在 .NET 应用程序中生成和解码 QR 码,包括用于 QR 和条形码扫描功能的 .NET MAUI 移动应用程序。 它为跨平台集成二维码和条形码扫描功能提供快速可靠的解决方案,适用于iOS、Android、macOS和Windows等平台,这对于构建跨平台移动应用程序至关重要。
a) 跨平台支持:在MAUI应用程序中能够在iOS、Android、macOS和Windows平台上无缝运行。
b) QR码扫描:高效解码各种类型的二维码(网址、文本、联系信息等)。 它还支持多种条形码,通过高效的条形码检测算法读取。
c) 二维码生成:这使得可以轻松地从URL、文本等数据生成二维码。
d) 相机权限处理:自动处理相机权限请求,简化扫描过程。
e) 高性能:快速可靠的二维码扫描和生成,资源使用极少。
f) 可定制设置:提供扫描参数和二维码外观的自定义选项。
g) 易于集成:只需简单的 API 和最少的设置,即可将 QR Code 扫描和生成添加到您的 .NET MAUI 应用程序中。
h) 错误处理:提供详细的错误信息和故障排除,确保在各种场景中实现流畅的功能。
i) 无外部依赖:IronQR独立运行,与ZXing条形码扫描器不同,减少了对第三方库或复杂配置的需求。
j) 多格式支持:支持多种QR码格式,确保与实际应用中使用的各种QR码的兼容性。
在进行实施之前,请确保您具备以下先决条件:
安装 Visual Studio 2022 或更高版本。
.NET 6.0 SDK或更高版本(因为.NET MAUI是基于.NET 6及更高版本构建的)。
IronQR NuGet 包用于二维码扫描和条形码检测。
首先,让我们创建一个简单的.NET MAUI项目:
打开 Visual Studio,然后点击创建一个新项目。
选择 .NET MAUI App 模板。
为项目命名(例如,MauiQRCodeScanner),选择一个位置,然后点击下一步。
\
选择所需的 .NET 版本并点击Create。
IronQR 是一个第三方库,提供二维码生成和扫描功能。 要安装IronQR,需要通过NuGet添加它:
在 Visual Studio 中,右键单击解决方案资源管理器中的Dependencies。
点击管理 NuGet 包。
在浏览选项卡中,搜索IronQR,并在相关包(通常是IronQR或IronQR.Maui,如果专为MAUI提供)上点击安装。
接受任何许可证并确保库安装成功。
为了让您的应用程序扫描二维码,您需要在移动平台(iOS 和 Android)上请求相机权限。 以下是如何添加以下权限的方法。
Android 设备:在 AndroidManifest.xml 文件中,添加相机权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
iOS:在 Info.plist 文件中,添加相机使用说明:
<key>NSCameraUsageDescription</key>
<string>我们需要访问您的相机来扫描二维码。</string>
现在,让我们在 MAUI 条码扫描器应用中为我们的 QR 扫描器创建一个简单的用户界面。我们将使用按钮来触发扫描过程,并使用标签来显示扫描的 QR 码文本。
在 XML 命名空间中编辑 MainPage.xaml 文件。
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MauiQRCodeScanner.MainPage">
<StackLayout Padding="20" VerticalOptions="Center">
<Button x:Name="scanButton" Text="Scan QR Code" Clicked="OnScanButtonClicked" />
<Label x:Name="resultLabel" Text="Scan Result will appear here" HorizontalOptions="Center" VerticalOptions="CenterAndExpand"/>
</StackLayout>
</ContentPage>
现在,在MainPage.xaml.cs中,您将处理相机权限和二维码扫描逻辑。 以下是实现方法:
using IronQrCode;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Essentials;
namespace MauiQRCodeScanner;
public partial class MainPage : ContentPage
{
public MainPage()
{
License.LicenseKey = "Your key";
InitializeComponent();
}
// OnScanButtonClicked method with object sender as input
private async void OnScanButtonClicked(object sender, EventArgs e)
{
// Check for camera permission for var barcode with clear and valid reason
var status = await Permissions.RequestAsync<Permissions.Camera>();
if (status != PermissionStatus.Granted)
{
await DisplayAlert("Permission Denied", "Cannot scan QR codes without camera permission", "OK");
return;
}
// Start scanning QR codes
try
{
var images = await FilePicker.Default.PickAsync(new PickOptions
{
PickerTitle = "Pick image",
FileTypes = FilePickerFileType.Images
});
var imageSource = images.FullPath.ToString();
// barcodeImage.Source = imageSource;
var inputBmp = AnyBitmap.FromFile(imageSource);
// Load the asset into QrImageInput
QrImageInput imageInput = new QrImageInput(inputBmp);
// Create a QR Reader object
QrReader reader = new QrReader();
// Read the input and get all embedded QR Codes
IEnumerable<QrResult> results = reader.Read(imageInput);
if (results.Any())
{
resultLabel.Text = "Scanned Text: " + results.First().Value;
// Display the result
}
else
{
resultLabel.Text = "No QR code detected";
}
}
catch (Exception ex)
{
resultLabel.Text = "Error: " + ex.Message;
}
}
}
using IronQrCode;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Essentials;
namespace MauiQRCodeScanner;
public partial class MainPage : ContentPage
{
public MainPage()
{
License.LicenseKey = "Your key";
InitializeComponent();
}
// OnScanButtonClicked method with object sender as input
private async void OnScanButtonClicked(object sender, EventArgs e)
{
// Check for camera permission for var barcode with clear and valid reason
var status = await Permissions.RequestAsync<Permissions.Camera>();
if (status != PermissionStatus.Granted)
{
await DisplayAlert("Permission Denied", "Cannot scan QR codes without camera permission", "OK");
return;
}
// Start scanning QR codes
try
{
var images = await FilePicker.Default.PickAsync(new PickOptions
{
PickerTitle = "Pick image",
FileTypes = FilePickerFileType.Images
});
var imageSource = images.FullPath.ToString();
// barcodeImage.Source = imageSource;
var inputBmp = AnyBitmap.FromFile(imageSource);
// Load the asset into QrImageInput
QrImageInput imageInput = new QrImageInput(inputBmp);
// Create a QR Reader object
QrReader reader = new QrReader();
// Read the input and get all embedded QR Codes
IEnumerable<QrResult> results = reader.Read(imageInput);
if (results.Any())
{
resultLabel.Text = "Scanned Text: " + results.First().Value;
// Display the result
}
else
{
resultLabel.Text = "No QR code detected";
}
}
catch (Exception ex)
{
resultLabel.Text = "Error: " + ex.Message;
}
}
}
Imports IronQrCode
Imports Microsoft.Maui.Controls
Imports Microsoft.Maui.Essentials
Namespace MauiQRCodeScanner
Partial Public Class MainPage
Inherits ContentPage
Public Sub New()
License.LicenseKey = "Your key"
InitializeComponent()
End Sub
' OnScanButtonClicked method with object sender as input
Private Async Sub OnScanButtonClicked(ByVal sender As Object, ByVal e As EventArgs)
' Check for camera permission for var barcode with clear and valid reason
Dim status = Await Permissions.RequestAsync(Of Permissions.Camera)()
If status <> PermissionStatus.Granted Then
Await DisplayAlert("Permission Denied", "Cannot scan QR codes without camera permission", "OK")
Return
End If
' Start scanning QR codes
Try
Dim images = Await FilePicker.Default.PickAsync(New PickOptions With {
.PickerTitle = "Pick image",
.FileTypes = FilePickerFileType.Images
})
Dim imageSource = images.FullPath.ToString()
' barcodeImage.Source = imageSource;
Dim inputBmp = AnyBitmap.FromFile(imageSource)
' Load the asset into QrImageInput
Dim imageInput As New QrImageInput(inputBmp)
' Create a QR Reader object
Dim reader As New QrReader()
' Read the input and get all embedded QR Codes
Dim results As IEnumerable(Of QrResult) = reader.Read(imageInput)
If results.Any() Then
resultLabel.Text = "Scanned Text: " & results.First().Value
' Display the result
Else
resultLabel.Text = "No QR code detected"
End If
Catch ex As Exception
resultLabel.Text = "Error: " & ex.Message
End Try
End Sub
End Class
End Namespace
选择所需的二维码或从摄像头取景中捕获。
结果在用户界面上显示如下。
权限:我们使用 Permissions.RequestAsync
IronQR Scanner:IronQrCode.Scanner() 类用于扫描二维码。 ScanAsync() 方法触发扫描,结果存储在 scanResult 中。 扫描的 QR 代码文本显示在 resultLabel 中。
测试二维码扫描器:现在,您可以测试二维码扫描器了。 当应用程序运行时,单击“扫描二维码”按钮将启动扫描过程。 如果摄像头前有有效的QR Code,它将被解码并显示在屏幕上。
IronQR在移动应用程序代码中使用许可证密钥。 开发者可以在许可页面轻松获取试用许可证。 在使用IronQR库之前,如下所示在代码中放置许可证。
License.LicenseKey = "Your License";
License.LicenseKey = "Your License";
License.LicenseKey = "Your License"
在本文中,我们介绍了使用IronQR在.NET MAUI应用程序中构建QR码扫描器的过程。 我们首先设置了一个.NET MAUI应用程序,安装了IronQR包,并实现了用户界面和扫描逻辑。 IronQR 使在 .NET MAUI 应用中进行二维码扫描变得极其简单和高效。
IronQR库旨在实现跨平台,确保使用.NET MAUI构建的应用程序在所有目标设备上(无论是智能手机、平板电脑还是桌面系统)都可以一致地访问QR码功能。 IronQR还支持自动相机权限处理等功能,使得集成二维码扫描更加容易,无需手动管理权限的麻烦。
简而言之,IronQR for .NET MAUI 使开发人员能够在其移动应用中快速实现二维码扫描和生成功能,简化开发过程并改善所有平台上的用户体验。