fix
This commit is contained in:
@@ -7,37 +7,57 @@ import (
|
||||
"github.com/skip2/go-qrcode"
|
||||
"golang.org/x/image/draw"
|
||||
"image"
|
||||
"image/color"
|
||||
_ "image/jpeg"
|
||||
"image/png"
|
||||
"os"
|
||||
)
|
||||
|
||||
// 带logo的二维码图片生成 content-二维码内容 size-像素单位 outPath 保存路径(传空则不保存) disableBorder是否不启用边框 logoPath-logo文件路径(传空就不带) x:x轴整体偏移 y:y轴整体偏移
|
||||
func CreateQrCodeBs64WithLogo(content, outPath string, logoPath string, size, x, y int, disableBorder bool) (data string, err error) {
|
||||
code, err := qrcode.New(content, qrcode.High)
|
||||
type CreateQrCodeBs64WithLogoReq struct {
|
||||
Content string //二维码内容
|
||||
OutPath *string //二维码保存路径(可选)
|
||||
LogoPath *string //logo路径(可选)
|
||||
Size int64 //二维码尺寸
|
||||
X *int64 //二维码整体x偏移
|
||||
Y *int64 //二维码整体Y偏移
|
||||
ForegroundColor *color.Color //二维码纹路颜色
|
||||
BackgroundColor *color.Color //二维码背景颜色
|
||||
DisableBorder bool // 是否不要二维码边框
|
||||
}
|
||||
|
||||
func CreateQrCodeBs64WithLogo(req CreateQrCodeBs64WithLogoReq) (data string, err error) {
|
||||
code, err := qrcode.New(req.Content, qrcode.High)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if disableBorder {
|
||||
if req.DisableBorder {
|
||||
code.DisableBorder = true
|
||||
}
|
||||
//二维码纹路颜色
|
||||
if req.ForegroundColor != nil {
|
||||
code.ForegroundColor = *req.ForegroundColor
|
||||
}
|
||||
//背景颜色
|
||||
if req.BackgroundColor != nil {
|
||||
code.BackgroundColor = *req.BackgroundColor
|
||||
}
|
||||
//设置文件大小并创建画板
|
||||
qrcodeImg := code.Image(size)
|
||||
qrcodeImg := code.Image(int(req.Size))
|
||||
outImg := image.NewRGBA(qrcodeImg.Bounds())
|
||||
buf := new(bytes.Buffer)
|
||||
//无logo
|
||||
if logoPath == "" {
|
||||
if req.LogoPath == nil {
|
||||
//图像偏移
|
||||
if x != 0 || y != 0 {
|
||||
draw.Draw(outImg, outImg.Bounds().Add(image.Pt(x, y)), qrcodeImg, outImg.Bounds().Min, draw.Src)
|
||||
if req.X != nil && req.Y != nil {
|
||||
draw.Draw(outImg, outImg.Bounds().Add(image.Pt(int(*req.X), int(*req.Y))), qrcodeImg, outImg.Bounds().Min, draw.Src)
|
||||
}
|
||||
err = png.Encode(buf, outImg)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if outPath != "" {
|
||||
if req.OutPath != nil {
|
||||
// 写入文件
|
||||
f, err := os.Create(outPath)
|
||||
f, err := os.Create(*req.OutPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -50,7 +70,7 @@ func CreateQrCodeBs64WithLogo(content, outPath string, logoPath string, size, x,
|
||||
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
|
||||
}
|
||||
//读取logo文件
|
||||
logoFile, err := os.Open(logoPath)
|
||||
logoFile, err := os.Open(*req.LogoPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -58,7 +78,7 @@ func CreateQrCodeBs64WithLogo(content, outPath string, logoPath string, size, x,
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
logoImg = resize.Resize(uint(size/5), uint(size/5), logoImg, resize.Lanczos3)
|
||||
logoImg = resize.Resize(uint(req.Size/5), uint(req.Size/5), logoImg, resize.Lanczos3)
|
||||
// 添加边框
|
||||
// 图片到边框距离
|
||||
pic2FramePadding := logoImg.Bounds().Dx() / 10
|
||||
@@ -79,16 +99,16 @@ func CreateQrCodeBs64WithLogo(content, outPath string, logoPath string, size, x,
|
||||
offset := image.Pt((outImg.Bounds().Max.X-transparentImg.Bounds().Max.X)/2, (outImg.Bounds().Max.Y-transparentImg.Bounds().Max.Y)/2)
|
||||
draw.Draw(outImg, outImg.Bounds().Add(offset), transparentImg, image.Pt(0, 0), draw.Over)
|
||||
//图像偏移
|
||||
if x != 0 || y != 0 {
|
||||
draw.Draw(outImg, outImg.Bounds().Add(image.Pt(x, y)), qrcodeImg, outImg.Bounds().Min, draw.Src)
|
||||
if req.X != nil && req.Y != nil {
|
||||
draw.Draw(outImg, outImg.Bounds().Add(image.Pt(int(*req.X), int(*req.Y))), qrcodeImg, outImg.Bounds().Min, draw.Src)
|
||||
}
|
||||
err = png.Encode(buf, outImg)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if outPath != "" {
|
||||
if req.OutPath != nil {
|
||||
// 写入文件
|
||||
f, err := os.Create(outPath)
|
||||
f, err := os.Create(*req.OutPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user