基于 OpenResty 的二维码生成方案
来源:互联网 发布:软件开发学徒骗局 编辑:程序博客网 时间:2024/05/29 15:07
做一个简单二维码生成API,先找找可用的轮子 lua-resty-QRcode 还有 qrencode 。 下面是安装和测试,第二个方案安装和使用更为简单一些,所以使用 qrencode 方案,可以给有兴趣的朋友一些启发。
这两种方案都依赖 libqrencode 还有 libpng , 大概的使用流程是 安装这两个依赖,然后编译上面2个库中的c文件成动态库,openresty中lua调用动态库完成操作。
假设openresty都已经安装好了,我这里是mac上 1.9.7 的环境
安装依赖
libqrencode, libpng 可以源码安装。
ubuntu
sudo apt-get install libqrencode-dev libpng12-dev
CentOS
- https://github.com/harrisj/qrencoder/wiki/qrencode-Dependency-installation-on-CentOS-(RHEL) 很不幸,这里提到了repo已经不在维护了。
yum install libpng-devel#centos7 找个一个包wget http://ftp.riken.jp/Linux/centos/7/os/x86_64/Packages/qrencode-devel-3.4.1-3.el7.x86_64.rpmrpm -ivh qrencode-devel-3.4.1-3.el7.x86_64.rpm
MacOS
brew install libqrencode
自动安装 libpng ,很简单呀
安装lua拓展
!!为了openresty中使用方面一些从新写了一个makefile,放到 https://github.com/orangle/lua-resty-qrencode 上了,可以用这个方案,不是mac系统记得修改 Makefile
git clone https://github.com/vincascm/qrencode.git## 它自己的makefile是使用luarocks安装,这里想自己编译成lua拓展gcc -bundle -undefined dynamic_lookup -lpng -lqrencode -I/usr/local/openresty/luajit/include/luajit-2.1/ qrencode.c -o qrencode.socp test/test.lua .//usr/local/openresty/luajit/bin/luajit test.lua
运行test.lua 可以看到有数据输出
openresty 接口编写
其实test.lua 中已经给出来使用方法了,这里就简单的贴出来代码片段和测试情况。
把lua拓展移动到 openresty lib目录下,当然可以手动指定 lua path 了,这是自己玩怎么方面怎么做了。
cp qrencode.so /usr/local/openresty/lualib/
nginx conf 中的 lua代码
location /qrcode { content_by_lua_block { local qr = require("qrencode") local args = ngx.req.get_uri_args() local text = args.text if text == nil or text== "" then ngx.say('need a text param') ngx.exit(404) end ngx.say(qr { text=text, level="L", kanji=false, ansi=true, size=4, margin=2, symversion=0, dpi=78, casesensitive=true, foreground="48AF6D", background="3FAF6F" }) } }
使用url测试
curl 'http://127.0.0.1:8008/qrcode?text=http://orangleliu.info'
在终端中也可以得到二维码,测试成功。 如果正式使用还需要很多细节和打磨了。
!为了openresty中使用方面一些从新写了一个makefile,放到 https://github.com/orangle/lua-resty-qrencode 上了
—更新—-
上面的代码在命令行显示是可以,但是在浏览器显示就不好使了
location /qrcode { content_by_lua_block { local qr = require("qrencode") local args = ngx.req.get_uri_args() local text = args.text if text == nil or text== "" then ngx.say('need a text param') ngx.exit(404) end ngx.say(qr { text=text, level="L", kanji=false, ansi=false, size=4, margin=2, symversion=0, dpi=78, casesensitive=true, foreground="000000", background="FFFFFF" }) } default_type image/png; add_header Expires "Fri, 01 Jan 1980 00:00:00 GMT"; add_header Pragma "no-cache"; add_header Cache-Control "no-cache, max-age=0, must-revalidate"; }
几个点
- ansi=false 配置为false
- foreground,background 注意色差,黑白最容易显示
- 还有mime以及no cache的设置
- 基于 OpenResty 的二维码生成方案
- OpenResty+lua的方案
- 基于libqrencode的二维码生成
- 基于ZXing的二维码生成与识别
- 基于canvas的二维码邀请函生成插件
- 基于canvas的二维码邀请函生成插件
- 基于canvas的二维码邀请函生成插件
- 基于canvas的二维码邀请函生成插件
- 基于canvas的二维码邀请函生成插件
- 基于canvas的二维码邀请函生成插件
- 基于qrencode-3.4.4的二维码生成
- 基于Zxing的二维码生成和二维码扫描
- 基于Zxing的二维码生成和二维码扫描
- 基于Zxing的二维码生成和二维码扫描
- 基于angular生成二维码
- 基于mysql的全局ID生成方案
- 基于 OpenResty 的服务器架构设计
- OpenResty基于nginx的限流插件
- Android开发常用的工具类
- hdu3461 Code Lock(并查集+快速幂)
- 系列:iOS开发-OC基础介绍
- python的类变量与实例变量以及__dict__属性
- shell script基础
- 基于 OpenResty 的二维码生成方案
- Spring Boot添加Spring Security权限
- 42.验证和授权(2)
- 洛谷 P1217 回文质数 USACO
- C#Windows应用程序开发—>菜单
- left join 和 left outer join 的区别
- string类 stringBuffer stringBuilder
- Linux 消息队列的实现
- lua的堆栈(摘要)