codebude / QRCoder

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

PayloadGenerator give the same result #480

Closed seghier closed 5 months ago

seghier commented 10 months ago

Hello What is the purpose of PayloadGenerator? I don't see any difference in the result.

The two codes give exactly the same image

Url generator = new Url("https://github.com/codebude/QRCoder/");
string payload = generator.ToString();

QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
var qrCodeAsBitmap = qrCode.GetGraphic(20);
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("https://github.com/codebude/QRCoder/", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
var qrCodeAsBitmap = qrCode.GetGraphic(20);
Shane32 commented 5 months ago

PayloadGenerator.Url will prefix the provided string with http:// if the url does not already include it. That is the only difference. As you provided a url that includes https://, there is no difference.

See the source code:


        public class Url : Payload
        {
            private readonly string url;

            public Url(string url)
            {
                this.url = url;
            }

            public override string ToString()
            {
                return (!this.url.StartsWith("http") ? "http://" + this.url : this.url);
            }
        }
codebude commented 5 months ago

As @Shane32 explained it - the url generator just ensures that the url has a valid protocol prefix. Not more not less. Compared to other payload generators the Url generator really doesn't do much, but for sake of completeness I implemented it anyway.