go-tinyqr

An experimental minimal QR Code generator  http://go-qrcode.appspot.com
git clone https://git.ce9e.org/go-tinyqr.git

commit
bbbe40053b64143d76939e70452fd649364bca80
parent
dc11ecdae0a9889dc81a343585516404e8dc6ead
Author
Tom <tfh@skip.org>
Date
2019-10-14 17:07
Fix scaling of QR codes in fixed sized images.

Previously each QR code module was the same number of pixels in width/height; this leads to some unfavourably small QR codes generated, with lots of white space.

Now, we map each image pixel to the nearest "pixel" in the QR code instead. QR code modules can now vary width/height by a pixel or two.

Diffstat

M qrcode.go 29 +++++++++++++----------------

1 files changed, 13 insertions, 16 deletions


diff --git a/qrcode.go b/qrcode.go

@@ -282,12 +282,7 @@ func (q *QRCode) Image(size int) image.Image {
  282   282 		size = realSize
  283   283 	}
  284   284 
  285    -1 	// Size of each module drawn.
  286    -1 	pixelsPerModule := size / realSize
  287    -1 
  288    -1 	// Center the symbol within the image.
  289    -1 	offset := (size - realSize*pixelsPerModule) / 2
  290    -1 
   -1   285 	// Output image.
  291   286 	rect := image.Rectangle{Min: image.Point{0, 0}, Max: image.Point{size, size}}
  292   287 
  293   288 	// Saves a few bytes to have them in this order
@@ -295,18 +290,20 @@ func (q *QRCode) Image(size int) image.Image {
  295   290 	img := image.NewPaletted(rect, p)
  296   291 	fgClr := uint8(img.Palette.Index(q.ForegroundColor))
  297   292 
   -1   293 	// QR code bitmap.
  298   294 	bitmap := q.symbol.bitmap()
  299    -1 	for y, row := range bitmap {
  300    -1 		for x, v := range row {
   -1   295 
   -1   296 	// Map each image pixel to the nearest QR code module.
   -1   297 	modulesPerPixel := float64(realSize) / float64(size)
   -1   298 	for y := 0; y < size; y++ {
   -1   299 		for x := 0; x < size; x++ {
   -1   300 			y2 := int(float64(y) * modulesPerPixel)
   -1   301 			x2 := int(float64(x) * modulesPerPixel)
   -1   302 
   -1   303 			v := bitmap[y2][x2]
  301   304 			if v {
  302    -1 				startX := x*pixelsPerModule + offset
  303    -1 				startY := y*pixelsPerModule + offset
  304    -1 				for i := startX; i < startX+pixelsPerModule; i++ {
  305    -1 					for j := startY; j < startY+pixelsPerModule; j++ {
  306    -1 						pos := img.PixOffset(i, j)
  307    -1 						img.Pix[pos] = fgClr
  308    -1 					}
  309    -1 				}
   -1   305 				pos := img.PixOffset(x, y)
   -1   306 				img.Pix[pos] = fgClr
  310   307 			}
  311   308 		}
  312   309 	}