如何在 Blazor 中构建二维码扫描器
Blazor Server 是一个 .NET Web 框架,可以在服务器上运行 C# 并通过 SignalR 连接将 UI 更新推送到浏览器。 IronQR 可直接集成到 Blazor Server 中,支持通过浏览器上传的图片进行服务器端二维码扫描,且无需任何 JavaScript 代码。
在本操作指南中,我们将构建一个 Blazor Server 页面,该页面支持图片上传,并使用 IronQR 解码其中的任何嵌入式二维码。
如何在 Blazor 中扫描二维码
- 安装 IronQR C# 库以扫描网页上的二维码
- 添加一个
InputFile组件,用于接收来自浏览器的图片上传 - 将上传的文件流式传输到服务器上的临时路径
- 加载图片并将其封装在
QrImageInput中 - 调用
Read方法并在 Blazor 组件中显示解码后的值
先决条件
- 已安装 ASP.NET 和 Web 开发工作负载的 Visual Studio 2022
- 针对 .NET 8 或更高版本的 Blazor Server 项目
安装 IronQR
请通过 Visual Studio 中的 NuGet 包管理器控制台安装 IronQR 库。 导航到 Tools > NuGet Package Manager > Package Manager Console 并运行:
或者,在 NuGet 上搜索 IronQR 并安装最新版本。
Blazor 组件布局
扫描器 UI 使用 Blazor 内置的 InputFile 组件进行浏览器本地文件选择,包含一个触发扫描的按钮和一个显示解码结果的段落。 无需 JavaScript 互操作。
添加一个新的 Razor 组件 Scanner.razor,并使用以下标记:
@page "/scanner"
@using IronQr
@using IronSoftware.Drawing
<h3>QR Code Scanner</h3>
<InputFile OnChange="OnFileSelected" accept="image/*" />
<br /><br />
<button @onclick="ScanQRCode" disabled="@(qrImageSrc == null)">Scan QR Code</button>
<p>Result: @scannedText</p>
示例输入
请使用下方的二维码作为测试图片。 将其保存到设备中,通过应用内的文件选择器上传,然后点击“扫描二维码”。 解码值应显示为 https://ironsoftware.com。

示例 QR 码 - 编码 https://ironsoftware.com
使用 IronQR 扫描二维码
当选择一个文件时,OnFileSelected 将浏览器上传流传输到服务器上的临时文件。 点击扫描按钮时,ScanQRCode 将文件加载到 AnyBitmap,传递给 QrReader.Read(),并将第一个解码值写入 scannedText,Blazor 会自动在组件中渲染它。
将以下 @code 块添加到 Scanner.razor:
using IronQr;
using IronSoftware.Drawing;
private string? qrImageSrc;
private string scannedText = string.Empty;
private async Task OnFileSelected(InputFileChangeEventArgs e)
{
var file = e.File;
var tempPath = Path.Combine(Path.GetTempPath(), file.Name);
await using var stream = file.OpenReadStream(maxAllowedSize: 10_000_000);
await using var fs = File.Create(tempPath);
await stream.CopyToAsync(fs);
qrImageSrc = tempPath;
}
private async Task ScanQRCode()
{
// Load the scanned QR code
var inputBmp = AnyBitmap.FromFile(qrImageSrc!);
var imageInput = new QrImageInput(inputBmp);
var reader = new QrReader();
// Read the scanned QR code
var results = reader.Read(imageInput);
// Check if a result was found
var firstResult = results.FirstOrDefault();
if (firstResult != null)
{
// Save the QR code value as a string
scannedText = firstResult.Value;
}
else
{
scannedText = "No QR code found in the selected image.";
}
}Imports IronQr
Imports IronSoftware.Drawing
Private qrImageSrc As String
Private scannedText As String = String.Empty
Private Async Function OnFileSelected(e As InputFileChangeEventArgs) As Task
Dim file = e.File
Dim tempPath = Path.Combine(Path.GetTempPath(), file.Name)
Await Using stream = file.OpenReadStream(maxAllowedSize:=10000000)
Await Using fs = File.Create(tempPath)
Await stream.CopyToAsync(fs)
End Using
End Using
qrImageSrc = tempPath
End Function
Private Async Function ScanQRCode() As Task
' Load the scanned QR code
Dim inputBmp = AnyBitmap.FromFile(qrImageSrc)
Dim imageInput = New QrImageInput(inputBmp)
Dim reader = New QrReader()
' Read the scanned QR code
Dim results = reader.Read(imageInput)
' Check if a result was found
Dim firstResult = results.FirstOrDefault()
If firstResult IsNot Nothing Then
' Save the QR code value as a string
scannedText = firstResult.Value
Else
scannedText = "No QR code found in the selected image."
End If
End FunctionImports IronQr
Imports IronSoftware.Drawing
Private qrImageSrc As String
Private scannedText As String = String.Empty
Private Async Function OnFileSelected(e As InputFileChangeEventArgs) As Task
Dim file = e.File
Dim tempPath = Path.Combine(Path.GetTempPath(), file.Name)
Await Using stream = file.OpenReadStream(maxAllowedSize:=10000000)
Await Using fs = File.Create(tempPath)
Await stream.CopyToAsync(fs)
End Using
End Using
qrImageSrc = tempPath
End Function
Private Async Function ScanQRCode() As Task
' Load the scanned QR code
Dim inputBmp = AnyBitmap.FromFile(qrImageSrc)
Dim imageInput = New QrImageInput(inputBmp)
Dim reader = New QrReader()
' Read the scanned QR code
Dim results = reader.Read(imageInput)
' Check if a result was found
Dim firstResult = results.FirstOrDefault()
If firstResult IsNot Nothing Then
' Save the QR code value as a string
scannedText = firstResult.Value
Else
scannedText = "No QR code found in the selected image."
End If
End FunctionInputFile.OpenReadStream 将上传的字节直接流传输到服务器端临时文件。AnyBitmap.FromFile 解码图像格式,QrReader.Read 返回一个 IEnumerable<QrResult>,每个找到的 QR 码都有一个条目。 FirstOrDefault 可以安全地处理没有 QR 码的图像而不会抛出异常。
输出
选择二维码图片并点击“扫描二维码”后,解码后的值将显示在控件下方的结果段落中。

