-
Notifications
You must be signed in to change notification settings - Fork 3
/
qrcode.go
204 lines (177 loc) · 3.85 KB
/
qrcode.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package qrcode
import (
"bytes"
"fmt"
"image"
"image/color"
"image/png"
"math/rand"
"os"
"time"
qrcode "github.com/skip2/go-qrcode"
)
// const (
// QR_CODE_SIZE = 256
// SHRINK_QR_CODE_SIZE = 35
// MARGIN = 29
// MULTIPLE = 6
// )
const (
QR_CODE_SIZE = 301
SHRINK_QR_CODE_SIZE = 39
MARGIN = 21
MULTIPLE = 7
)
type QRCode struct {
url string
img image.Image
genImg bool
points [QR_CODE_SIZE][QR_CODE_SIZE]int
tmpShrinkPoints [QR_CODE_SIZE][SHRINK_QR_CODE_SIZE]int
shrinkPoints [SHRINK_QR_CODE_SIZE][SHRINK_QR_CODE_SIZE]int
}
//NewQRCode 返回二维码结构
func NewQRCode(uri string, genImg bool) *QRCode {
qr := &QRCode{
url: uri,
genImg: genImg,
}
qr.genQRCode()
return qr
}
//genQRCode 生成二维码
func (qr *QRCode) genQRCode() error {
code, err := qrcode.Encode(qr.url, qrcode.Medium, QR_CODE_SIZE)
if err != nil {
return err
}
buf := bytes.NewBuffer(code)
img, err := png.Decode(buf)
if err != nil {
return err
}
qr.img = img
if qr.genImg {
newPng, _ := os.Create("qrcode.png")
defer newPng.Close()
png.Encode(newPng, img)
}
return nil
}
func (qr *QRCode) SetImage(img image.Image) error {
qr.img = img
if qr.genImg {
newPng, _ := os.Create("qrcode.png")
defer newPng.Close()
png.Encode(newPng, img)
}
return nil
}
//binarization 二维码图片二值化 0-1
func (qr *QRCode) binarization() {
gray := image.NewGray(image.Rect(0, 0, QR_CODE_SIZE, QR_CODE_SIZE))
for x := 0; x < QR_CODE_SIZE; x++ {
for y := 0; y < QR_CODE_SIZE; y++ {
r32, g32, b32, _ := qr.img.At(x, y).RGBA()
r, g, b := int(r32>>8), int(g32>>8), int(b32>>8)
if (r+g+b)/3 > 180 {
qr.points[y][x] = 0
gray.Set(x, y, color.Gray{uint8(255)})
} else {
qr.points[y][x] = 1
gray.Set(x, y, color.Gray{uint8(0)})
}
}
}
if qr.genImg {
newPng, _ := os.Create("qrcode.binarization.png")
defer newPng.Close()
png.Encode(newPng, gray)
}
}
//shrink 缩小二值化数组
func (qr *QRCode) shrink() {
for x := 0; x < QR_CODE_SIZE; x++ {
cal := 1
for y := MARGIN + 1; y < QR_CODE_SIZE-MARGIN; y += MULTIPLE {
qr.tmpShrinkPoints[x][cal] = qr.points[x][y]
cal++
}
}
for y := 1; y < SHRINK_QR_CODE_SIZE-1; y++ {
row := 1
for x := MARGIN + 1; x < QR_CODE_SIZE-MARGIN; x += MULTIPLE {
qr.shrinkPoints[row][y] = qr.tmpShrinkPoints[x][y]
row++
}
}
}
//Output 控制台输出二维码
func (qr *QRCode) Output() {
qr.binarization()
qr.shrink()
for x := 0; x < SHRINK_QR_CODE_SIZE; x++ {
for y := 0; y < SHRINK_QR_CODE_SIZE; y++ {
if qr.shrinkPoints[x][y] == 1 {
//fmt.Print("\033[40;40m \033[0m")
randColor()
} else {
fmt.Print("\033[47;30m \033[0m")
}
}
fmt.Println()
}
}
func randColor() {
cls := []int{41, 42, 43, 44, 45, 46}
rand.Seed(time.Now().UnixNano())
i := rand.Intn(1)
fmt.Printf("\033[40;%dm \033[0m", cls[i])
}
//Debug 调试二维码二值化及缩小过程
func (qr *QRCode) Debug() {
qr.binarization()
//qr.shrink()
src, _ := os.Create("src.txt")
for i := 0; i < len(qr.points); i++ {
var line string
for j := 0; j < len(qr.points[i]); j++ {
if qr.points[i][j] == 1 {
line += "1"
} else {
line += "0"
}
}
line += "\n"
src.WriteString(line)
}
src.Close()
tmp, _ := os.Create("tmp.txt")
for i := 0; i < len(qr.tmpShrinkPoints); i++ {
var line string
for j := 0; j < len(qr.tmpShrinkPoints[i]); j++ {
if qr.tmpShrinkPoints[i][j] == 1 {
line += "1"
} else {
line += "0"
}
}
line += "\n"
tmp.WriteString(line)
}
tmp.Close()
dst, _ := os.Create("dst.txt")
for i := 0; i < len(qr.shrinkPoints); i++ {
var line string
for j := 0; j < len(qr.shrinkPoints[i]); j++ {
if qr.shrinkPoints[i][j] == 1 {
line += "1"
} else {
line += "0"
}
}
line += "\n"
dst.WriteString(line)
}
dst.Close()
}