在实际环境中测试
在生产中测试无水印。
随时随地为您服务。
随着移动应用程序利用二维码快速检索信息的兴起,对高效且易于集成的二维码扫描器以及一个用于扫描条形码的.NET MAUI条形码扫描器的需求正在增加。.NETMAUI(多平台应用程序用户界面),微软的跨平台框架,提供了一个统一的环境,用于在 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. 二维码扫描:高效解码各种类型的二维码(URLs,文本,联系信息等。). 它还支持多种条形码,通过高效的条形码检测算法读取。
**c. QR码生成:这使得从URL、文本等数据中轻松生成QR码成为可能。
d. 相机权限处理:自动处理相机权限请求,简化扫描过程。
e. 高性能:快速可靠的二维码扫描和生成,内存占用小。
f. 可自定义设置:提供扫描参数和 QR 码外观的自定义选项。
g. 轻松集成:只需简单的API和最低限度的设置,即可将QR Code扫描和生成添加到您的.NET MAUI应用程序中。
h. 错误处理**:提供详细的错误信息和故障排除,确保在各种场景中流畅运行。
i. 无外部依赖:IronQR独立运行,减少对第三方库或复杂配置的需求,不同于ZXing条形码扫描器。
j. 多格式支持:支持多种二维码格式,确保与真实世界中广泛使用的二维码兼容。
在进行实施之前,请确保您具备以下先决条件:
已安装Visual Studio 2022或更高版本。
.NET 6.0 SDK或更高版本(因为.NET MAUI是建立在.NET 6及更高版本之上的).
IronQR NuGet 包用于 QR 码扫描和条形码检测。
首先,让我们创建一个简单的.NET MAUI项目:
打开 Visual Studio,然后点击 创建新项目。
选择 .NET MAUI App 模板。
命名项目(例如,MauiQRCodeScanner),选择一个位置,然后点击下一步。
选择所需的 .NET 版本,然后点击创建。
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在移动应用程序代码中使用许可证密钥。 开发人员可以轻松从许可证page. 在使用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 使开发人员能够在其移动应用中快速实现二维码扫描和生成功能,简化开发过程并改善所有平台上的用户体验。