golang如何生成與讀取二維碼
生成二維碼
skip2/go-qrcode生成二維碼
獲仁帷:
go get skip2/go-qrcode
生成二維碼圖片:
package main
import qrcode "github.com/skip2/go-qrcode"
import "fmt"
func main() {
err := qrcode.WriteFile("http://blog.csdn.net/wangshubo1989", qrcode.Medium, 256, "qr.png")
if err != nil {
fmt.Println("write error")
}
}
boombuler/barcode生成二維碼
github地址:https://github.com/boombuler/barcode
獲取:
go get github.com/boombuler/barcode
生成二維碼圖片:
package main
import (
"image/png"
"os"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
)
func main() {
qrCode, _ := qr.Encode("http://blog.csdn.net/wangshubo1989", qr.M, qr.Auto)
qrCode, _ = barcode.Scale(qrCode, 256, 256)
file, _ := os.Create("qr2.png")
defer file.Close()
png.Encode(file, qrCode)
}
識(shí)別二維碼
github地址:https://github.com/tuotoo/qrcode
獲取:
go get github.com/tuotoo/qrcode
讀取二維碼圖片:
package main
import (
"fmt"
"os"
"github.com/tuotoo/qrcode"
)
func main() {
fi, err := os.Open("qrcode.png")
if err != nil {
fmt.Println(err.Error())
return
}
defer fi.Close()
qrmatrix, err := qrcode.Decode(fi)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(qrmatrix.Content)
}