Golang Gin实践 连载十二 生成二维码
作者:互联网
前言
本章节将实现如下功能:
- 生成二维码
- 合并海报(背景图+二维码)
实现
首先,你需要在 App 配置项中增加二维码及其海报的存储路径,我们约定配置项名称为 QrCodeSavePath,值为 qrcode/
生成二维码
安装:go get -u github.com/boombuler/barcode
工具包
考虑生成二维码这一动作贴合工具包的定义,且有公用的可能性,新建 pkg/qrcode/qrcode.go 文件,写入内容:
点击查看代码
package qrcode
import (
"gin_log/pkg/file"
"gin_log/pkg/setting"
"gin_log/pkg/util"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
"image/jpeg"
"os"
)
type QrCode struct {
URL string
Width int
Height int
Ext string
Level qr.ErrorCorrectionLevel
Mode qr.Encoding
}
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,
}
}
func GetQrCodePath() string {
return setting.AppSetting.QrCodeSavePath
}
func GetQrCodeFullPath() string {
return setting.AppSetting.RuntimeRootPath + setting.AppSetting.QrCodeSavePath
}
func GetQrCodeFullUrl(name string) string {
return setting.AppSetting.PrefixUrl + "/" + GetQrCodePath() + name
}
func GetQrCodeFileName(value string) string {
return util.EncodeMD5(value)
}
func (q *QrCode) GetQrCodeExt() string {
return q.Ext
}
func (q *QrCode) CheckEncode(path string) bool {
src := path + GetQrCodeFileName(q.URL) + q.GetQrCodeExt()
return !file.CheckExist(src)
}
func (q *QrCode) Encode(path string) (string, string, error) {
name := GetQrCodeFileName(q.URL) + q.GetQrCodeExt()
src := path + name
if !file.CheckExist(src) {
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 := file.Open(src, os.O_CREATE|os.O_RDWR, 0755)
if err != nil {
return "", "", err
}
defer func() {
_ = f.Close()
}()
err = jpeg.Encode(f, code, nil)
if err != nil {
return "", "", err
}
}
return name, path, nil
}
// Options are the encoding parameters.
// Quality ranges from 1 to 100 inclusive, higher is better.
type Options struct {
Quality int
}
### 路由方法
1、第一步
在 routers/api/v1/article.go 新增 GenerateArticlePoster 方法用于接口开发
2、第二步
在 routers/router.go 的 apiv1 中新增 apiv1.POST("/articles/poster/generate", v1.GenerateArticlePoster) 路由
3、第三步
修改 GenerateArticlePoster 方法,编写对应的生成逻辑,如下:
const QR_CODE = "https://github.com/mayanan-py-go/gin-blog"
// GenerateArticlePoster 生成文章二维码
func GenerateArticlePoster(c *gin.Context) {
appG := app.Gin{C: c}
qrCode := qrcode.NewQrCode(QR_CODE, 300, 300, qrcode.EXT_JPG, qr.L, qr.Auto)
path := qrcode.GetQrCodeFullPath()
_, _, err := qrCode.Encode(path)
if err != nil {
logging.Warn(err)
appG.Response(http.StatusOK, e.ERROR, nil)
return
}
appG.Response(http.StatusOK, e.SUCCESS, nil)
}
### 验证
通过post访问:`http://127.0.0.1:8000/api/v1/articles/poster/generate`
返回值:
{
"code": 200,
"data": null,
"msg": "ok"
}
通过检查两个点确定功能是否正常,如下:
1、访问结果是否 200
2、本地目录是否成功生成二维码图片
![](https://www.icode9.com/i/l/?n=22&i=blog/1696528/202209/1696528-20220915150817391-465762880.png)
标签:return,连载,err,nil,Golang,二维码,func,Gin,string 来源: https://www.cnblogs.com/mayanan/p/16696716.html