1 引言

在github上有好用golan二维码生成和读取库,两个生成二维码的qrcode库和一个读取qrcode库。

skip2/go-qrcode生成二维码,github地址:https://github.com/skip2/go-qrcode

boombuler/barcode生成二维码,github地址:https://github.com/boombuler/barcode 

tuotoo/qrcode解析二维码,github地址:https://github.com/tuotoo/qrcode

2 代码

import (
"image/png"
"os"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
"github.com/skip2/go-qrcode"
qrcodeReader "github.com/tuotoo/qrcode"
"fmt"
) func main() { content := "https://www.cnblogs.com/fanbi"
size := 200 //Skip2
dest := "qrcode.png"
CreateQRCodeBySkip2(content, qrcode.Medium, size, dest)
fmt.Println("QRCodeBySkip2 content",ReadQRCode(dest)) //Boombuler
dest2 := "qrcode2.png"
CreateQRCodeByBoombuler(content, qr.M, size, dest2)
fmt.Println("QRCodeBySBoombule content",ReadQRCode(dest2)) //输出
//QRCodeBySkip2 content https://www.cnblogs.com/fanbi
//QRCodeBySBoombule content https://www.cnblogs.com/fanbi } func CreateQRCodeBySkip2(content string, quality qrcode.RecoveryLevel, size int, dest string) (err error) {
err = qrcode.WriteFile(content, quality, size, dest)
return
} func CreateQRCodeByBoombuler(content string, quality qr.ErrorCorrectionLevel, size int, dest string) (err error) {
qrCode, err := qr.Encode(content, quality, qr.Auto)
if err != nil {
return
} // Scale the barcode to 200x200 pixels
qrCode, err = barcode.Scale(qrCode, size, size)
if err != nil {
return
} // create the output file
file, err := os.Create(dest)
if err != nil {
return
} defer file.Close()
// encode the barcode as png
err = png.Encode(file, qrCode)
if err != nil {
return
} return
} func ReadQRCode(filename string) (content string) {
fi, err := os.Open(filename)
if err != nil {
fmt.Println(err.Error())
return
}
defer fi.Close()
qrmatrix, err := qrcodeReader.Decode(fi)
if err != nil {
fmt.Println(err.Error())
return
}
return qrmatrix.Content
}

效果图:

qrcode.png

qrcode2.png

说明:第二种生成的二维码会更好,图片的四周白边占比小。

3 参考

https://blog.csdn.net/wangshubo1989/article/details/77897363  

golang中生成读取二维码(skip2/go-qrcode和boombuler/barcode,tuotoo/qrcode)的更多相关文章

  1. pbfunc外部函数扩展应用-直接在Datawindow中生成QR二维码,非图片方式

    利用pbfunc外部函数在Datawindow中直接生成QR二维码,非图片方式.需要注意以下面几点: Datawindow的DataObject的单位必须为像素(Pixels). Datawindow ...

  2. laravel中生成支付宝 二维码 扫码支付

    文档教程模拟: http://www.023xs.cn/Article/37/laravel5%E9%9B%86%E6%88%90%E6%94%AF%E4%BB%98%E5%AE%9Dalipay%E ...

  3. ZXing 生成、读取二维码(带logo)

    前言 ZXing,一个支持在图像中解码和生成条形码(如二维码.PDF 417.EAN.UPC.Aztec.Data Matrix.Codabar)的库.ZXing(“zebra crossing”)是 ...

  4. Swift3.0生成二维码、扫描二维码、相册读取二维码,兼容iOS7(结合ZXingObjC)

    二维码生成 //MARK: 传进去字符串,生成二维码图片(>=iOS7) text:要生成的二维码内容 WH:二维码高宽 private func creatQRCodeImage(text: ...

  5. 【java】google的zxing架包生成二维码和读取二维码【可带文字和logo】

    承接RC4生成不重复字符串的需求之后,因为优惠码要方便用户使用的缘故,所以思来想去,觉得还是直接生成二维码给用户直接扫比较实用,也不用用户专门记录冗长的优惠码编号. ================= ...

  6. ZXing生成二维码、读取二维码

    使用谷歌的开源包ZXing maven引入如下两个包即可 <dependency>   <groupId>com.google.zxing</groupId>  & ...

  7. jquery-qrcode 生成和读取二维码

    首先要导入jar包(生成二维码的jar和读取二维码的jar) 生成二维码: package com.imooc.qrcode; import java.awt.Color; import java.a ...

  8. zxing生成二维码和读取二维码

    当然,首先要导入zxing的jar包. 生成二维码代码: package com.imooc.zxing; import java.io.File; import java.nio.file.Path ...

  9. [AX2012 R3]在SSRS报表中使用QR二维码

    AX2012是自带生成QR二维码的类,可以很方便的用在SSRS报表中,下面演示如何在RDP的报表中使用二维码,首先从定义临时表开始: 字段URL是要用于二维码的字符串,QrCode是container ...

随机推荐

  1. ansible-playbook-常用

    创建软链:file: - name: create link hosts: "{{hosts_ip}}" tasks: - name: create link file: src= ...

  2. linux 挂载windows ntfs 分区 -- centos 安装ntfs-3g

    安装fuse 下载: wget http://nchc.dl.sourceforge.net/project/fuse/fuse-2.X/2.9.2/fuse-2.9.2.tar.gz 安装: tar ...

  3. Hawq架构

    Hawq采用分层架构,将MPP shared-nothing的计算层架在HDFS之上. Hawq集群中有三种角色:master, namenode和segment hosts. 1.Master负责认 ...

  4. Mercurial 安装及使用

      版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/moonspiritacm/articl ...

  5. NSIS安装或卸载时检查程序是否正在运行

    转载:https://www.cnblogs.com/z5337/p/4766415.html 转载:https://www.gongzi.org/nsisbuildqqstop.html 转载:ht ...

  6. QWidget添加带有图片的QPushButton,布局QGridLayout

    QWidget* w = new QWidget(this); w->setGeometry(10,20,400,300); QVBoxLayout* layout = new QVBoxLay ...

  7. CISCO DHCP全攻略详解

    原文链接地址:https://bbs.51cto.com/thread-800321-1.html 最近看到大家经常由于DHCP的问题犯愁, 为了让大家更明白的了解DHCP并且会配置,特此发这个贴 相 ...

  8. Collections--ChainMap

    一个 ChainMap 类是为了将多个映射快速的链接到一起,这样它们就可以作为一个单元处理.它通常比创建一个新字典和多次调用 update() 要快很多. class collections.Chai ...

  9. springboot docker 部署

    1.新建一个最简单的springboot项目 https://code.aliyun.com/859143303/hello-world.git 2.src/main/docker下新建Dockerf ...

  10. CSS继承控制:inherit、initial和unset

    CSS里有三种常用的属性值继承方式:inherit,initial和unset.我们用一个简单的例子来演示一下: <ul style="color: green;"> ...