nodejs实现超简单生成二维码的方法

看吧,山上的矮松越发的青黑,树尖上顶着一髻儿白花,好像日本的看护妇。山尖全白了,给蓝天镶上一道银边。山坡上,有的地方雪厚点,有的地方草色还露着。

本文实例讲述了nodejs实现超简单生成二维码的方法。分享给大家供大家参考,具体如下:

一开始使用node-qrcode(https://github.com/soldair/node-qrcode),结果安装的时候需要安装python,且不支持python3.0以上,安装python2.0的时候又需要安装其他的环境,所以放弃了。

最后选择了一个小众的插件qr-image(https://github.com/alexeyten/qr-image)

前台页面如下

views/index.ejs

<!DOCTYPE html>
<html>
<head>
  <title><%= title %></title>
  <link rel='stylesheet' rel="nofollow noopener noreferrer" href='/stylesheets/style.css'/>
</head>
<body>
<h1><%= title %></h1>
<img src="/create_qrcode?text=http://blog.csdn.net/fo11ower"/>
</body>
</html>

后端代码:

routes/index.js

var qr = require('qr-image')
router.get('/', function (req, res, next) {
  res.render('index', {title: 'Express'});
});
router.get('/create_qrcode', function (req, res, next) {
  var text = req.query.text;
  try {
    var img = qr.image(text,{size :10});
    res.writeHead(200, {'Content-Type': 'image/png'});
    img.pipe(res);
  } catch (e) {
    res.writeHead(414, {'Content-Type': 'text/html'});
    res.end('<h1>414 Request-URI Too Large</h1>');
  }
})

最后效果

PS:这里再为大家推荐两款二维码相关在线工具供大家参考使用:

在线生成二维码工具(加强版)
http://tools.haodaima.com/transcoding/jb51qrcode

在线二维码解码识别工具
http://tools.haodaima.com/transcoding/trans_qrcode

希望本文所述对大家nodejs程序设计有所帮助。

到此这篇关于nodejs实现超简单生成二维码的方法就介绍到这了。如果心胸不似海,又怎能有海一样的事业。更多相关nodejs实现超简单生成二维码的方法内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
nodejs二进制与Buffer的介绍与使用

nodejs中各种加密算法的实现详解

独立部署小程序基于nodejs的服务器过程详解

NodeJs 模仿SIP话机注册的方法

nodejs实现获取本地文件夹下图片信息功能示例