-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathqrcode2console.go
164 lines (148 loc) · 3.37 KB
/
qrcode2console.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
package qrcode2console
import (
"bytes"
"fmt"
"github.com/disintegration/imaging"
"github.com/skip2/go-qrcode"
"image"
"image/color"
"image/png"
"os"
)
/*
* @Author:hanyajun
* @Date:2019/4/30 14:56
* @Name:qrcode2console
* @Function: 二维码输出到console
*/
const (
QR_CODE_SIZE = 256
SHRINK_QR_CODE_SIZE = 35
MARGIN = 29
MULTIPLE = 6
)
type QRCode2Console struct {
url string
img image.Image
Path string
kind int
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 NewQRCode2ConsoleWithUrl(url string, genImg bool) *QRCode2Console {
qr := &QRCode2Console{
url: url,
genImg: genImg,
kind: 0,
}
qr.genQRCode()
return qr
}
//通过二维码图片
func NewQRCode2ConsoleWithPath(path string) *QRCode2Console {
qr := &QRCode2Console{
Path: path,
genImg: false,
kind: 1,
}
qr.genQRCode()
return qr
}
//genQRCode 生成二维码矩阵
func (qr *QRCode2Console) genQRCode() error {
var err error
var img image.Image
if qr.kind == 0 {
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)
} else if qr.kind == 1 {
file, err := os.Open(qr.Path)
if err != nil {
return err
}
defer file.Close()
dstImag, _, err := image.Decode(file)
img = imaging.Resize(dstImag, QR_CODE_SIZE, QR_CODE_SIZE, imaging.Lanczos)
}
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 *QRCode2Console) 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 *QRCode2Console) 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 *QRCode2Console) 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 *QRCode2Console) 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()
}
}