使用IRONQR

如何扫描 Android QR 码(初级教程)

发布 2024年十二月16日
分享:

如今,二维码随处可见,可作为网站、折扣或重要信息的快速链接。 然而,在所有安卓设备上扫描它们的方法并不总是相同的。 根据您的手机型号,您可能可以使用内置相机应用程序,也可能需要一个单独的QR 码扫描器. 为了消除您的困惑,我们为您准备了一份详细的指南,帮助您在任何安卓智能手机或平板电脑上轻松扫描二维码。 我们还将探索一种使用IronQR 库.

在安卓系统上扫描二维码的内置方法

安卓设备通常都配备了支持二维码扫描的本地功能。 这些内置方法无需安装第三方应用程序,因此既方便又安全。 让我们来看看安卓设备用户可用的主要内置选项。

1.谷歌镜头应用程序

Google Lens 是一款功能强大的视觉分析工具,具有 QR 码扫描功能。 该应用程序通常预装在安卓设备上,可通过各种入口访问。

访问 Google Lens

用户可以根据自己的设备和偏好,通过多种方法在安卓手机上访问 Google Lens。 最常见的方法是通过 Google 应用程序。以下是访问 Google Lens 的步骤:

  1. 打开谷歌应用程序

  2. 找到搜索栏附近的 Google Lens 图标

    如何扫描 QR 码 Android(初级教程):图 1

  3. 点击图标激活 Google Lens

    对于那些喜欢使用现有图片的人来说,Google Photos 提供了另一个切入点。 翻译过程如下:

  4. 打开谷歌照片

    1. 选择包含二维码的图片

    如何扫描 QR 码 Android(初级教程):图 2

  5. 点击屏幕底部的 Google Lens 图标。 之后,您可以使用 Google Lens 功能进行二维码扫描。

    一些安卓设备,尤其是运行最新版本操作系统的设备,将 Google Lens 直接集成到了原生相机应用程序中。 这些设备的用户可以通过以下方式访问 Google Lens:

  6. 启动相机应用程序

  7. 寻找 "模式 "或 "更多 "选项

  8. 选择 Google Lens(如有

使用 Google Lens 进行 QR 码扫描

激活 Google Lens 后,扫描二维码的过程非常简单。 下面是使用方法:

  1. 将设备的摄像头对准二维码

  2. 等待 Google Lens 自动启动扫描

  3. 识别到二维码后,会弹出一个窗口,显示编码信息

  4. 点击弹出式窗口进入链接内容或执行相关操作

2.快速设置磁贴

一些安卓设备提供了用于二维码扫描的 "快速设置 "磁贴,可快速访问该功能。 设置和使用此功能:

  1. 从屏幕顶部向下滑动,访问快速设置面板。

    如何扫描 QR 码 Android(初级教程):图 3

  2. 向下轻扫一下即可展开整个面板。

    如何扫描 QR 码 Android(初级教程):图 4

  3. 找到编辑功能,通常用铅笔图标或三个点图标表示。

    如何扫描 QR 码 Android(初级教程):图 5

  4. 在编辑模式下,搜索标有 "QR 码 "或 "QR 扫描仪 "的磁贴。

    如何扫描 QR 码 Android(初级教程):图 6

  1. 将此磁贴拖入活动的快速设置区域。

  2. 保存更改以确认新布局。

    使用快速设置二维码扫描仪:

  3. 打开快速设置面板。

  4. 点击 QR 码扫描器磁贴。

  5. 该设备将启动一个 QR 码扫描界面,可以使用相机应用程序或 Google Lens。

  6. 将摄像头对准 QR 代码启动扫描。

    这些内置方法为安卓用户提供了扫描 QR 码的有效方法,无需额外安装软件。 用户可以快速访问 QR 代码中编码的信息,同时维护其设备的安全性和完整性。 除此之外,您还可以在 Google Play 商店下载第三方应用程序,用于在安卓手机上扫描二维码。

介绍 IronQR for .NET 库

虽然我们讨论的内置方法为最终用户提供了出色的 QR 代码扫描功能,但开发人员通常需要更强大的工具来在其应用程序中创建和操作 QR 代码。 这就是 IronQR for .NET 库发挥作用的地方。 它为 Android 应用程序开发中的 QR 码功能提供了强大的解决方案。

IronQR 概述

如何扫描 QR 码 Android(初级教程):图 7

IronQR是一个全面的 .NET 库,设计用于生成.NET 代码、阅读二维码翻译《QR 码》,并操作 QR 码。 虽然主要针对 .NET 开发人员,但也可以通过 Xamarin 或 .NET MAUI 等框架用于 Android 开发。 它是具有 QR 功能的跨平台应用程序的完美选择。

主要特点和优势

IronQR 提供了许多有关 QR 代码功能的特性:

  1. QR 代码生成:开发人员可以通过编程创建 QR 代码,控制各种参数,如大小、纠错级别和内容。

  2. 先进的扫描功能:该库为读取 QR 码提供了强大的算法,即使在恶劣条件下或从图像中读取 QR 码也不例外。

  3. 定制选项:IronQR 允许创建自定义 QR 代码,包括添加徽标或自定义颜色,同时保持可扫描性。

  4. 支持各种数据类型:该库可将不同类型的数据(包括 URL、联系信息和自定义文本)编码到 QR 代码中。

  5. 错误处理和纠正:IronQR 采用先进的纠错技术,确保生成的 QR 代码可靠且易于扫描。

与安卓开发整合

对于在 .NET 环境中工作的 Android 开发人员来说,IronQR 可以集成到项目中,提供服务器端 QR 代码生成或增强 Android 应用程序的功能。 这种集成允许进行更复杂的二维码操作,而标准的 Android 库可能无法实现这些操作。

开发人员可以创建具有复杂 QR 代码功能的 Android 应用程序,例如批量生成 QR 代码、高级解码功能或符合品牌要求的自定义 QR 代码设计。 IronQR for .NET 库对于希望将其 Android 应用程序的 QR 码功能扩展到标准内置功能之外的开发人员来说是一个强大的工具。

代码示例

下面是使用 IronQR for .NET 的 .NET MAUI QR Code Scanner App 演示。 在项目中安装 IronQR 库后,更新 MauiPage.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="QR_Code_Business_Card.MainPage"
             Title="QR Code Reader">
    <Grid Padding="20" RowDefinitions="Auto,*" ColumnDefinitions="*,Auto,*">
        <!-- Left side -->
        <VerticalStackLayout Grid.Row="1" Grid.Column="0" Spacing="20">
            <Button x:Name="ScanQRButton" 
                    Text="Scan QR Code"
                    Clicked="OnScanQRButtonClicked"
                    HorizontalOptions="Fill"
                    BackgroundColor="#6200EE"
                    TextColor="White"
                    FontAttributes="Bold"
                    CornerRadius="10" />
            <Frame BorderColor="#E0E0E0" Padding="0" CornerRadius="10" HasShadow="True">
                <Grid>
                    <Image x:Name="SelectedImage"
                           HeightRequest="300"
                           WidthRequest="300"
                           Aspect="AspectFit" />
                    <ActivityIndicator x:Name="LoadingIndicator"
                                       IsRunning="False"
                                       IsVisible="False"
                                       Color="#6200EE"
                                       VerticalOptions="Center"
                                       HorizontalOptions="Center" />
                </Grid>
            </Frame>
        </VerticalStackLayout>
        <!-- Divider -->
        <BoxView Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" 
                 WidthRequest="1" 
                 Color="#E0E0E0" 
                 Margin="10,0" />
        <!-- Right side -->
        <VerticalStackLayout Grid.Row="1" Grid.Column="2" Spacing="20">
            <Frame BorderColor="#E0E0E0" Padding="10" CornerRadius="10" HasShadow="True">
                <Editor x:Name="ResultEditor"
                        HeightRequest="200"
                        IsReadOnly="True"
                        Placeholder="QR code content will appear here"
                        FontSize="16" />
            </Frame>
            <Button x:Name="CopyTextButton" 
                    Text="Copy Text"
                    Clicked="OnCopyTextButtonClicked"
                    HorizontalOptions="Fill"
                    BackgroundColor="#6200EE"
                    TextColor="White"
                    FontAttributes="Bold"
                    CornerRadius="10" />
        </VerticalStackLayout>
    </Grid>
</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="QR_Code_Business_Card.MainPage"
             Title="QR Code Reader">
    <Grid Padding="20" RowDefinitions="Auto,*" ColumnDefinitions="*,Auto,*">
        <!-- Left side -->
        <VerticalStackLayout Grid.Row="1" Grid.Column="0" Spacing="20">
            <Button x:Name="ScanQRButton" 
                    Text="Scan QR Code"
                    Clicked="OnScanQRButtonClicked"
                    HorizontalOptions="Fill"
                    BackgroundColor="#6200EE"
                    TextColor="White"
                    FontAttributes="Bold"
                    CornerRadius="10" />
            <Frame BorderColor="#E0E0E0" Padding="0" CornerRadius="10" HasShadow="True">
                <Grid>
                    <Image x:Name="SelectedImage"
                           HeightRequest="300"
                           WidthRequest="300"
                           Aspect="AspectFit" />
                    <ActivityIndicator x:Name="LoadingIndicator"
                                       IsRunning="False"
                                       IsVisible="False"
                                       Color="#6200EE"
                                       VerticalOptions="Center"
                                       HorizontalOptions="Center" />
                </Grid>
            </Frame>
        </VerticalStackLayout>
        <!-- Divider -->
        <BoxView Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" 
                 WidthRequest="1" 
                 Color="#E0E0E0" 
                 Margin="10,0" />
        <!-- Right side -->
        <VerticalStackLayout Grid.Row="1" Grid.Column="2" Spacing="20">
            <Frame BorderColor="#E0E0E0" Padding="10" CornerRadius="10" HasShadow="True">
                <Editor x:Name="ResultEditor"
                        HeightRequest="200"
                        IsReadOnly="True"
                        Placeholder="QR code content will appear here"
                        FontSize="16" />
            </Frame>
            <Button x:Name="CopyTextButton" 
                    Text="Copy Text"
                    Clicked="OnCopyTextButtonClicked"
                    HorizontalOptions="Fill"
                    BackgroundColor="#6200EE"
                    TextColor="White"
                    FontAttributes="Bold"
                    CornerRadius="10" />
        </VerticalStackLayout>
    </Grid>
</ContentPage>
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'<?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="QR_Code_Business_Card.MainPage" Title="QR Code Reader"> <Grid Padding="20" RowDefinitions="Auto,*" ColumnDefinitions="*,Auto,*"> <!-- Left side -- > <VerticalStackLayout Grid.Row="1" Grid.Column="0" Spacing="20"> <Button x:Name="ScanQRButton" Text="Scan QR Code" Clicked="OnScanQRButtonClicked" HorizontalOptions="Fill" BackgroundColor="#6200EE" TextColor="White" FontAttributes="Bold" CornerRadius="10" /> <Frame BorderColor="#E0E0E0" Padding="0" CornerRadius="10" HasShadow="True"> <Grid> <Image x:Name="SelectedImage" HeightRequest="300" WidthRequest="300" Aspect="AspectFit" /> <ActivityIndicator x:Name="LoadingIndicator" IsRunning="False" IsVisible="False" Color="#6200EE" VerticalOptions="Center" HorizontalOptions="Center" /> </Grid> </Frame> </VerticalStackLayout> <!-- Divider -- > <BoxView Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" WidthRequest="1" Color="#E0E0E0" Margin="10,0" /> <!-- Right side -- > <VerticalStackLayout Grid.Row="1" Grid.Column="2" Spacing="20"> <Frame BorderColor="#E0E0E0" Padding="10" CornerRadius="10" HasShadow="True"> <Editor x:Name="ResultEditor" HeightRequest="200" IsReadOnly="True" Placeholder="QR code content will appear here" FontSize="16" /> </Frame> <Button x:Name="CopyTextButton" Text="Copy Text" Clicked="OnCopyTextButtonClicked" HorizontalOptions="Fill" BackgroundColor="#6200EE" TextColor="White" FontAttributes="Bold" CornerRadius="10" /> </VerticalStackLayout> </Grid> </ContentPage>
VB   C#

然后更新 Maui.Page.xaml.cs 代码:

using IronQr;
using IronSoftware.Drawing;
namespace QR_Code_Business_Card
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            License.LicenseKey = "License-Key";
        }
        private async void OnScanQRButtonClicked(object sender, EventArgs e)
        {
            try
            {
                var result = await FilePicker.PickAsync(new PickOptions
                {
                    FileTypes = FilePickerFileType.Images,
                    PickerTitle = "Pick an image with a QR code"
                });
                if (result != null)
                {
                    string filePath = result.FullPath;
                    SelectedImage.Source = ImageSource.FromFile(filePath);
                    // Show loading indicator
                    LoadingIndicator.IsVisible = true;
                    LoadingIndicator.IsRunning = true;
                    ResultEditor.Text = "Processing QR code...";
                    // Process QR code in the background
                    await Task.Run(() =>
                    {
                        var inputBmp = AnyBitmap.FromFile(filePath);
                        QrImageInput imageInput = new QrImageInput(inputBmp);
                        QrReader reader = new QrReader();
                        IEnumerable<QrResult> results = reader.Read(imageInput);
                        MainThread.BeginInvokeOnMainThread(() =>
                        {
                            if (results.Any())
                            {
                                string scannedData = results.First().Value.ToString();
                                ResultEditor.Text = scannedData;
                            }
                            else
                            {
                                ResultEditor.Text = "No QR code found in the image.";
                            }
                            // Hide loading indicator
                            LoadingIndicator.IsVisible = false;
                            LoadingIndicator.IsRunning = false;
                        });
                    });
                }
            }
            catch (Exception ex)
            {
                ResultEditor.Text = $"An error occurred: {ex.Message}";
                LoadingIndicator.IsVisible = false;
                LoadingIndicator.IsRunning = false;
            }
        }
        private async void OnCopyTextButtonClicked(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(ResultEditor.Text))
            {
                await Clipboard.SetTextAsync(ResultEditor.Text);
                await DisplayAlert("Success", "Text copied to clipboard", "OK");
            }
            else
            {
                await DisplayAlert("Error", "No text to copy", "OK");
            }
        }
    }
}
using IronQr;
using IronSoftware.Drawing;
namespace QR_Code_Business_Card
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            License.LicenseKey = "License-Key";
        }
        private async void OnScanQRButtonClicked(object sender, EventArgs e)
        {
            try
            {
                var result = await FilePicker.PickAsync(new PickOptions
                {
                    FileTypes = FilePickerFileType.Images,
                    PickerTitle = "Pick an image with a QR code"
                });
                if (result != null)
                {
                    string filePath = result.FullPath;
                    SelectedImage.Source = ImageSource.FromFile(filePath);
                    // Show loading indicator
                    LoadingIndicator.IsVisible = true;
                    LoadingIndicator.IsRunning = true;
                    ResultEditor.Text = "Processing QR code...";
                    // Process QR code in the background
                    await Task.Run(() =>
                    {
                        var inputBmp = AnyBitmap.FromFile(filePath);
                        QrImageInput imageInput = new QrImageInput(inputBmp);
                        QrReader reader = new QrReader();
                        IEnumerable<QrResult> results = reader.Read(imageInput);
                        MainThread.BeginInvokeOnMainThread(() =>
                        {
                            if (results.Any())
                            {
                                string scannedData = results.First().Value.ToString();
                                ResultEditor.Text = scannedData;
                            }
                            else
                            {
                                ResultEditor.Text = "No QR code found in the image.";
                            }
                            // Hide loading indicator
                            LoadingIndicator.IsVisible = false;
                            LoadingIndicator.IsRunning = false;
                        });
                    });
                }
            }
            catch (Exception ex)
            {
                ResultEditor.Text = $"An error occurred: {ex.Message}";
                LoadingIndicator.IsVisible = false;
                LoadingIndicator.IsRunning = false;
            }
        }
        private async void OnCopyTextButtonClicked(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(ResultEditor.Text))
            {
                await Clipboard.SetTextAsync(ResultEditor.Text);
                await DisplayAlert("Success", "Text copied to clipboard", "OK");
            }
            else
            {
                await DisplayAlert("Error", "No text to copy", "OK");
            }
        }
    }
}
Imports IronQr
Imports IronSoftware.Drawing
Namespace QR_Code_Business_Card
	Partial Public Class MainPage
		Inherits ContentPage

		Public Sub New()
			InitializeComponent()
			License.LicenseKey = "License-Key"
		End Sub
		Private Async Sub OnScanQRButtonClicked(ByVal sender As Object, ByVal e As EventArgs)
			Try
				Dim result = Await FilePicker.PickAsync(New PickOptions With {
					.FileTypes = FilePickerFileType.Images,
					.PickerTitle = "Pick an image with a QR code"
				})
				If result IsNot Nothing Then
					Dim filePath As String = result.FullPath
					SelectedImage.Source = ImageSource.FromFile(filePath)
					' Show loading indicator
					LoadingIndicator.IsVisible = True
					LoadingIndicator.IsRunning = True
					ResultEditor.Text = "Processing QR code..."
					' Process QR code in the background
					Await Task.Run(Sub()
						Dim inputBmp = AnyBitmap.FromFile(filePath)
						Dim imageInput As New QrImageInput(inputBmp)
						Dim reader As New QrReader()
						Dim results As IEnumerable(Of QrResult) = reader.Read(imageInput)
						MainThread.BeginInvokeOnMainThread(Sub()
							If results.Any() Then
								Dim scannedData As String = results.First().Value.ToString()
								ResultEditor.Text = scannedData
							Else
								ResultEditor.Text = "No QR code found in the image."
							End If
							' Hide loading indicator
							LoadingIndicator.IsVisible = False
							LoadingIndicator.IsRunning = False
						End Sub)
					End Sub)
				End If
			Catch ex As Exception
				ResultEditor.Text = $"An error occurred: {ex.Message}"
				LoadingIndicator.IsVisible = False
				LoadingIndicator.IsRunning = False
			End Try
		End Sub
		Private Async Sub OnCopyTextButtonClicked(ByVal sender As Object, ByVal e As EventArgs)
			If Not String.IsNullOrWhiteSpace(ResultEditor.Text) Then
				Await Clipboard.SetTextAsync(ResultEditor.Text)
				Await DisplayAlert("Success", "Text copied to clipboard", "OK")
			Else
				Await DisplayAlert("Error", "No text to copy", "OK")
			End If
		End Sub
	End Class
End Namespace
VB   C#

然后运行应用程序,您将看到以下用户界面。 选择 QR 代码后,它将扫描 QR 代码并在文本框中显示结果文本,您可以使用复制按钮进行复制。

如何扫描 QR 码 Android(初级教程):图 8

结论

如何扫描 QR 码 Android(初级教程):图 9

借助 Google Lens 和快速设置磁贴等内置功能,在安卓设备上扫描二维码变得非常容易。 这些本地方法可让用户快速、安全地访问二维码信息,而无需使用其他应用程序。

对于旨在将高级 QR 代码功能纳入其应用程序的开发人员来说,IronQR for .NET 库提供了一个强大的解决方案。 它可以生成自定义 QR 代码,具有高级扫描功能和广泛的自定义选项。 IronQR 提供了一个免费试用供开发人员探索其功能,许可证起价为 $749,这使得在 Android 应用程序中进行专业级别的二维码集成成为值得投资的选择。

< 前一页
Zelle QR代码(.NET开发人员如何使用)
下一步 >
名片二维码生成器(初级指南)