Skip to content

Commit 016ced4

Browse files
committed
Merge branch 'fix_scaling'
2 parents bbbe400 + cb3bf7b commit 016ced4

7 files changed

+54
-11
lines changed

example_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package qrcode
99

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

qrcode.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ type QRCode struct {
135135
ForegroundColor color.Color
136136
BackgroundColor color.Color
137137

138+
// Disable the QR Code border.
139+
DisableBorder bool
140+
138141
encoder *dataEncoder
139142
version qrCodeVersion
140143

@@ -193,8 +196,6 @@ func New(content string, level RecoveryLevel) (*QRCode, error) {
193196
version: *chosenVersion,
194197
}
195198

196-
q.encode(chosenVersion.numTerminatorBitsRequired(encoded.Len()))
197-
198199
return q, nil
199200
}
200201

@@ -239,8 +240,6 @@ func newWithForcedVersion(content string, version int, level RecoveryLevel) (*QR
239240
version: *chosenVersion,
240241
}
241242

242-
q.encode(chosenVersion.numTerminatorBitsRequired(encoded.Len()))
243-
244243
return q, nil
245244
}
246245

@@ -251,6 +250,9 @@ func newWithForcedVersion(content string, version int, level RecoveryLevel) (*QR
251250
// The bitmap includes the required "quiet zone" around the QR Code to aid
252251
// decoding.
253252
func (q *QRCode) Bitmap() [][]bool {
253+
// Build QR code.
254+
q.encode()
255+
254256
return q.symbol.bitmap()
255257
}
256258

@@ -268,6 +270,9 @@ func (q *QRCode) Bitmap() [][]bool {
268270
// negative number to increase the scale of the image. e.g. a size of -5 causes
269271
// each module (QR Code "pixel") to be 5px in size.
270272
func (q *QRCode) Image(size int) image.Image {
273+
// Build QR code.
274+
q.encode()
275+
271276
// Minimum pixels (both width and height) required.
272277
realSize := q.symbol.size
273278

@@ -296,11 +301,12 @@ func (q *QRCode) Image(size int) image.Image {
296301
// Map each image pixel to the nearest QR code module.
297302
modulesPerPixel := float64(realSize) / float64(size)
298303
for y := 0; y < size; y++ {
304+
y2 := int(float64(y) * modulesPerPixel)
299305
for x := 0; x < size; x++ {
300-
y2 := int(float64(y) * modulesPerPixel)
301306
x2 := int(float64(x) * modulesPerPixel)
302307

303308
v := bitmap[y2][x2]
309+
304310
if v {
305311
pos := img.PixOffset(x, y)
306312
img.Pix[pos] = fgClr
@@ -368,7 +374,9 @@ func (q *QRCode) WriteFile(size int, filename string) error {
368374
// encode completes the steps required to encode the QR Code. These include
369375
// adding the terminator bits and padding, splitting the data into blocks and
370376
// applying the error correction, and selecting the best data mask.
371-
func (q *QRCode) encode(numTerminatorBits int) {
377+
func (q *QRCode) encode() {
378+
numTerminatorBits := q.version.numTerminatorBitsRequired(q.data.Len())
379+
372380
q.addTerminatorBits(numTerminatorBits)
373381
q.addPadding()
374382

@@ -381,7 +389,7 @@ func (q *QRCode) encode(numTerminatorBits int) {
381389
var s *symbol
382390
var err error
383391

384-
s, err = buildRegularSymbol(q.version, mask, encoded)
392+
s, err = buildRegularSymbol(q.version, mask, encoded, !q.DisableBorder)
385393

386394
if err != nil {
387395
log.Panic(err.Error())

qrcode/main.go

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func main() {
1717
size := flag.Int("s", 256, "image size (pixel)")
1818
textArt := flag.Bool("t", false, "print as text-art on stdout")
1919
negative := flag.Bool("i", false, "invert black and white")
20+
disableBorder := flag.Bool("d", false, "disable QR Code border")
2021
flag.Usage = func() {
2122
fmt.Fprintf(os.Stderr, `qrcode -- QR Code encoder in Go
2223
https://github.com/skip2/go-qrcode
@@ -52,6 +53,10 @@ Usage:
5253
q, err = qrcode.New(content, qrcode.Highest)
5354
checkError(err)
5455

56+
if *disableBorder {
57+
q.DisableBorder = true
58+
}
59+
5560
if *textArt {
5661
art := q.ToString(*negative)
5762
fmt.Println(art)

qrcode_decode_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func zbarimgDecode(q *QRCode) (string, error) {
196196
}
197197

198198
cmd := exec.Command("zbarimg", "--quiet", "-Sdisable",
199-
"-Sqrcode.enable", "/dev/stdin")
199+
"-Sqrcode.enable", "-")
200200

201201
var out bytes.Buffer
202202

qrcode_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ func TestQRCodeISOAnnexIExample(t *testing.T) {
151151
err.Error())
152152
}
153153

154+
q.encode()
155+
154156
const expectedMask int = 2
155157

156158
if q.mask != 2 {

regular_symbol.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,19 @@ var (
105105
)
106106

107107
func buildRegularSymbol(version qrCodeVersion, mask int,
108-
data *bitset.Bitset) (*symbol, error) {
108+
data *bitset.Bitset, includeQuietZone bool) (*symbol, error) {
109+
110+
quietZoneSize := 0
111+
if includeQuietZone {
112+
quietZoneSize = version.quietZoneSize()
113+
}
114+
109115
m := &regularSymbol{
110116
version: version,
111117
mask: mask,
112118
data: data,
113119

114-
symbol: newSymbol(version.symbolSize(), version.quietZoneSize()),
120+
symbol: newSymbol(version.symbolSize(), quietZoneSize),
115121
size: version.symbolSize(),
116122
}
117123

regular_symbol_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestBuildRegularSymbol(t *testing.T) {
1919
data.AppendNumBools(8, false)
2020
}
2121

22-
s, err := buildRegularSymbol(*v, k, data)
22+
s, err := buildRegularSymbol(*v, k, data, false)
2323

2424
if err != nil {
2525
fmt.Println(err.Error())

0 commit comments

Comments
 (0)