golang中生成读取二维码(skip2/go-qrcode和boombuler/barcode,tuotoo/qrcode)
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)的更多相关文章
- pbfunc外部函数扩展应用-直接在Datawindow中生成QR二维码,非图片方式
利用pbfunc外部函数在Datawindow中直接生成QR二维码,非图片方式.需要注意以下面几点: Datawindow的DataObject的单位必须为像素(Pixels). Datawindow ...
- laravel中生成支付宝 二维码 扫码支付
文档教程模拟: http://www.023xs.cn/Article/37/laravel5%E9%9B%86%E6%88%90%E6%94%AF%E4%BB%98%E5%AE%9Dalipay%E ...
- ZXing 生成、读取二维码(带logo)
前言 ZXing,一个支持在图像中解码和生成条形码(如二维码.PDF 417.EAN.UPC.Aztec.Data Matrix.Codabar)的库.ZXing(“zebra crossing”)是 ...
- Swift3.0生成二维码、扫描二维码、相册读取二维码,兼容iOS7(结合ZXingObjC)
二维码生成 //MARK: 传进去字符串,生成二维码图片(>=iOS7) text:要生成的二维码内容 WH:二维码高宽 private func creatQRCodeImage(text: ...
- 【java】google的zxing架包生成二维码和读取二维码【可带文字和logo】
承接RC4生成不重复字符串的需求之后,因为优惠码要方便用户使用的缘故,所以思来想去,觉得还是直接生成二维码给用户直接扫比较实用,也不用用户专门记录冗长的优惠码编号. ================= ...
- ZXing生成二维码、读取二维码
使用谷歌的开源包ZXing maven引入如下两个包即可 <dependency> <groupId>com.google.zxing</groupId> & ...
- jquery-qrcode 生成和读取二维码
首先要导入jar包(生成二维码的jar和读取二维码的jar) 生成二维码: package com.imooc.qrcode; import java.awt.Color; import java.a ...
- zxing生成二维码和读取二维码
当然,首先要导入zxing的jar包. 生成二维码代码: package com.imooc.zxing; import java.io.File; import java.nio.file.Path ...
- [AX2012 R3]在SSRS报表中使用QR二维码
AX2012是自带生成QR二维码的类,可以很方便的用在SSRS报表中,下面演示如何在RDP的报表中使用二维码,首先从定义临时表开始: 字段URL是要用于二维码的字符串,QrCode是container ...
随机推荐
- iptables 限制端口
限制端口 #!/bin/bashiptables -P INPUT ACCEPTiptables -P OUTPUT ACCEPTiptables -Fiptables -P INPUT DROPip ...
- 第1001次安kali
第1001次安kali 由于VMware跟win10有仇等原因,最终投入了VirtualBox的怀抱 主要参考这个博客Kali Linux安装教程--VirtualBox 参考Kali安装教程(Vir ...
- TortoiseGit用户名密码的更换方法介绍
http://www.downza.cn/xy/53171.html 有时候因实际需求要更换TortoiseGit用户名密码,怎么办呢?下文就是TortoiseGit用户名密码的更换方法介绍,一起看看 ...
- jstl标签库使用报错index_jsp.java找不到问题
初学jstl的时候记得只需要讲jstl和standard的jar放在lib下面,然后jsp中使用对应导入语法就可以使用标签库了. 但那时候用的是myeclipes,myeclipes的导包的过程记得是 ...
- Python3基础 tuple 使用通配符*进行拆包 简单示例
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- JS 生成随机字符串 随机颜色
使用Math.random()生成随机数 0.7489584611780002数字的.toString(n) 将数字转换为 n 进制的字符串 n取值范围(0~36)"0.vbpjw8lipf ...
- Visual-Based Autonomous Driving Deployment from a Stochastic and Uncertainty-Aware Perspective
张宁 Visual-Based Autonomous Driving Deployment from a Stochastic and Uncertainty-Aware Perspective Le ...
- Android Studio Error:Execution failed for task ':app:compileDebugJavaWithJavac' 根本解决方法
造成这种异常的原因有很多.具体的还是要去终端编译,查看到底是什么地方出错了,然后具体问题具体分析. 终端进入项目的根目录,然后输入命令 gradlew compileDebugJavaWithJava ...
- tomcat 启动和关闭脚本
start.sh #!/bin/sh . ~/.bash_profile echo "" > ${TOMCAT_HOME}/logs/catalina.out; sh ${T ...
- node excel export包导致find函数被覆盖
这个包确实是巨坑:https://github.com/functionscope/Node-Excel-Export 本来是想用来导出Excel的,没想到把Array的find函数能乱了.这种基础函 ...