skip2 / go-qrcode

:sparkles: QR Code encoder (Go)
http://go-qrcode.appspot.com
MIT License
2.58k stars 335 forks source link

fix add unnecessary offset when drawn image #24

Closed mitu217 closed 4 years ago

mitu217 commented 4 years ago

I wanna use QR code as a design. but this library add unnecessary offset when drawn image...

the reason is that the QR code is resized only linearly based on realsize.

// Size of each module drawn.
pixelsPerModule := size / realSize

// Center the symbol within the image.
offset := (size - realSize*pixelsPerModule) / 2

I tried changing to a method of resizing using golang.org/x/image/draw after drawing to appropriate drawing size once

As a result, it was possible to correct that unnecessary offsets when drawn image

skip2 commented 4 years ago

I have been working on a fix for this, which doesn't need any image resizing: https://github.com/skip2/go-qrcode/commit/80e59a6a8136ad8330912576a584f91de71b6689

Here is a comparison between the old and new generated QR codes: https://skip.org/drop/fix_scaling/

If you see in particular QR code #99, the old QR code has too much whitespace, and the new QR code has the minimum amount of white space.

Is this better?

mitu217 commented 4 years ago

@skip2 it looks like great!!

thank you for working fix for this 🙏

mitu217 commented 4 years ago

I checked the size of the new version QR code further, as a result I found that the margins are not constantiate...

Probably, difference is occured when casting from float to int 🤔

skip2 commented 4 years ago

The QR code specification calls for a 4 "module" (4 block/pixel) border around each QR code to make it easier for them to be decoded. This makes the border variable sized, as you can see here:

The border is kind of optional though, QR code reader software will still read a QR code with a small, or even no border.

To help, I could add an option to disable the border entirely? The above QR codes would be generated as:

https://skip.org/drop/fix_scaling is updated with another column.

https://github.com/skip2/go-qrcode/commit/cb3bf7ba5ad6fdfa39f4e0533bdee90445d8bb89

mitu217 commented 4 years ago

Sorry for my late reply 🙇

To help, I could add an option to disable the border entirely?

DisableBorder flag is nice idea! I agree with it 👍

skip2 commented 4 years ago

I have committed these changes (minimise border in QR code images, option to disable the border entirely, and a -d flag to the command line tool).

https://github.com/skip2/go-qrcode/ now has a section on how to disable borders. https://gist.github.com/skip2/7e3d8a82f5317df9be437f8ec8ec0b7d

thank you for your help :-)

mitu217 commented 4 years ago

thanks! I will use added option!! :+1: