QRCodeImageReader Class

Provides methods to read the QR codes of an image: standard QR codes, Micro QR codes and rMQR codes.

public static class QRCodeImageReader

Methods

NameDescription
ReadQRCodes(Image, QRCodeReadingOptions?, CancellationToken)Reads the QR codes of an image.

ReadQRCodes(Image, QRCodeReadingOptions?, CancellationToken)

QRCodeImageReader.cs

Reads the QR codes of an image.

public static IReadOnlyList<QRCodeImageResult> ReadQRCodes(this Image image, QRCodeReadingOptions? options = null, CancellationToken cancellationToken = default)

Parameters

image Image
The image. For an animated image, its first frame is read.
options QRCodeReadingOptions?
The reading options, or null for the defaults.
cancellationToken CancellationToken
A token to cancel the reading.

Returns

IReadOnlyList<QRCodeImageResult> The QR codes found, each once; empty when there is none.

Exceptions

ArgumentNullException
image is null.
OperationCanceledException
cancellationToken was canceled.

Remarks

The reader reads generated, printed and scanned images, and photographs. It finds the finder patterns of the symbols, which can be rotated, mirrored, scaled, blurred, unevenly lit or in perspective: the corners of the finder patterns and the alignment patterns give the perspective of a symbol photographed at an angle. It samples their modules and decodes them with QRCodeDecoder. A transparent background reads as white.

A symbol needs its quiet zone and modules of at least 2 pixels. The pixels are classified as dark or light with a local threshold (from the blocks of 8x8 pixels around them, which follows shadows and gradients), then with the Otsu threshold of the image and a Sauvola threshold when no symbol is found.

Example

using var image = QRCode.Create("https://www.meziantou.net").ToImage(); // or Image.Load("photo.png")

var results = image.ReadQRCodes();

var result = Assert.Single(results);
Assert.Equal("https://www.meziantou.net", result.Text);

// The corners of the symbol in the image: top-left, top-right, bottom-right, bottom-left
var topLeft = result.Corners[0];
Assert.True(topLeft.DistanceTo(new PointD(40, 40)) < 1); // after the quiet zone of 4 modules of 10 pixels