codebude / QRCoder

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

Improve QRCode output sharpness #404

Closed DanielHolland closed 3 months ago

DanielHolland commented 2 years ago

Hi there,

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

I am looking at using this package in place of calling Google's Chart API to generate QRCodes, the implementation I've followed uses the basic example in Wiki. What I see is that the image output of the library is noticeably blurred / pixelated when compared to the output from Google Chart.

Screenshot 2022-06-01 at 12 00 19

What can I do to improve on the image sharpness?

var url = "https://github.com/codebude/QRCoder/issues/new";
QRCodeGenerator _qrCode = new QRCodeGenerator();
QRCodeData _qrCodeData = _qrCode.CreateQrCode(url, QRCodeGenerator.ECCLevel.L);
PngByteQRCode qrCode = new PngByteQRCode(_qrCodeData);
byte[] qrAsPngByteArray = qrCode.GetGraphic(20);
using(var ms = new MemoryStream(qrAsPngByteArray))
{
    testQRCode.Image = new Bitmap(ms);
}
digitaldirk commented 2 years ago

@DanielHolland
Official responses from the maintainer:

I believe you want to raise the pixelsPerModule value in this line: byte[] qrAsPngByteArray = qrCode.GetGraphic(20);

Source from the Wiki

Parameter name | Type | Default | Description -- | -- | -- | -- pixelsPerModule | int |   | The pixel size each b/w module is drawn

It will make the file size larger but will sharpen the image.

Alternative

SvgQRCode This one is vector based so it can be resized without loss of quality but it may not fit your requirements.

codebude commented 3 months ago

Hi @DanielHolland the solution to your problem was perfectly summarized by @digitaldirk thus I'm closing the issue. In case you have any further questions just reopen the issue. :-)