/
generate.go
70 lines (60 loc) · 1.2 KB
/
generate.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
package qr
import (
"bytes"
"fmt"
"image"
"io"
"github.com/dusnm/wifiqr/pkg/utils"
"github.com/yeqown/go-qrcode/v2"
"github.com/yeqown/go-qrcode/writer/standard"
)
type (
nopCloser struct {
io.Writer
}
)
func (nopCloser) Close() error {
return nil
}
func Generate(
data fmt.Stringer,
noLogo bool,
logoPath string,
) ([]byte, error) {
qcode, err := qrcode.NewWith(
data.String(),
qrcode.WithEncodingMode(qrcode.EncModeByte),
qrcode.WithErrorCorrectionLevel(qrcode.ErrorCorrectionHighest),
)
var logo image.Image
if !noLogo {
if logoPath != "" {
logo, err = utils.MakeLogoFromPath(logoPath)
if err != nil {
return nil, err
}
} else {
logof, err := resources.Open("res/logo.png")
if err != nil {
return nil, err
}
logo, _, err = image.Decode(logof)
if err != nil {
return nil, err
}
}
}
b := bytes.NewBuffer(nil)
w := standard.NewWithWriter(
nopCloser{Writer: b},
// top, right, bottom, left
standard.WithBorderWidth(90, 40, 40, 40),
standard.WithCircleShape(),
standard.WithBuiltinImageEncoder(standard.PNG_FORMAT),
standard.WithLogoImage(logo),
)
if err = qcode.Save(w); err != nil {
return nil, err
}
return b.Bytes(), nil
}