nodejs簡單二維碼生成

一開始使用node-qrcodehttps://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' 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>');
    }
})

最後效果
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章