生成二维码的开源项目可谓是琳琅满目,SwetakeQRCode、BarCode4j、Zxing,前端有JQuery-qrcode,同样能实现生成二维码。选择Zxing的原因可能是对 Google 公司的信赖和个人崇拜吧。
参考:GitHub - makiuchi-d/gozxing: Port of ZXing (https://github.com/zxing/zxing) core to pure Go.
package main
import (
"fmt"
"image"
_ "image/jpeg"
"os"
"github.com/makiuchi-d/gozxing"
"github.com/makiuchi-d/gozxing/qrcode"
)
func main() {
// open and decode image file
file, _ := os.Open("qrcode.jpg")
img, _, _ := image.Decode(file)
// prepare BinaryBitmap
bmp, _ := gozxing.NewBinaryBitmapFromImage(img)
// decode image
qrReader := qrcode.NewQRCodeReader()
result, _ := qrReader.Decode(bmp, nil)
fmt.Println(result)
}
"github.com/tuotoo/qrcode" 库 斜的解析不出来
fi, err := os.Open("qrcode.png")
if err != nil{
logger.Println(err.Error())
return
}
defer fi.Close()
qrmatrix, err := qrcode.Decode(fi)
if err != nil{
logger.Println(err.Error())
return
}
logger.Println(qrmatrix.Content)