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. zabbix-常规配置

    zabbix server:cat zabbix_server.confLogFile=/data/log/zabbix_server.logLogFileSize=250DebugLevel=3Pi ...

  2. iptables 限制端口

    限制端口 #!/bin/bashiptables -P INPUT ACCEPTiptables -P OUTPUT ACCEPTiptables -Fiptables -P INPUT DROPip ...

  3. java并发编程(二)synchronized

    参考文章: http://blog.csdn.net/javazejian/article/details/72828483http://ifeve.com/java-synchronized/htt ...

  4. Gamma阶段第三次scrum meeting

    每日任务内容 队员 昨日完成任务 明日要完成的任务 张圆宁 #91 用户体验与优化https://github.com/rRetr0Git/rateMyCourse/issues/91(持续完成) # ...

  5. Spring Boot通过Configuration配置多数据源

    本文结合SpringBoot + MyBatis + MySql进行多数据源配置,DataSource信息采用自定义dataSource.properties进行配置. 1.文件结构如下: 2.1 p ...

  6. How to Use Convolutional Neural Networks for Time Series Classification

    How to Use Convolutional Neural Networks for Time Series Classification 2019-10-08 12:09:35 This blo ...

  7. mod_spatialite.so.7

  8. Python开源项目Top30

    原文地址:https://www.cnblogs.com/stoker/p/9101825.html No 1:Home-assistant (v0.6+) 基于Python 3的开源家庭自动化平台[ ...

  9. layui中的table中toolbar自定义过程

    自己挖过的坑需要自己来填. layui的table默认表头工具栏右边有3个操作,分别是过滤字段.导出excel.打印功能. 在js中代码添加toolbar即可实现上面的效果: table.render ...

  10. [原]JSON 字符串(值)做判断,比较 “string ”

    现在我这样一个json字符串: char* cjson = "{\"code\": \"200\", \"code2\": 200 ...