Go实战--golang中生成读取二维码(skip2/go-qrcode和boombuler/barcode) - Go语言中文社区

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生成二维码
github地址:https://github.com/skip2/go-qrcode
star 211

获取:

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
star 300

获取:

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)
}

结果:
这里写图片描述
###tuotoo/qrcode识别二维码
github地址:https://github.com/tuotoo/qrcode
star 13
获取:

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)
}

输出:
http://blog.csdn.net/wangshubo1989

这里写图片描述

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/wangshubo1989/article/details/77897363?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522159324394219195265934477%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=159324394219195265934477&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_v2~rank_blog_default-17-77897363.pc_v2_rank_blog_default&utm_term=golang
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-06-27 17:57:53
  • 阅读 ( 1263 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