go-tinyqr

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

commit
016ced4a77f51518615c9ee1d47a51a9a277591e
parent
bbbe40053b64143d76939e70452fd649364bca80
Author
Tom <tfh@skip.org>
Date
2019-10-27 14:54
Merge branch 'fix_scaling'

Diffstat

M example_test.go 22 ++++++++++++++++++++++
M qrcode.go 22 +++++++++++++++-------
M qrcode/main.go 5 +++++
M qrcode_decode_test.go 2 +-
M qrcode_test.go 2 ++
M regular_symbol.go 10 ++++++++--
M regular_symbol_test.go 2 +-

7 files changed, 54 insertions, 11 deletions


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

@@ -9,6 +9,7 @@ package qrcode
    9     9 
   10    10 import (
   11    11 	"fmt"
   -1    12 	"image/color"
   12    13 	"os"
   13    14 	"testing"
   14    15 )
@@ -29,3 +30,24 @@ func TestExampleWriteFile(t *testing.T) {
   29    30 		}
   30    31 	}
   31    32 }
   -1    33 
   -1    34 func TestExampleEncodeWithColourAndWithoutBorder(t *testing.T) {
   -1    35 	q, err := New("https://example.org", Medium)
   -1    36 	if err != nil {
   -1    37 		t.Errorf("Error: %s", err)
   -1    38 		return
   -1    39 	}
   -1    40 
   -1    41 	// Optionally, disable the QR Code border.
   -1    42 	q.DisableBorder = true
   -1    43 
   -1    44 	// Optionally, set the colours.
   -1    45 	q.ForegroundColor = color.RGBA{R: 0x33, G: 0x33, B: 0x66, A: 0xff}
   -1    46 	q.BackgroundColor = color.RGBA{R: 0xef, G: 0xef, B: 0xef, A: 0xff}
   -1    47 
   -1    48 	err = q.WriteFile(256, "example2.png")
   -1    49 	if err != nil {
   -1    50 		t.Errorf("Error: %s", err)
   -1    51 		return
   -1    52 	}
   -1    53 }

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

@@ -135,6 +135,9 @@ type QRCode struct {
  135   135 	ForegroundColor color.Color
  136   136 	BackgroundColor color.Color
  137   137 
   -1   138 	// Disable the QR Code border.
   -1   139 	DisableBorder bool
   -1   140 
  138   141 	encoder *dataEncoder
  139   142 	version qrCodeVersion
  140   143 
@@ -193,8 +196,6 @@ func New(content string, level RecoveryLevel) (*QRCode, error) {
  193   196 		version: *chosenVersion,
  194   197 	}
  195   198 
  196    -1 	q.encode(chosenVersion.numTerminatorBitsRequired(encoded.Len()))
  197    -1 
  198   199 	return q, nil
  199   200 }
  200   201 
@@ -239,8 +240,6 @@ func newWithForcedVersion(content string, version int, level RecoveryLevel) (*QR
  239   240 		version: *chosenVersion,
  240   241 	}
  241   242 
  242    -1 	q.encode(chosenVersion.numTerminatorBitsRequired(encoded.Len()))
  243    -1 
  244   243 	return q, nil
  245   244 }
  246   245 
