nodejs實現超簡單生成二維碼的方法
本文實例講述了nodejs實現超簡單生成二維碼的方法。分享給大家供大家參考,具體如下:
一開始使用node-qrcode(https://github.com/soldair/node-qrcode),結果安裝的時候需要安裝python,且不支持python3.0以上,安裝python2.0的時候又需要安裝其他的環(huán)境,所以放棄了。
最后選擇了一個小眾的插件qr-image(https://github.com/alexeyten/qr-image)
前臺頁面如下
views/index.ejs
<!DOCTYPE html> <html> <head> <title><%= title %></title> <link rel='stylesheet' 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.jb51.net/transcoding/jb51qrcode
在線二維碼解碼識別工具
http://tools.jb51.net/transcoding/trans_qrcode
希望本文所述對大家nodejs程序設計有所幫助。
相關文章
Node.js?內置模塊fs文件系統(tǒng)操作示例詳解
這篇文章主要為大家介紹了Node.js?內置模塊fs文件系統(tǒng)操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02