Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataTooLongException: Generating QR Code for URL Payload with fixed version #583

Open
gabrielifeliz opened this issue Nov 13, 2024 · 8 comments

Comments

@gabrielifeliz
Copy link

gabrielifeliz commented Nov 13, 2024

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

@Shane32
Copy link
Contributor

Shane32 commented Nov 13, 2024

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

@gabrielifeliz
Copy link
Author

gabrielifeliz commented Nov 13, 2024

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.

@Shane32
Copy link
Contributor

Shane32 commented Nov 13, 2024

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.

@gabrielifeliz
Copy link
Author

gabrielifeliz commented Nov 15, 2024

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.

@Shane32
Copy link
Contributor

Shane32 commented Nov 15, 2024

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

@Shane32
Copy link
Contributor

Shane32 commented Nov 15, 2024

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
Copy link
Contributor

Shane32 commented Nov 15, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants