Go生成二维码

使用github.com/skip2/go-qrcode包

import (
	"fmt"
	qrcode "github.com/skip2/go-qrcode"
	"image/color"
)
func  main(){
	qrcode.WriteFile("https://blog.csdn.net/wzb_wzt",qrcode.Medium,256,"./QRcode/demo.png")
	
	//自定义设置二维码样式
	//生成一个qrcode struct指针
	code, _ := qrcode.New("https://blog.csdn.net/wzb_wzt", qrcode.High)
	//背景色是绿色
	code.BackgroundColor=color.RGBA{50,205,50,255}
	//前置色是白色
	code.ForegroundColor=color.White
	code.DisableBorder=false
	//生成
	code.WriteFile(256,"./QRcode/demo2.png")

}

WriteFile函数的参数介绍:
1. content表示要生成二维码的内容,可以是任意字符串。
2. level表示二维码的容错级别,取值有Low、Medium、High、Highest。
3. size表示生成图片的width和height,像素单位。
4. filename表示生成的文件名路径。

RecoveryLevel类型其实是个int,它的定义和常量如下(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
)

encode, _ := qrcode.Encode("https://blog.csdn.net/wzb_wzt", qrcode.Medium, 256)
fmt.Println(encode)

Encode 用法和WriteFile函数差不多,只不过返回的是一个[]byte字节数组,这样我们就可以对这个字节数组进行处理了。

//自定义设置二维码样式
//生成一个*QRCode指针
code, _ := qrcode.New("https://blog.csdn.net/wzb_wzt", qrcode.High)
//背景色是绿色
code.BackgroundColor=color.RGBA{50,205,50,255}
//前置色是白色
code.ForegroundColor=color.White
//去除边框
code.DisableBorder=true
//生成
code.WriteFile(256,"./QRcode/demo2.png")

*QRCode 其他参数参考qrcode包中的具体用法:
type QRCode struct {
Content string
Level RecoveryLevel
VersionNumber int
ForegroundColor color.Color
BackgroundColor color.Color
DisableBorder bool
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值