go语言web开发系列之二十一:用go-qrcode库生成二维码
一,安装用到的库
1,go-qrcode库的地址
https://github.com/skip2/go-qrcode
2,安装go-qrcode库
liuhongdi@ku:~$ go get -u github.com/skip2/go-qrcode/...
3,resize库的地址(在二维码中间放icon时太大时需要缩放)
https://github.com/nfnt/resize
4,安装resize库:
liuhongdi@ku:~$ go get -u github.com/nfnt/resize
说明:刘宏缔的go森林是一个专注golang的博客,
地址:https://blog.csdn.net/weixin_43881017
说明:作者:刘宏缔 邮箱: [email protected]
二,演示项目的相关信息
1,地址:
https://github.com/liuhongdi/digv21
2,功能说明:演示了生成不带icon的二维码和带icon的二维码
3,项目结构:如图:
三,go代码说明
1,controller/codeController.go
-
package controller
-
-
import (
-
"fmt"
-
"github.com/gin-gonic/gin"
-
"github.com/skip2/go-qrcode"
-
"github.com/nfnt/resize"
-
"image"
-
"image/draw"
-
"image/gif"
-
"image/jpeg"
-
"image/png"
-
"os"
-
"path/filepath"
-
"strings"
-
)
-
-
type CodeController struct{}
-
-
func NewCodeController() CodeController {
-
return CodeController{}
-
}
-
//生成一个二维码
-
func (g *CodeController) GetOne(c *gin.Context) {
-
//保存path
-
pngpath:="/data/liuhongdi/digv21/static/images/q.png"
-
-
//生成二维码
-
url:="http://www.baidu.com/"
-
qrCode, err := qrcode.New(url, qrcode.Highest)
-
if err != nil {
-
fmt.Println(err)
-
return
-
}
-
qrCode.DisableBorder = true
-
-
//保存成文件
-
qrCode.WriteFile(256,pngpath)
-
//显示二维码
-
pngurl:= "/static/images/q.png"
-
html:="<img src=\'"+pngurl+"\' />"
-
c.Header("Content-Type", "text/html; charset=utf-8")
-
c.String(200, html)
-
return
-
}
-
-
//生成一个中间带icon的二维码
-
func (g *CodeController) GetIcon(c *gin.Context) {
-
-
var (
-
bgImg image.Image
-
offset image.Point
-
avatarFile *os.File
-
avatarImg image.Image
-
)
-
//png图片的本地保存路径
-
pngpath:="/data/liuhongdi/digv21/static/images/q2.png"
-
//url,创建二维码
-
url:="http://www.baidu.com/"
-
qrCode, err := qrcode.New(url, qrcode.Highest)
-
if err != nil {
-
//return nil, errors.New("创建二维码失败")
-
fmt.Println(err)
-
return
-
}
-
qrCode.DisableBorder = true
-
bgImg = qrCode.Image(256)
-
//icon的路径
-
headpath:="/data/liuhongdi/digv21/static/images/head.jpeg"
-
avatarFile, err = os.Open(headpath)
-
avatarImg, err = jpeg.Decode(avatarFile)
-
//修改图片的大小
-
avatarImg = resize.Resize(40, 40, avatarImg, resize.Lanczos3)
-
-
//得到背景图的大小
-
b := bgImg.Bounds()
-
//居中设置icon到二维码图片
-
offset = image.Pt((b.Max.X-avatarImg.Bounds().Max.X)/2, (b.Max.Y-avatarImg.Bounds().Max.Y)/2)
-
m := image.NewRGBA(b)
-
draw.Draw(m, b, bgImg, image.Point{X: 0, Y: 0,}, draw.Src)
-
draw.Draw(m, avatarImg.Bounds().Add(offset), avatarImg, image.Point{X: 0, Y: 0}, draw.Over)
-
-
//save image
-
errsave:=SaveImage(pngpath,m)
-
if (errsave != nil){
-
fmt.Println(errsave)
-
}
-
//显示图片
-
pngurl:= "/static/images/q2.png"
-
html:="<img src=\'"+pngurl+"\' />"
-
c.Header("Content-Type", "text/html; charset=utf-8")
-
c.String(200, html)
-
return
-
}
-
-
//保存image
-
func SaveImage(p string, src image.Image) error {
-
f, err := os.OpenFile(p, os.O_SYNC|os.O_RDWR|os.O_CREATE, 0666)
-
if err != nil {
-
return err
-
}
-
defer f.Close()
-
ext := filepath.Ext(p)
-
if strings.EqualFold(ext, ".jpg") || strings.EqualFold(ext, ".jpeg") {
-
err = jpeg.Encode(f, src, &jpeg.Options{Quality: 80})
-
} else if strings.EqualFold(ext, ".png") {
-
err = png.Encode(f, src)
-
} else if strings.EqualFold(ext, ".gif") {
-
err = gif.Encode(f, src, &gif.Options{NumColors: 256})
-
}
-
return err
-
}
2,其他相关代码可以访问github查看
四,测试效果
1,查看生成普通的二维码:
访问:
http://127.0.0.1:8080/code/getone
返回:
2,查看生成带icon或头像的二维码:
访问:
http://127.0.0.1:8080/code/geticon
返回:
五,查看库的版本:
-
module github.com/liuhongdi/digv21
-
-
go 1.15
-
-
require (
-
github.com/gin-gonic/gin v1.6.3
-
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
-
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086
-
)
请发表评论