codebude / QRCoder

A pure C# Open Source QR Code implementation
MIT License
4.46k stars 1.09k forks source link

[Question] Understanding ECCLevel, EciMode, and QR Code Version #249

Closed indy-singh closed 3 years ago

indy-singh commented 3 years ago

Type of issue

Question (e.g. about handling/usage)

Question

I'm trying to understand the correct values for ECCLevel, EciMode, and QR Code Version. We are looking at implementing this into our platform (finance industry). So the most accurate, precise, and "correct" settings are desired.

Reading this for EECLevel seems to suggest that the most desirable setting is "H". Are you able to confirm that?

And in terms of EciMode, it looks like UTF8 is most desirable.

And in terms of qr code are they forwards/backwards compatible or it is suggested that we fix at a particular version? The only thing I've found around that is https://www.qrcode.com/en/about/version.html.

Any pointers are appreciated.

Thanks, Indy

var generator = new PayloadGenerator.OneTimePassword
{
    Secret = "LAESZWLP7QZTN7FG6XXVBOQCD5GSGEIT",
    Issuer = "Testy McTestFace",
    Label = "test@test.com",
    AuthAlgorithm = PayloadGenerator.OneTimePassword.OneTimePasswordAuthAlgorithm.SHA1,
    Type = PayloadGenerator.OneTimePassword.OneTimePasswordAuthType.TOTP,
    Period = 30,
    Digits = 6
};

var payload = generator.ToString();
var qrCodeGenerator = new QRCodeGenerator();
var qrCodeData = qrCodeGenerator.CreateQrCode(plainText: payload, eccLevel: QRCodeGenerator.ECCLevel.H, forceUtf8: true, utf8BOM: false, eciMode: QRCodeGenerator.EciMode.Utf8, requestedVersion: -1);
var graphic = new Base64QRCode(qrCodeData).GetGraphic(4);

Your Environment

codebude commented 3 years ago

I'm sorry to have not replied in time. Nevertheless let me answer the questions, so that maybe someone else with the same questions can benefit from them...

Reading this for EECLevel seems to suggest that the most desirable setting is "H". Are you able to confirm that?

That's correct. Choosing ECCLevel (=level of error correction) H means highest amount of error correction data. In theory, by using ECC level H, round about 30% of the QR code image could be destroyed and nevertheless the code would be readable.

And in terms of EciMode, it looks like UTF8 is most desirable.

If you search for the most compatible settings, the you shouldn't set the ECI mode parameter (which means QRCoder takes ECIMode.Default). ECI mode is a special mode in the QR code specification and should only be used, if it can be ensured that the reading application can handle ECI encoded QR codes. (Hint: Most QR code readers can't!) Not setting ECIMode to ECIMode.UTF8 doesn't mean, that the QR code can't contain UTF8 data. UTF-8 data can also be encoded in a QR code when choosing ECIMode.Default (or not setting the parameter). Setting ECIMode to UTF-8 just means: "Encode QR code data in UTF-8 an use special ECI mode."

And in terms of qr code are they forwards/backwards compatible or it is suggested that we fix at a particular version? The only thing I've found around that is https://www.qrcode.com/en/about/version.html.

The term "version" may be a bit misleading. A higher QR code version does not mean that this is a newer QR code standard, etc. Versions in context of QR codes mean something like "size". A QR code of version 3 can save less data/information than a QR code of version 10 for example. For your use-case you shouldn't set the version parameter, because QRCoder will select the best matching version automatically. Setting the version manually is just an option to fix QR code graphics size manually. (But if you do so, you know what you're doing and want especially this behaviour. So if you're unsure what a QR code version is, just don't set it - QRCoder will do it for you.)