二维码/条形码的生成
This commit is contained in:
@@ -11,12 +11,25 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"errors"
|
||||
"image/png"
|
||||
"os"
|
||||
"encoding/base64"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/qr"
|
||||
"github.com/boombuler/barcode/code128"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxCodeWidth = 400
|
||||
MaxCodeHeight = 400
|
||||
CodeTypeQr = "qr"
|
||||
CodeTypeBar = "bar"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -451,3 +464,82 @@ func CaculateSkuEarningPrice(shopPrice, salePrice int64, storePayPercentage int)
|
||||
earningPrice = earningPrice * int64(storePayPercentage) / 100
|
||||
return earningPrice
|
||||
}
|
||||
|
||||
func File2BytesByFileName(filename string) ([]byte, error) {
|
||||
file, err := os.Open(filename)
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return File2BytesByFile(file)
|
||||
}
|
||||
|
||||
func File2BytesByFile(file *os.File) ([]byte, error) {
|
||||
stats, err := file.Stat()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data := make([]byte, stats.Size())
|
||||
_, err = file.Read(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func CreateCodeImage(qrCode barcode.Barcode) (fileName string, err error) {
|
||||
fileName = "qrcode.png"
|
||||
file, err := os.Create(fileName)
|
||||
defer file.Close()
|
||||
if err == nil {
|
||||
err = png.Encode(file, qrCode)
|
||||
}
|
||||
|
||||
return fileName, err
|
||||
}
|
||||
|
||||
func GetImgBase64(qrCode barcode.Barcode) (imgBase64 string, err error) {
|
||||
fileName, err := CreateCodeImage(qrCode)
|
||||
if err == nil {
|
||||
fileData, err := File2BytesByFileName(fileName)
|
||||
if err == nil {
|
||||
imgBase64 = "data:image/png;base64," + base64.StdEncoding.EncodeToString(fileData)
|
||||
}
|
||||
}
|
||||
|
||||
return imgBase64, err
|
||||
}
|
||||
|
||||
func CreateQrAndBarCode(width, height int, codetype, srcData string) (imgBase64 string, err error) {
|
||||
if width > MaxCodeWidth {
|
||||
width = MaxCodeWidth
|
||||
}
|
||||
if height > MaxCodeHeight {
|
||||
height = MaxCodeHeight
|
||||
}
|
||||
|
||||
if codetype == CodeTypeQr {
|
||||
qrCode, err := qr.Encode(srcData, qr.M, qr.Auto)
|
||||
if err == nil {
|
||||
qrCode, err = barcode.Scale(qrCode, width, height)
|
||||
if err == nil {
|
||||
imgBase64, err = GetImgBase64(qrCode)
|
||||
}
|
||||
}
|
||||
} else if codetype == CodeTypeBar {
|
||||
cs, err := code128.Encode(srcData)
|
||||
if err == nil {
|
||||
qrCode, err2 := barcode.Scale(cs, width, height)
|
||||
if err = err2; err == nil {
|
||||
imgBase64, err = GetImgBase64(qrCode)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = errors.New(fmt.Sprintf("未知编码类型:%s", codetype))
|
||||
}
|
||||
|
||||
return imgBase64, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user