利用Go產生二維條碼圖檔

使用套件

// 介面

func WriteFile(content string, level RecoveryLevel, size int, filename string) error

// RecoveryLevel 結構,數值越高表示容錯率越高
type RecoveryLevel int
const (
    // Level L: 7% error recovery.
    Low RecoveryLevel = iota
    // Level M: 15% error recovery. Good default choice.
    Medium
    // Level Q: 25% error recovery.
    High
    // Level H: 30% error recovery.
    Highest
)

在當前目錄下產生 256*256 的條碼

import "github.com/skip2/go-qrcode"
func main() {
    qrcode.WriteFile("http://www.flysnow.org/",qrcode.Medium,256,"./blog_qrcode.png")
}

Encode(content string, level RecoveryLevel, size int) ([]byte, error)

自訂 qr code

func main() {
    qr,err:=qrcode.New("http://justjii.justdrink.com.tw/",qrcode.Medium)
    if err != nil {
        log.Fatal(err)
    } else {
        qr.BackgroundColor = color.RGBA{50,205,50,255}  // 綠色
        qr.ForegroundColor = color.White  // 白色
        qr.WriteFile(256,"./blog_qrcode.png")
    }
}
func New(content string, level RecoveryLevel) (*QRCode, error)
// A QRCode represents a valid encoded QRCode.
type QRCode struct {
    // Original content encoded.
    Content string
    // QR Code type.
    Level         RecoveryLevel
    VersionNumber int
    // User settable drawing options.
    ForegroundColor color.Color
    BackgroundColor color.Color
}