/
GenerateQR.go
154 lines (123 loc) · 3.04 KB
/
GenerateQR.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"bufio"
"code.google.com/p/rsc/qr"
"encoding/csv"
"flag"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"os"
"sync"
)
var (
seperator *string
label *int
filename *string
outdir *string
scale int
black *image.RGBA
white *image.RGBA
)
func init() {
seperator = flag.String("s", ";", "seperator used in the CSV")
label = flag.Int("l", 0, "the column to use as filename")
outdir = flag.String("o", ".", "directory for the generated PNG files")
filename = flag.String("i", "input.csv", "input file to process")
flag.Usage = func() {
fmt.Fprint(os.Stderr, "Generates a QR code for each row in the specified CSV file.\n\n")
flag.PrintDefaults()
}
flag.IntVar(&scale, "sc", 10, "pixel size of a dot in the QR code")
}
func main() {
// read parameters
flag.Parse()
// open input file
inFile, err := os.Open(*filename)
if err != nil {
panic(err)
}
// close file on exit and check for its returned error
defer func() {
if err := inFile.Close(); err != nil {
panic(err)
}
}()
r := csv.NewReader(bufio.NewReader(inFile))
r.Comma = ';'
r.Comment = '#'
records, err := r.ReadAll()
if err != nil {
panic(err)
}
// prepare images to draw white an black rectangles
black = image.NewRGBA(image.Rect(0, 0, scale, scale))
white = image.NewRGBA(image.Rect(0, 0, scale, scale))
cB := color.RGBA{0x0, 0x0, 0x0, 0xFF}
cW := color.RGBA{0xFF, 0xFF, 0xFF, 0x0}
draw.Draw(black, black.Bounds(), &image.Uniform{cB}, image.ZP, draw.Src)
draw.Draw(white, white.Bounds(), &image.Uniform{cW}, image.ZP, draw.Src)
var headers []string
pool := make(chan bool, 200/scale)
var wg sync.WaitGroup
for i, v := range records {
if i == 0 {
// first row are the headings
headers = v
} else {
wg.Add(1)
pool <- true
go generateQr(headers, v, pool, &wg)
}
}
wg.Wait()
}
func generateQr(headers []string, row []string, pool chan bool, wg *sync.WaitGroup) {
filename := row[*label]
var text string
//build the output string
for index, value := range row {
text += headers[index] + ": " + value + "\n"
}
// Encode string to QR codes
code, err := qr.Encode(text, qr.M)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// convert code to rgba
b := code.Size
img := image.NewRGBA(image.Rect(0, 0, b*scale, b*scale))
for x := 0; x < b; x++ {
for y := 0; y < b; y++ {
if code.Black(x, y) {
draw.Draw(img, image.Rect(x*scale, y*scale, (x+1)*scale, (y+1)*scale), black, black.Bounds().Min, draw.Src)
} else {
draw.Draw(img, image.Rect(x*scale, y*scale, (x+1)*scale, (y+1)*scale), white, white.Bounds().Min, draw.Src)
}
}
}
// verify output dir
if _, err := os.Stat(*outdir); err != nil {
os.Mkdir(*outdir, 0666)
}
//save the imgByte to file
filename = *outdir + "/" + filename + ".png"
out, err := os.Create(filename)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = png.Encode(out, img)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// everything ok
fmt.Println("QR code generated and saved to " + filename)
<-pool
(*wg).Done()
}