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
Imports System.IO
Imports Microsoft.AspNetCore.Components.Forms
Imports System.Threading.Tasks
Private qrImageSrc As String = Nothing
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 Function
Install-Package IronQR
Blazor QR 코드 스캐너
Blazor 서버 애플리케이션에서 QR 코드를 스캔하기 위해 IronQR을 사용하세요. Blazor의 InputFile 컴포넌트를 사용하여 브라우저를 통해 이미지를 업로드한 다음, QrReader.Read()를 사용하여 서버 측에서 디코딩합니다.
Blazor에서 QR 코드를 스캔하는 5단계 가이드
using IronQr;
using IronSoftware.Drawing;
await using var stream = file.OpenReadStream(maxAllowedSize: 10_000_000);
var inputBmp = AnyBitmap.FromFile(qrImageSrc!);
var results = reader.Read(imageInput);
코드 설명
InputFile.OnChange은 사용자가 파일을 선택하면 실행됩니다. OpenReadStream은 브라우저 업로드 데이터를 임시 서버 경로로 스트리밍하며, 이 데이터는 AnyBitmap.FromFile으로 전달되어 이미지 형식을 디코딩합니다. QrImageInput는 IronQR용 비트맵을 감싸며, QrReader.Read는 IEnumerable<QrResult>를 반환합니다. FirstOrDefault은 QR 코드가 포함되지 않은 이미지에 대해서는 예외를 발생시키지 않고 첫 번째 결과를 안전하게 반환합니다.