@@ -251,6 +250,9 @@ func newWithForcedVersion(content string, version int, level RecoveryLevel) (*QR
  251   250 // The bitmap includes the required "quiet zone" around the QR Code to aid
  252   251 // decoding.
  253   252 func (q *QRCode) Bitmap() [][]bool {
   -1   253 	// Build QR code.
   -1   254 	q.encode()
   -1   255 
  254   256 	return q.symbol.bitmap()
  255   257 }
  256   258 
@@ -268,6 +270,9 @@ func (q *QRCode) Bitmap() [][]bool {
  268   270 // negative number to increase the scale of the image. e.g. a size of -5 causes
  269   271 // each module (QR Code "pixel") to be 5px in size.
  270   272 func (q *QRCode) Image(size int) image.Image {
   -1   273 	// Build QR code.
   -1   274 	q.encode()
   -1   275 
  271   276 	// Minimum pixels (both width and height) required.
  272   277 	realSize := q.symbol.size
  273   278 
@@ -296,11 +301,12 @@ func (q *QRCode) Image(size int) image.Image {
  296   301 	// Map each image pixel to the nearest QR code module.
  297   302 	modulesPerPixel := float64(realSize) / float64(size)
  298   303 	for y := 0; y < size; y++ {
   -1   304 		y2 := int(float64(y) * modulesPerPixel)
  299   305 		for x := 0; x < size; x++ {
  300    -1 			y2 := int(float64(y) * modulesPerPixel)
  301   306 			x2 := int(float64(x) * modulesPerPixel)
  302   307 
  303   308 			v := bitmap[y2][x2]
   -1   309 
  304   310 			if v {
  305   311 				pos := img.PixOffset(x, y)
  306   312 				img.Pix[pos] = fgClr
@@ -368,7 +374,9 @@ func (q *QRCode) WriteFile(size int, filename string) error {
  368   374 // encode completes the steps required to encode the QR Code. These include
  369   375 // adding the terminator bits and padding, splitting the data into blocks and
  370   376 // applying the error correction, and selecting the best data mask.
  371    -1 func (q *QRCode) encode(numTerminatorBits int) {
   -1   377 func (q *QRCode) encode() {
   -1   378 	numTerminatorBits := q.version.numTerminatorBitsRequired(q.data.Len())
   -1   379 
  372   380 	q.addTerminatorBits(numTerminatorBits)
  373   381 	q.addPadding()
  374   382 
@@ -381,7 +389,7 @@ func (q *QRCode) encode(numTerminatorBits int) {
  381   389 		var s *symbol
  382   390 		var err error
  383   391 
  384    -1 		s, err = buildRegularSymbol(q.version, mask, encoded)
   -1   392 		s, err = buildRegularSymbol(q.version, mask, encoded, !q.DisableBorder)
  385   393 
  386   394 		if err != nil {
  387   395 			log.Panic(err.Error())

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

@@ -17,6 +17,7 @@ func main() {
   17    17 	size := flag.Int("s", 256, "image size (pixel)")
   18    18 	textArt := flag.Bool("t", false, "print as text-art on stdout")
   19    19 	negative := flag.Bool("i", false, "invert black and white")
   -1    20 	disableBorder := flag.Bool("d", false, "disable QR Code border")
   20    21 	flag.Usage = func() {
   21    22 		fmt.Fprintf(os.Stderr, `qrcode -- QR Code encoder in Go
   22    23 https://github.com/skip2/go-qrcode
@@ -52,6 +53,10 @@ Usage:
   52    53 	q, err = qrcode.New(content, qrcode.Highest)
   53    54 	checkError(err)
   54    55 
   -1    56 	if *disableBorder {
   -1    57 		q.DisableBorder = true
   -1    58 	}
   -1    59 
   55    60 	if *textArt {
   56    61 		art := q.ToString(*negative)
   57    62 		fmt.Println(art)

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

@@ -196,7 +196,7 @@ func zbarimgDecode(q *QRCode) (string, error) {
  196   196 	}
  197   197 
  198   198 	cmd := exec.Command("zbarimg", "--quiet", "-Sdisable",
  199    -1 		"-Sqrcode.enable", "/dev/stdin")
   -1   199 		"-Sqrcode.enable", "-")
  200   200 
  201   201 	var out bytes.Buffer
  202   202 

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

@@ -151,6 +151,8 @@ func TestQRCodeISOAnnexIExample(t *testing.T) {
  151   151 			err.Error())
  152   152 	}
  153   153 
   -1   154 	q.encode()
   -1   155 
  154   156 	const expectedMask int = 2
  155   157 
  156   158 	if q.mask != 2 {

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

@@ -105,13 +105,19 @@ var (
  105   105 )
  106   106 
  107   107 func buildRegularSymbol(version qrCodeVersion, mask int,
  108    -1 	data *bitset.Bitset) (*symbol, error) {
   -1   108 	data *bitset.Bitset, includeQuietZone bool) (*symbol, error) {
   -1   109 
   -1   110 	quietZoneSize := 0
   -1   111 	if includeQuietZone {
   -1   112 		quietZoneSize = version.quietZoneSize()
   -1   113 	}
   -1   114 
  109   115 	m := &regularSymbol{
  110   116 		version: version,
  111   117 		mask:    mask,
  112   118 		data:    data,
  113   119 
  114    -1 		symbol: newSymbol(version.symbolSize(), version.quietZoneSize()),
   -1   120 		symbol: newSymbol(version.symbolSize(), quietZoneSize),
  115   121 		size:   version.symbolSize(),
  116   122 	}
  117   123 

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

@@ -19,7 +19,7 @@ func TestBuildRegularSymbol(t *testing.T) {
   19    19 			data.AppendNumBools(8, false)
   20    20 		}
   21    21 
   22    -1 		s, err := buildRegularSymbol(*v, k, data)
   -1    22 		s, err := buildRegularSymbol(*v, k, data, false)
   23    23 
   24    24 		if err != nil {
   25    25 			fmt.Println(err.Error())