go 二维码生成与解析

package main

import (
	"fmt"
	"github.com/makiuchi-d/gozxing"
	gq "github.com/makiuchi-d/gozxing/qrcode"
	"github.com/outakujo/utils"
	"github.com/skip2/go-qrcode"
	"image"
	"os"
)

func main() {
	qr := &MQr{}
	s, e := qr.Parse("qrcode.png")
	fmt.Println(s, e)
	qr.Gen("你好,world!", "qrcode.png")
}

type MQr struct {
}

func (*MQr) Gen(data, file string) {
	code, e := qrcode.New(data, qrcode.Medium)
	utils.PanicError(e)
	code.DisableBorder = true
	utils.PanicError(e)
	e = code.WriteFile(256, file)
	utils.PanicError(e)
}

func (*MQr) Parse(file string) (string, error) {
	fi, _ := os.Open(file)
	img, _, _ := image.Decode(fi)
	bmp, _ := gozxing.NewBinaryBitmapFromImage(img)
	qrReader := gq.NewQRCodeReader()
	result, e := qrReader.Decode(bmp, nil)
        if e != nil {
	    return "", e
	}
	return result.String(), e
}

 所生成的二维码

go 二维码生成与解析_第1张图片

 

你可能感兴趣的:(go)