QRCodeDecoder Class

Reads the data of QR codes, Micro QR codes and rMQR codes from their modules: the format information, the error correction, and the segments of the data.

public static class QRCodeDecoder

Remarks

The decoder works on modules, not on pixels. To read the QR codes of an image, use the ReadQRCodes methods of the Meziantou.Framework.Imaging.Barcodes package, which locate the symbols and sample their modules.

Methods

NameDescription
Decode(QRCode)Reads the data of a QR code.
Decode(bool[,])Reads the data of a QR code from its modules.
MergeStructuredAppend(IEnumerable<QRCodeDecodeResult>)Puts the data of a structured append sequence back together.
TryDecode(bool[,], QRCodeDecodeResult?)Tries to read the data of a QR code from its modules.

Decode overloads

Decode(QRCode)

QRCodeDecoder.cs

Reads the data of a QR code.

public static QRCodeDecodeResult Decode(QRCode qrCode)
Parameters
qrCode QRCode
The QR code.
Returns

QRCodeDecodeResult The content of the QR code.

Exceptions
ArgumentNullException
qrCode is null.
Example
var qrCode = QRCode.Create("https://www.meziantou.net", ErrorCorrectionLevel.M);

var result = QRCodeDecoder.Decode(qrCode);

Assert.Equal("https://www.meziantou.net", result.Text);
Assert.Equal((QRCodeType.Standard, 2, ErrorCorrectionLevel.M), (result.Type, result.Version, result.ErrorCorrectionLevel));

Decode(bool[,])

QRCodeDecoder.cs

Reads the data of a QR code from its modules.

public static QRCodeDecodeResult Decode(bool[,] modules)
Parameters
modules bool[,]
The modules, true for dark, indexed by row then column, without quiet zone. The size of the matrix gives the type of the symbol: a square of 21 to 177 modules is a standard QR code, a square of 11 to 17 modules a Micro QR code, and a rectangle an rMQR code.
Returns

QRCodeDecodeResult The content of the QR code.

Exceptions
ArgumentNullException
modules is null.
FormatException
The modules are not a QR code, or have more errors than the error correction can fix.

MergeStructuredAppend(IEnumerable<QRCodeDecodeResult>)

QRCodeDecoder.cs

Puts the data of a structured append sequence back together.

public static string MergeStructuredAppend(IEnumerable<QRCodeDecodeResult> results)

Parameters

results IEnumerable<QRCodeDecodeResult>
The decoded symbols of the sequence, in any order.

Returns

string The text of the sequence.

Exceptions

ArgumentNullException
results is null or contains null.
ArgumentException
A symbol is not part of a structured append sequence, the symbols belong to different sequences, a symbol is missing or repeated, or the parity of the data does not match.

Example

var text = "Structured append splits this text across several QR codes, and the reader puts it back together.";
var symbols = QRCode.CreateStructuredAppend(text, ErrorCorrectionLevel.M, new QRCodeOptions { MaximumVersion = 2 });

// The symbols can be read in any order
var results = symbols.Reverse().Select(QRCodeDecoder.Decode).ToList();

Assert.Equal(text, QRCodeDecoder.MergeStructuredAppend(results));

TryDecode(bool[,], QRCodeDecodeResult?)

QRCodeDecoder.cs

Tries to read the data of a QR code from its modules.

public static bool TryDecode(bool[,] modules, out QRCodeDecodeResult? result)

Parameters

modules bool[,]
The modules, true for dark, indexed by row then column, without quiet zone.
result QRCodeDecodeResult?
When this method returns true, the content of the QR code.

Returns

bool true if the modules are a QR code that could be read; otherwise, false.

Exceptions

ArgumentNullException
modules is null.