Example-1

Tools

Goole
GoDoc

qrcode

Package qr encodes QR codes.

code.google.com/p/rsc/qr

main

package main

import (
    "bytes"
    "code.google.com/p/rsc/qr"
    "flag"
    "fmt"
    "image"
    "image/png"
    "os"
)

func main() {
    var imgName string
    flag.StringVar(&imgName, "n", "QR", "name")

    flag.Parse()

    if flag.NArg() == 0 {
        fmt.Println("未輸入編碼內容")
        os.Exit(1)
    }

    str := flag.Args()[0]
    fileName := imgName + ".png"

    code, err := qr.Encode(str, qr.H)

    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    imgByte := code.PNG()

    img, _, _ := image.Decode(bytes.NewReader(imgByte))

    out, err := os.Create(fileName)

    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    err = png.Encode(out, img)

    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    fmt.Println("QR code generated and saved to " + fileName)
}

ex1.png