1. 程式人生 > >Go實戰 golang中生成讀取二維碼 skip2/go qrcode和boombuler/barcode

Go實戰 golang中生成讀取二維碼 skip2/go qrcode和boombuler/barcode

                     

生命不止,繼續go go go!!!

這裡介紹一下,golang如何生成二維碼,當然是面向github程式設計了。

QRCode

百度百科: QR Code碼,是由Denso公司於1994年9月研製的一種矩陣二維碼符號,它具有一維條碼及其它二維條碼所具有的資訊容量大、可靠性高、可表示漢字及圖象多種文字資訊、保密防偽性強等優點。

wiki: QR code (abbreviated from Quick Response Code) is the trademark for a type of matrix barcode (or two-dimensional barcode) first designed for the automotive industry in Japan. A barcode is a machine-readable optical label that contains information about the item to which it is attached. A QR code uses four standardized encoding modes (numeric, alphanumeric, byte/binary, and kanji) to efficiently store data; extensions may also be used

skip2/go-qrcode生成二維碼

獲取:

go get skip2/go-qrcode
  • 1

生成二維碼圖片:

package mainimport 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")    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

結果: 這裡寫圖片描述

boombuler/barcode生成二維碼

獲取:

go get github.com/boombuler/barcode
  • 1

生成二維碼圖片:

package mainimport (    "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)}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

結果: 這裡寫圖片描述

tuotoo/qrcode識別二維碼

go get github.com/tuotoo/qrcode
  • 1

讀取二維碼圖片:

package mainimport (    "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)}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

這裡寫圖片描述