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);
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}");
}
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));
C#
输出
控制台显示产品标签的检测到的格式、解码值、解析的 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);
}
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.