在 Blazor 组件中渲染的解码 QR 码值
下载项目
结论
IronQR 可无缝集成到 Blazor Server 应用程序中,无需使用 JavaScript。 InputFile 提供浏览器本地文件选择,而 QrReader.Read 完全在服务器上处理解码。 通过迭代完整的结果集而不是调用 FirstOrDefault,相同的模式可以扩展到每张图像扫描多个 QR 码。
有关读取 QR 码和可用扫描模式的更多信息,请参阅 从图像读取 QR 码 和 使用扫描模式读取 QR 码 指南。
常见问题
IronQR库是什么?
IronQR是一个库,用于将QR码扫描功能集成到应用中,例如一个Blazor Server应用,通过处理从浏览器上传的图像进行服务器端的QR码解码。
如何将IronQR集成到Blazor Server应用中?
要将IronQR集成到Blazor Server应用中,您需要通过NuGet安装IronQR库,使用InputFile组件处理图像上传,并使用QrReader.Read()进行服务器端QR码解码。
使用Blazor构建QR码扫描器需要哪些先决条件?
您需要安装具有ASP.NET和Web开发工作负载的Visual Studio 2022,以及一个目标为.NET 8或更高版本的Blazor Server项目,以构建QR码扫描器。
在Blazor中可以不使用JavaScript使用IronQR吗?
可以,IronQR允许在Blazor Server应用中进行QR码扫描,而无需JavaScript,因为所有解码操作都是在服务器端处理的。
IronQR在Blazor中如何处理图像上传?
IronQR通过使用InputFile组件来流式传输文件到服务器端的临时路径,以便进行解码来处理图像上传。
如何在Blazor中显示QR码结果?
使用IronQR在Blazor中,QR码解码后的结果可通过将从QrReader.Read()获取的结果文本设定为一个在UI中呈现的属性,显示在Blazor组件的UI中。
IronQR可以解码单个图像中的多个QR码吗?
可以,IronQR可以通过迭代结果集合而不是使用FirstOrDefault()来解码单个图像中的多个QR码。
在哪里可以下载完整的Blazor QR Scanner项目?
您可以从Iron Software网站指南中提供的链接下载完整的Blazor QR Scanner项目。
QrReader.Read方法在解码QR码时返回什么?
QrReader.Read方法返回一个IEnumerable
IronQR库在NuGet上可用吗?
可以,IronQR库在NuGet上可用,您可以通过Visual Studio中的NuGet Package Manager Console安装它。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。