一,安裝用到的庫
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
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,演示項目的相關信息
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
-
)
