-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr_code.go
91 lines (72 loc) · 1.93 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
package service
import (
"bytes"
"encoding/base64"
"image/png"
"github.com/doutorfinancas/pun-sho/api/request"
"github.com/doutorfinancas/pun-sho/buf"
"github.com/doutorfinancas/pun-sho/str"
"github.com/yeqown/go-qrcode/v2"
"github.com/yeqown/go-qrcode/writer/standard"
)
type QRCodeService struct {
logo string
}
func NewQRCodeService(logo string) *QRCodeService {
return &QRCodeService{
logo: logo,
}
}
func (q *QRCodeService) Generate(qr *request.QRCode, link string) (string, error) {
qrc, err := qrcode.New(link)
if err != nil {
return "", err
}
bgColor := standard.WithBgColorRGBHex("#ffffff")
fgColor := standard.WithFgColorRGBHex("#000000")
if str.SubString(qr.BgColor, 0, 1) == "#" {
bgColor = standard.WithBgColorRGBHex(qr.BgColor)
}
if qr.BgColor == TransparentBackground {
bgColor = standard.WithBgTransparent()
}
if str.SubString(qr.FgColor, 0, 1) == "#" {
fgColor = standard.WithFgColorRGBHex(qr.FgColor)
}
options := []standard.ImageOption{
bgColor,
fgColor,
standard.WithBuiltinImageEncoder(standard.PNG_FORMAT),
}
if qr.LogoImage != "" {
x, err := base64.StdEncoding.DecodeString(qr.LogoImage)
if err != nil {
return "", err
}
img, err := png.Decode(bytes.NewReader(x))
if err != nil {
return "", err
}
options = append(options, standard.WithLogoImage(img))
} else if q.logo != "" {
options = append(options, standard.WithLogoImageFilePNG(q.logo))
}
if qr.Width > 0 {
options = append(options, standard.WithQRWidth(uint8(qr.Width)))
}
if qr.BorderWidth > 0 {
options = append(options, standard.WithBorderWidth(qr.BorderWidth))
}
if qr.Shape == "circle" {
options = append(options, standard.WithCircleShape())
}
var b []byte
x := bytes.NewBuffer(b)
w := buf.NewWriteCloser(x)
wr := standard.NewWithWriter(w, options...)
err = qrc.Save(wr)
if err != nil {
return "", err
}
return "data:image/png;base64," + base64.StdEncoding.EncodeToString(x.Bytes()), nil
}