什么是二维码
二维条码是指在一维条码的基础上扩展出另一维具有可读性的条码,使用黑白矩形图案表示二进制数据,被设备扫描后可获取其中所包含的信息。一维条码的宽度记载着数据,而其长度没有记载数据。二维条码的长度、宽度均记载着数据。二维条码有一维条码没有的“定位点”和“容错机制”。容错机制在即使没有辨识到全部的条码、或是说条码有污损时,也可以正确地还原条码上的信息。
生成二维码
使用Go语言编程时,生成任意内容的二维码是非常方便的,因为我们有go-qrcode这个库。该库的源代码托管在github上,大家可以下载使用 https://github.com/skip2/go-qrcode。
这个库的使用很简单,假如我要以我的博客网站地址 http://www.flysnow.org 生成一张256*256的图片,可以使用如下代码:
1
2
3
4
5
|
import "github.com/skip2/go-qrcode"
func main() {
qrcode.WriteFile("http://www.flysnow.org/",qrcode.Medium,256,"./blog_qrcode.png")
}
|
这样我们运行代码的时候,就在当前目录下,生成一张256*256的二维码,扫描后可以看到内容是http://www.flysnow.org/。
1
|
func WriteFile(content string, level RecoveryLevel, size int, filename string) error
|
WriteFile函数的原型定义如上,它有几个参数,大概意思如下:
- content表示要生成二维码的内容,可以是任意字符串。
- level表示二维码的容错级别,取值有Low、Medium、High、Highest。
- size表示生成图片的width和height,像素单位。
- filename表示生成的文件名路径。
RecoveryLevel类型其实是个int,它的定义和常量如下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
)
|
RecoveryLevel越高,二维码的容错能力越好。
生成二维码图片字节
有时候我们不想直接生成一个PNG文件存储,我们想对PNG图片做一些处理,比如缩放了,旋转了,或者网络传输了等,基于此,我们可以使用Encode函数,生成一个PNG 图片的字节流,这样我们就可以进行各种处理了。
1
|
func Encode(content string, level RecoveryLevel, size int) ([]byte, error)
|
用法和WriteFile函数差不多,只不过返回的是一个[]byte字节数组,这样我们就可以对这个字节数组进行处理了。
自定义二维码
除了以上两种快捷方式,该库还为我们提供了对二维码的自定义方式,比如我们可以自定义二维码的前景色和背景色等。qrcode.New函数可以返回一个*QRCode
,我们可以对*QRCode
设置,实现对二维码的自定义。
比如我们设置背景色为绿色,前景色为白色的二维码
1
2
3
4
5
6
7
8
9
10
|
func main() {
qr,err:=qrcode.New("http://www.flysnow.org/",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")
}
}
|
指定*QRCode
的BackgroundColor和ForegroundColor即可。然后调用WriteFile方法生成这个二维码文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
}
|
以上QRCode的这些字段都是可以设置的,这样我们就可以灵活自定义二维码了。
生成白边较少的二维码
获取:
1
|
go get github.com/boombuler/barcode
|
生成二维码图片:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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)
}
|
识别二维码
获取:
1
|
go get github.com/tuotoo/qrcode
|
读取二维码图片:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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)
}
|
转载:
https://www.jianshu.com/p/fc7bd26abcda
https://www.flysnow.org/2017/09/29/go-qrcode.html
https://www.flysnow.org/2017/09/29/go-qrcode.html