-
Notifications
You must be signed in to change notification settings - Fork 0
/
QRCode.go
60 lines (52 loc) · 1.28 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
package main
import (
"NothinBot/EasyBot"
"bytes"
"encoding/base64"
"image/png"
"regexp"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
log "github.com/sirupsen/logrus"
)
func checkQRCode(ctx *EasyBot.CQMessage) {
matches := ctx.RegFindAllStringSubmatch(regexp.MustCompile(`(?s)制作二维码\s*(.*)`))
if len(matches) > 0 {
s := trimOuterQuotes(matches[0][1])
replyMsg, err := ctx.GetReplyedMsg()
if replyMsg != nil && err == nil { //复述回复时无视内容
s = trimOuterQuotes(replyMsg.RawMessage)
}
qrc, _ := NewQRcode().With(s, 512)
ctx.SendMsgReply(bot.Utils.Format.ImageBase64(qrc.ToBase64()))
}
}
type QRcode []byte
func NewQRcode() *QRcode {
return &QRcode{}
}
func (qrc QRcode) With(content string, size int) (QRcode, error) {
if size == 0 {
size = 256
}
code, err := qr.Encode(content, qr.L, qr.Auto)
if err != nil {
log.Error("[QRcode] err1:", err)
return qrc, err
}
code, err = barcode.Scale(code, size, size)
if err != nil {
log.Error("[QRcode] err2:", err)
return qrc, err
}
buf := new(bytes.Buffer)
err = png.Encode(buf, code)
if err != nil {
log.Error("[QRcode] err3:", err)
return qrc, err
}
return buf.Bytes(), nil
}
func (qrc QRcode) ToBase64() string {
return base64.StdEncoding.EncodeToString(qrc)
}