-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr_code.go
116 lines (112 loc) · 1.81 KB
/
qr_code.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
package qrcode
import (
"github.com/pkg/errors"
"github.com/skip2/go-qrcode"
"image/color"
"strconv"
"strings"
)
// 这是一坨定义好的颜色,可以往里面加颜色,看你们喜欢
var (
White = color.RGBA{
R: 255,
G: 255,
B: 255,
A: 255,
}
Black = color.RGBA{
R: 0,
G: 0,
B: 0,
A: 255,
}
Yellow = color.RGBA{
R: 255,
G: 255,
B: 0,
A: 255,
}
LightBlue = color.RGBA{
R: 0,
G: 255,
B: 255,
A: 255,
}
Blue = color.RGBA{
R: 0,
G: 0,
B: 255,
A: 255,
}
Red = color.RGBA{
R: 255,
G: 0,
B: 0,
A: 255,
}
Pink = color.RGBA{
R: 255,
G: 0,
B: 255,
A: 255,
}
LightPurple = color.RGBA{
R: 127,
G: 0,
B: 255,
A: 255,
}
Green = color.RGBA{
R: 0,
G: 255,
B: 0,
A: 255,
}
Orange = color.RGBA{
R: 255,
G: 127,
B: 0,
A: 255,
}
)
// CreateQRCode 创建二维码
// 注意:此处的RGBA的A 不是百分制 而是 0-255
func CreateQRCode(content string, bkColor, picColor color.RGBA, size int) ([]byte, error) {
qr, err := qrcode.New(content, qrcode.Medium)
if err != nil {
return nil, err
}
qr.BackgroundColor = bkColor
qr.ForegroundColor = picColor
png, err := qr.PNG(size)
if err != nil {
return nil, err
}
return png, nil
}
// TransferColor 十六进制转 RGBA颜色
func TransferColor(colorStr string, transparency float64) (rgba color.RGBA, err error) {
colorStr = strings.ReplaceAll(colorStr, "#", "")
if len(colorStr) != 6 {
err = errors.New("Invalid color string !")
return
}
R, err := strconv.ParseUint(colorStr[:2], 16, 32)
if err != nil {
return
}
G, err := strconv.ParseUint(colorStr[2:4], 16, 32)
if err != nil {
return
}
B, err := strconv.ParseUint(colorStr[4:6], 16, 32)
if err != nil {
return
}
return color.RGBA{
R: uint8(R),
G: uint8(G),
B: uint8(B),
A: uint8(transparency * 255),
}, nil
}