using IronQr;using IronSoftware.Drawing;var reader = new QrReader();IEnumerable<QrResult> results = reader.Read(new QrImageInput("label.png"));if (!results.Any()){Console.WriteLine("No QR code detected or decoding failed."); return;}Console.WriteLine(results.First().Value);
using IronQr;
using IronSoftware.Drawing;
var reader = new QrReader();
IEnumerable<QrResult> results = reader.Read(new QrImageInput("label.png"));
if (!results.Any())
{
Console.WriteLine("No QR code detected or decoding failed.");
return;
}
Console.WriteLine(results.First().Value);
ImportsIronQrImportsIronSoftware.DrawingDim reader = New QrReader()Dim results AsIEnumerable(OfQrResult) = reader.Read(New QrImageInput("label.png"))IfNot results.Any() ThenConsole.WriteLine("No QR code detected or decoding failed.") ReturnEnd IfConsole.WriteLine(results.First().Value)
Imports IronQr
Imports IronSoftware.Drawing
Dim reader = New QrReader()
Dim results As IEnumerable(Of QrResult) = reader.Read(New QrImageInput("label.png"))
If Not results.Any() Then
Console.WriteLine("No QR code detected or decoding failed.")
Return
End If
Console.WriteLine(results.First().Value)
using IronQr;using IronSoftware.Drawing;var reader = new QrReader();IEnumerable<QrResult> results = reader.Read(new QrImageInput(AnyBitmap.FromFile("damaged-label.png")));// Reed-Solomon decoding is pass/fail — presence in results means valid checksumif (!results.Any()){ // Decoding failed entirely — damage exceeded the error correction capacityConsole.WriteLine("QR code could not be decoded. Consider re-scanning or using a higher error correction level at generation time."); return;}foreach (QrResult result in results){ // Decoded successfully — validate the content matches expected format if (string.IsNullOrWhiteSpace(result.Value)) {Console.WriteLine("QR decoded but produced an empty value."); continue; }Console.WriteLine($"Valid QR: {result.Value}");}
using IronQr;
using IronSoftware.Drawing;
var reader = new QrReader();
IEnumerable<QrResult> results = reader.Read(new QrImageInput(AnyBitmap.FromFile("damaged-label.png")));
// Reed-Solomon decoding is pass/fail — presence in results means valid checksum
if (!results.Any())
{
// Decoding failed entirely — damage exceeded the error correction capacity
Console.WriteLine("QR code could not be decoded. Consider re-scanning or using a higher error correction level at generation time.");
return;
}
foreach (QrResult result in results)
{
// Decoded successfully — validate the content matches expected format
if (string.IsNullOrWhiteSpace(result.Value))
{
Console.WriteLine("QR decoded but produced an empty value.");
continue;
}
Console.WriteLine($"Valid QR: {result.Value}");
}
ImportsIronQrImportsIronSoftware.DrawingDim reader = New QrReader()Dim results AsIEnumerable(OfQrResult) = reader.Read(New QrImageInput(AnyBitmap.FromFile("damaged-label.png")))' Reed-Solomon decoding is pass/fail — presence in results means valid checksumIfNot results.Any() Then ' Decoding failed entirely — damage exceeded the error correction capacityConsole.WriteLine("QR code could not be decoded. Consider re-scanning or using a higher error correction level at generation time.") ReturnEnd IfFor Each result AsQrResultIn results ' Decoded successfully — validate the content matches expected format IfString.IsNullOrWhiteSpace(result.Value) ThenConsole.WriteLine("QR decoded but produced an empty value.") Continue For End IfConsole.WriteLine($"Valid QR: {result.Value}")Next
Imports IronQr
Imports IronSoftware.Drawing
Dim reader = New QrReader()
Dim results As IEnumerable(Of QrResult) = reader.Read(New QrImageInput(AnyBitmap.FromFile("damaged-label.png")))
' Reed-Solomon decoding is pass/fail — presence in results means valid checksum
If Not results.Any() Then
' Decoding failed entirely — damage exceeded the error correction capacity
Console.WriteLine("QR code could not be decoded. Consider re-scanning or using a higher error correction level at generation time.")
Return
End If
For Each result As QrResult In results
' Decoded successfully — validate the content matches expected format
If String.IsNullOrWhiteSpace(result.Value) Then
Console.WriteLine("QR decoded but produced an empty value.")
Continue For
End If
Console.WriteLine($"Valid QR: {result.Value}")
Next
using IronQr;using IronSoftware.Drawing;using IronQr.Enum;// Read from an image file with ML + classic scan (default)var reader = new QrReader();IEnumerable<QrResult> results = reader.Read(new QrImageInput(AnyBitmap.FromFile("product-label.png")));foreach (QrResult result in results){ // Inspect the detected QR formatConsole.WriteLine($"Format: {result.QrType}"); // QRCode, MicroQRCode, or RMQRCodeConsole.WriteLine($"Value: {result.Value}"); // Url is non-null only if Value is a valid URI if (result.Url != null) {Console.WriteLine($"URI: {result.Url.AbsoluteUri}"); } // Corner coordinates for positional contextConsole.WriteLine($"Corners: {result.Points.Length} points detected");}// Read from a bitmap with ML-only mode for faster throughputvar bitmap = AnyBitmap.FromFile("camera-capture.jpg");var fastResults = reader.Read(new QrImageInput(bitmap, QrScanMode.OnlyDetectionModel));
using IronQr;
using IronSoftware.Drawing;
using IronQr.Enum;
// Read from an image file with ML + classic scan (default)
var reader = new QrReader();
IEnumerable<QrResult> results = reader.Read(new QrImageInput(AnyBitmap.FromFile("product-label.png")));
foreach (QrResult result in results)
{
// Inspect the detected QR format
Console.WriteLine($"Format: {result.QrType}"); // QRCode, MicroQRCode, or RMQRCode
Console.WriteLine($"Value: {result.Value}");
// Url is non-null only if Value is a valid URI
if (result.Url != null)
{
Console.WriteLine($"URI: {result.Url.AbsoluteUri}");
}
// Corner coordinates for positional context
Console.WriteLine($"Corners: {result.Points.Length} points detected");
}
// Read from a bitmap with ML-only mode for faster throughput
var bitmap = AnyBitmap.FromFile("camera-capture.jpg");
var fastResults = reader.Read(new QrImageInput(bitmap, QrScanMode.OnlyDetectionModel));
ImportsIronQrImportsIronSoftware.DrawingImportsIronQr.Enum' Read from an image file with ML + classic scan (default)Dim reader As New QrReader()Dim results AsIEnumerable(OfQrResult) = reader.Read(New QrImageInput(AnyBitmap.FromFile("product-label.png")))For Each result AsQrResultIn results ' Inspect the detected QR formatConsole.WriteLine($"Format: {result.QrType}") ' QRCode, MicroQRCode, or RMQRCodeConsole.WriteLine($"Value: {result.Value}") ' Url is non-null only if Value is a valid URI If result.UrlIsNot Nothing ThenConsole.WriteLine($"URI: {result.Url.AbsoluteUri}") End If ' Corner coordinates for positional contextConsole.WriteLine($"Corners: {result.Points.Length} points detected")Next' Read from a bitmap with ML-only mode for faster throughputDim bitmap = AnyBitmap.FromFile("camera-capture.jpg")Dim fastResults = reader.Read(New QrImageInput(bitmap, QrScanMode.OnlyDetectionModel))
Imports IronQr
Imports IronSoftware.Drawing
Imports IronQr.Enum
' Read from an image file with ML + classic scan (default)
Dim reader As New QrReader()
Dim results As IEnumerable(Of QrResult) = reader.Read(New QrImageInput(AnyBitmap.FromFile("product-label.png")))
For Each result As QrResult In results
' Inspect the detected QR format
Console.WriteLine($"Format: {result.QrType}") ' QRCode, MicroQRCode, or RMQRCode
Console.WriteLine($"Value: {result.Value}")
' Url is non-null only if Value is a valid URI
If result.Url IsNot Nothing Then
Console.WriteLine($"URI: {result.Url.AbsoluteUri}")
End If
' Corner coordinates for positional context
Console.WriteLine($"Corners: {result.Points.Length} points detected")
Next
' Read from a bitmap with ML-only mode for faster throughput
Dim bitmap = AnyBitmap.FromFile("camera-capture.jpg")
Dim fastResults = reader.Read(New QrImageInput(bitmap, QrScanMode.OnlyDetectionModel))
输出
控制台显示产品标签的检测到的格式、解码值、解析的 URI 和角数,然后显示相机捕获的快速扫描结果计数。
当应用程序需要特定格式时,QrType字段很有帮助。 例如,一个只生成标准二维码的仓库系统可以过滤掉意外的微型二维码或矩形微型二维码检测结果,这些结果可能表示噪声或无关标签。 每种格式都有其独特的容量特性:标准 QR 码最多支持 7,089 个数字字符,Micro QR 码最多支持 35 个字符,而矩形 Micro QR 码则采用矩形外形,适用于标签空间有限的情况。
一个完善的验证模式会在将数据传递给另一个系统之前检查三个方面:集合计数、值完整性和类型或 URI 有效性。
输入
一张没有二维码的空白白色图像,代表一批混合文档中的一页,其中一些页面没有机器可读标签。
using IronQr;using IronQr.Enum;using IronSoftware.Drawing;using System.Collections.Generic;using System.Linq;public static class QrValidator{ public static List<QrResult> GetValidResults( string imagePath, QrEncoding? expectedFormat = null) { var reader = new QrReader(); IEnumerable<QrResult> results = reader.Read(new QrImageInput(AnyBitmap.FromFile(imagePath))); // Guard: no QR codes detected if (!results.Any()) return new List<QrResult>(); return results .Where(r => !string.IsNullOrWhiteSpace(r.Value)) .Where(r => expectedFormat == null || r.QrType == expectedFormat) .ToList(); }}// Usage — only accept standard QR codes with non-empty valuesvar validated = QrValidator.GetValidResults( "shipping-manifest.png", expectedFormat: QrEncoding.QRCode);if (validated.Count == 0){Console.WriteLine("No valid QR codes found for processing."); return;}foreach (var qr in validated){ // Safe for downstream: value is non-empty, format is verifiedSendToInventoryApi(qr.Value, qr.Url?.AbsoluteUri);}
using IronQr;
using IronQr.Enum;
using IronSoftware.Drawing;
using System.Collections.Generic;
using System.Linq;
public static class QrValidator
{
public static List<QrResult> GetValidResults(
string imagePath,
QrEncoding? expectedFormat = null)
{
var reader = new QrReader();
IEnumerable<QrResult> results = reader.Read(new QrImageInput(AnyBitmap.FromFile(imagePath)));
// Guard: no QR codes detected
if (!results.Any())
return new List<QrResult>();
return results
.Where(r => !string.IsNullOrWhiteSpace(r.Value))
.Where(r => expectedFormat == null || r.QrType == expectedFormat)
.ToList();
}
}
// Usage — only accept standard QR codes with non-empty values
var validated = QrValidator.GetValidResults(
"shipping-manifest.png",
expectedFormat: QrEncoding.QRCode);
if (validated.Count == 0)
{
Console.WriteLine("No valid QR codes found for processing.");
return;
}
foreach (var qr in validated)
{
// Safe for downstream: value is non-empty, format is verified
SendToInventoryApi(qr.Value, qr.Url?.AbsoluteUri);
}
ImportsIronQrImportsIronQr.EnumImportsIronSoftware.DrawingPublicModuleQrValidator Public Function GetValidResults( imagePath AsString,Optional expectedFormat AsQrEncoding? = Nothing) AsList(OfQrResult) Dim reader = New QrReader() Dim results AsIEnumerable(OfQrResult) = reader.Read(New QrImageInput(AnyBitmap.FromFile(imagePath))) ' Guard: no QR codes detected IfNot results.Any() Then Return New List(OfQrResult)() End If Return results.Where(Function(r) NotString.IsNullOrWhiteSpace(r.Value)).Where(Function(r) expectedFormat Is NothingOrElse r.QrType = expectedFormat).ToList() End FunctionEndModule' Usage — only accept standard QR codes with non-empty valuesDim validated = QrValidator.GetValidResults( "shipping-manifest.png", expectedFormat:=QrEncoding.QRCode)If validated.Count = 0 ThenConsole.WriteLine("No valid QR codes found for processing.") ReturnEnd IfFor Each qr In validated ' Safe for downstream: value is non-empty, format is verifiedSendToInventoryApi(qr.Value, qr.Url?.AbsoluteUri)Next qr
Imports IronQr
Imports IronQr.Enum
Imports IronSoftware.Drawing
Public Module QrValidator
Public Function GetValidResults(
imagePath As String,
Optional expectedFormat As QrEncoding? = Nothing) As List(Of QrResult)
Dim reader = New QrReader()
Dim results As IEnumerable(Of QrResult) = reader.Read(New QrImageInput(AnyBitmap.FromFile(imagePath)))
' Guard: no QR codes detected
If Not results.Any() Then
Return New List(Of QrResult)()
End If
Return results.
Where(Function(r) Not String.IsNullOrWhiteSpace(r.Value)).
Where(Function(r) expectedFormat Is Nothing OrElse r.QrType = expectedFormat).
ToList()
End Function
End Module
' Usage — only accept standard QR codes with non-empty values
Dim validated = QrValidator.GetValidResults(
"shipping-manifest.png",
expectedFormat:=QrEncoding.QRCode)
If validated.Count = 0 Then
Console.WriteLine("No valid QR codes found for processing.")
Return
End If
For Each qr In validated
' Safe for downstream: value is non-empty, format is verified
SendToInventoryApi(qr.Value, qr.Url?.AbsoluteUri)
Next qr
What is the role of the `QrResult` class in IronQR?
The `QrResult` class in IronQR represents decoded results from QR code reading. It provides properties like the decoded value, detected QR type, and optionally, a URI if the value is a valid URL.
How does IronQR handle physical damage to QR codes?
IronQR uses Reed-Solomon error correction to automatically correct minor damages in QR codes during decoding, making it more resilient to physical damage and ensuring data integrity.
Can IronQR validate QR codes asynchronously?
Yes, IronQR supports asynchronous workflows. You can use `ReadAsync` to perform non-blocking QR code reading while applying similar validation patterns on the results.