导入qrcode
go get github.com/skip2/go-qrcode
import (
"github.com/skip2/go-qrcode"
"image/color"
"log"
"net/http"
)
生成二维码文件
// Level L: 7% error recovery.
// Low
// Level M: 15% error recovery. Good default choice.
// Medium
// Level Q: 25% error recovery.
// High
// Level H: 30% error recovery.
// Highest
_ = qrcode.WriteFile("https://www.baidu.com/", qrcode.Low, 500, "qrcode-Low.png")
浏览器输出二维码
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
f, err := qrcode.Encode("https://www.baidu.com/", qrcode.Highest, 300)
if err != nil {
log.Println(err.Error())
return
}
w.Write(f)
})
_ = http.ListenAndServe("127.0.0.1:9090", nil)
生成不同颜色二维码
// 背景颜色
b := color.RGBA{
R: 255,
G: 255,
B: 255,
A: 255,
}
// 二维码颜色
c := color.RGBA{
R: 177,
G: 7,
B: 7,
A: 255,
}
_ = qrcode.WriteColorFile("https://www.baidu.com/", qrcode.Medium, 300, b, c, "qrcode-color.png")
网友评论