-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqrcode.go
178 lines (150 loc) · 4.1 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
package main
import (
"fmt"
"image"
"time"
"github.com/disintegration/imaging"
log "github.com/sirupsen/logrus"
"github.com/yeqown/go-qrcode/v2"
"github.com/yeqown/go-qrcode/writer/standard"
)
type LogoWidth int
type FileType string
type OutputSize int
type Option func(*QrCodeGen)
const (
MINI LogoWidth = 4
MEDIUM LogoWidth = 3
BIG LogoWidth = 2
NOT FileType = ""
JPG FileType = "jpg"
PNG FileType = "png"
OutputMini OutputSize = 200
OutputMedium OutputSize = 500
OutputBig OutputSize = 1000
DefaultLogoWidth = MEDIUM
DefaultOutputSize = OutputMedium
DefaultFileType = JPG
)
type QrCodeGen struct {
Name string
Content string
LogoFile string
LogoWidth LogoWidth
HalftoneSrcFile string
Width OutputSize
OutputFileType FileType
Path string
}
func NewQuCodeGen(content string, opts ...Option) *QrCodeGen {
gen := &QrCodeGen{
Content: content,
Width: DefaultOutputSize,
OutputFileType: DefaultFileType,
LogoWidth: DefaultLogoWidth,
}
for _, opt := range opts {
opt(gen)
}
return gen
}
func WithLogoFile(fileName string) Option {
return func(c *QrCodeGen) {
c.LogoFile = fileName
}
}
func WithLogoWidth(width LogoWidth) Option {
return func(c *QrCodeGen) {
c.LogoWidth = width
}
}
func WithHalftoneSrcFile(fileName string) Option {
return func(c *QrCodeGen) {
c.HalftoneSrcFile = fileName
}
}
func WithName(name string) Option {
return func(c *QrCodeGen) {
c.Name = name
}
}
func WithOutputFileType(fileType FileType) Option {
return func(c *QrCodeGen) {
c.OutputFileType = fileType
}
}
func WithOutputFileSize(size OutputSize) Option {
return func(c *QrCodeGen) {
c.Width = size
}
}
func WithPath(path string) Option {
return func(c *QrCodeGen) {
c.Path = path
}
}
func (g *QrCodeGen) GenQrCode() (string, error) {
// 确认文件名称
qrFileName := fmt.Sprintf("%d.%s", time.Now().UnixMilli(), g.OutputFileType)
if g.Name != "" {
qrFileName = fmt.Sprintf("%s.%s", g.Name, g.OutputFileType)
}
// 内容
qrc, err := qrcode.NewWith(g.Content,
qrcode.WithEncodingMode(qrcode.EncModeByte),
qrcode.WithErrorCorrectionLevel(qrcode.ErrorCorrectionMedium),
)
if err != nil {
log.Errorf("qrcode.NewWith error: %v", err)
return "", err
}
// 基本内容
imageOptions := make([]standard.ImageOption, 0)
imageOptions = append(imageOptions, standard.WithQRWidth(uint8(g.Width/10)))
if g.LogoFile != "" {
var resizeImg *image.NRGBA
logoSrc, err := imaging.Open(g.LogoFile)
if err != nil {
log.Errorf("imaging.Open error: %v", err)
return "", err
}
logoWidth, logoHeight := logoSrc.Bounds().Dx(), logoSrc.Bounds().Dy()
log.Infof("logofile size width: %d ,height: %d", logoWidth, logoHeight)
if g.LogoWidth > 0 {
switch g.LogoWidth {
case MINI:
resizeImg = imaging.Resize(logoSrc, int(g.Width)/int(MINI), int(g.Width)/int(MINI), imaging.Lanczos)
case MEDIUM:
resizeImg = imaging.Resize(logoSrc, int(g.Width)/int(MEDIUM), int(g.Width)/int(MEDIUM), imaging.Lanczos)
case BIG:
resizeImg = imaging.Resize(logoSrc, int(g.Width)/int(BIG), int(g.Width)/int(BIG), imaging.Lanczos)
}
} else {
resizeImg = imaging.Resize(logoSrc, int(g.Width)/int(MEDIUM), int(g.Width)/int(MEDIUM), imaging.Lanczos)
}
g.LogoFile = fmt.Sprintf("%s_tmp.%s", GetFileName(g.LogoFile), JPG)
if err = imaging.Save(resizeImg, g.LogoFile); err != nil {
log.Errorf("imaging.Save: %v", err)
return "", err
}
imageOptions = append(imageOptions, standard.WithLogoImageFileJPEG(g.LogoFile))
imageOptions = append(imageOptions, standard.WithLogoSizeMultiplier(int(g.LogoWidth)))
}
// Halftone
if g.HalftoneSrcFile != "" {
imageOptions = append(imageOptions, []standard.ImageOption{
standard.WithHalftone(g.HalftoneSrcFile),
standard.WithBgTransparent(),
}...)
}
w, err := standard.New(fmt.Sprintf("%s/%s", fmt.Sprintf(".%s", g.Path), qrFileName), imageOptions...)
if err != nil {
log.Errorf("qrcode.NewWith error: %v", err)
return "", err
}
if err = qrc.Save(w); err != nil {
log.Errorf("qrc.Save: %v", err)
return "", err
}
return qrFileName, nil
}