1 Star 0 Fork 0

白菜林 / go-pkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
qrcode.go 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
白菜林 提交于 2021-05-24 22:57 . 二维码合成海报工具
package qrposter
import (
"crypto/md5"
"encoding/hex"
"image/jpeg"
"os"
"path"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
)
type QrCode struct {
URL string
Width int
Height int
Ext string
Level qr.ErrorCorrectionLevel
Mode qr.Encoding
FileName string
}
const (
EXT_JPG = ".jpg"
)
func NewQrCode(url string, width, height int, level qr.ErrorCorrectionLevel, mode qr.Encoding) *QrCode {
return &QrCode{
URL: url,
Width: width,
Height: height,
Level: level,
Mode: mode,
Ext: EXT_JPG,
}
}
func (q *QrCode) GetQrCodeExt() string {
return q.Ext
}
// Encode generate QR code
func (q *QrCode) Encode(folderPath string) (filePath string, err error) {
name := encodeMD5(q.URL) + q.GetQrCodeExt()
filePath = path.Join(folderPath, name)
q.FileName = name
code, err := qr.Encode(q.URL, q.Level, q.Mode)
if err != nil {
return "", err
}
code, err = barcode.Scale(code, q.Width, q.Height)
if err != nil {
return "", err
}
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return "", err
}
defer f.Close()
err = jpeg.Encode(f, code, nil)
if err != nil {
return "", err
}
return filePath, nil
}
func encodeMD5(value string) string {
m := md5.New()
m.Write([]byte(value))
return hex.EncodeToString(m.Sum(nil))
}
Go
1
https://gitee.com/lyhuilin/pkg.git
git@gitee.com:lyhuilin/pkg.git
lyhuilin
pkg
go-pkg
v0.0.1

搜索帮助

53164aa7 5694891 3bd8fe86 5694891