QRCoder icon indicating copy to clipboard operation
QRCoder copied to clipboard

DataTooLongException: Generating QR Code for URL Payload with fixed version

Open gabrielifeliz opened this issue 1 year ago • 8 comments

Type of issue

[x ] Bug
[ ] Question (e.g. about handling/usage)
[ ] Request for new feature/improvement

Expected Behavior

I'm trying to generate a version 8 QR Code to share an URL, and I thought it would simply generate it with the specified parameters

Current Behavior

However, it throws the following exception: QRCoder.Exceptions.DataTooLongException: 'The given payload exceeds the maximum size of the QR code standard. The maximum size allowed for the chosen parameters (ECC level=M, EncodingMode=Byte, FixedVersion=8) is 152 bytes.'

Possible Solution (optional)

I noticed that the encoding mode selected for the text is Byte even though the text is Alphanumeric. I took a look at the code and found out that this is the method used to assign the EncodingMode, and the highlighted . According to qrcode.com, the maximum byte size for a version 8 QR Code with an alphanumeric encoding and an ECC-level M is 221, which would fit my URL, an alphanumeric text. image

Steps to Reproduce (for bugs)

image image image

QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(new PayloadGenerator.Url("https://www.walmart.com/search?q=oatmeal&max_price=7&catId=976759&facet=flavor%3AUnflavored%7C%7Ccustomer_rating%3A4+-+5+Stars%7C%7Ccontainer_type%3ABag%7C%7Cdietary_need%3AOrganic%7C%7Cretailer_type%3AWalmart")
    .ToString(), QRCodeGenerator.ECCLevel.M, requestedVersion: 8);
AsciiQRCode qrCode = new AsciiQRCode(qrCodeData);
string qrCodeAsAsciiArt = qrCode.GetGraphicSmall(drawQuietZones: false);

Your Environment

  • Version used: QRCoder.1.60
  • Compiled from source or NuGet package?: NuGet package
  • Payload/Encoded text: new PayloadGenerator.Url("https://www.walmart.com/search?q=oatmeal&max_price=7&catId=976759&facet=flavor%3AUnflavored%7C%7Ccustomer_rating%3A4+-+5+Stars%7C%7Ccontainer_type%3ABag%7C%7Cdietary_need%3AOrganic%7C%7Cretailer_type%3AWalmart")
  • Used payload generator: Url
  • Used ECC-level: M
  • Used renderer class: AsciiQrCode
  • Environment (.NET 3.5, .NET 4.X, .NETCore, ...): .NET 8

gabrielifeliz avatar Nov 13 '24 11:11 gabrielifeliz

Alphanumeric only contains uppercase characters. Convert your url completely to uppercase and it will select the alphanumeric encoding mode automatically.

Shane32 avatar Nov 13 '24 13:11 Shane32

Alphanumeric only contains uppercase characters. Convert your url completely to uppercase and it will select the alphanumeric encoding mode automatically.

I understand. What should I do in the case that a query parameter in my URL is case sensitive and requires I send lowercase characters as well to get the results? I tried converting the URL completely to uppercase but visiting the webpage no longer worked.

gabrielifeliz avatar Nov 13 '24 14:11 gabrielifeliz

Any lowercase characters will require the byte character mode. I think QR codes can switch modes mid-stream but I don’t think the QRCoder library contains logic to do so. (?) The easy answer of course is to use a larger QR code or generate shortened urls somehow (e.g. private or third party server). Note that most servers have case-sensitive paths, so switching to uppercase would only work if it is known that the server is case insensitive. Often this is the case with Windows-based servers.

Shane32 avatar Nov 13 '24 15:11 Shane32

Depending on your requirements maybe some parameters can be eliminated.

The following is an Amazon example of two urls which point to the same location:

https://www.amazon.com/DEWALT-DWST17806-TSTAK-Deep-Box/dp/B00CBY2EIO/ref=mp_s_a_1_3?crid=28F7T8LUWN9FL&dib=eyJ2IjoiMSJ9.gyMaH3ynQrp3V2plH6aplXgrWdeE6qfdJaaiHydy2W7UI3naE13H6mDFUfDwqEkHOBFNmSqZy03SYVlRsbscZ866iF1YF69w9SE9Y9761ztajCQoQBRHEx3ZV_iLXeswgsAwVZ_hPmQQRmxzHnql2enZrcN0SGrcPgBqwB3Rn1GMfhM0rgv5YouEx10zAtilccCvaHSw1T1zBU1__qtyfA.ni80X4w-k4hgXQUrZTJaBEhRF_gIvUfUqlq-jUimw1A&dib_tag=se&keywords=tool+box&qid=1731510697&sprefix=tool+box%2Caps%2C90&sr=8-3

https://www.amazon.com/dp/B00CBY2EIO

Shane32 avatar Nov 13 '24 15:11 Shane32

Thank you for the suggestions. I ran some tests, including converting the mandatory lowercase letters to its hexadecimal value, and the link still worked, leaving only uppercase letters. However, it continues to choose the byte encoding mode because of the following characters: ?, &, and =. I just wanted to know if shortening the URL or choosing a larger QR version are still the only options for me at the moment. Are those characters not considered alphanumeric? I appreciate your assistance.

gabrielifeliz avatar Nov 15 '24 19:11 gabrielifeliz

please see QR Code - Information capacity which describes the exact characters available for each encoding mode

Shane32 avatar Nov 15 '24 21:11 Shane32

Encoding modes can be mixed as needed within a QR symbol. (e.g., a url with a long string of alphanumeric characters )

But QRCoder doesn't contain code to mix encoding modes in an attempt to shorten the encoded code; it just picks the mode that all characters will fit into and uses it.

Shane32 avatar Nov 15 '24 21:11 Shane32

I ** think ** you can encode the payload yourself and use QRCoder to apply checksums and encode it into a graphic. But I'm not sure; it might be a private method. Probably rather complicated anyway.

Shane32 avatar Nov 15 '24 21:11 Shane32

Planning for mixed mode support in v2 - tracking via:

  • #585

Shane32 avatar Oct 02 '25 01:10 Shane32