-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
99 lines (85 loc) · 2.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* Copyright (C) 2014 Igor C. de Paula
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
var qrcode = function (req, res, next) {
/**
* Return a qrcode for put qrcode image on ejs template
* @returns mixed
*/
req.qrcode = function () {
/**
* Endpoint google to generate a qrcode image
* @type String
*/
var endpoint = "http://chart.googleapis.com/chart?";
/**
* Properties of qrcode image
* @type Array
*/
var properties = [];
properties.push({name: 'cht', value: 'qr'});
/**
* Set dimension of image
* @param int w width
* @param int h height
*/
var setDimension = function (w, h) {
properties.push({name: 'chs', value: w + "x" + w});
}
/**
* Set the charset of data
* @param String charset Charset of data
*/
var setCharset = function (charset) {
properties.push({name: 'choe', value: charset});
}
/**
* Set level of loss of content and margin of image
* @param String level Level of loss
* @param int margin Margin of image
*/
var setCorrectionLevel = function (level, margin) {
properties.push({name: 'chld', value: level + "|" + margin});
}
/**
* Set data to encode in QRCode
* @param {type} data
*/
var setData = function (data) {
properties.push({name: 'chl', value: data});
}
/**
* Generate the URL og QRCode Image
* @returns String
*/
var getImage = function () {
var parameters = properties.map(function (elem) {
return elem.name + "=" + elem.value;
}).join("&");
return (endpoint + parameters);
}
return {
setDimension: setDimension,
setCharset: setCharset,
setCorrectionLevel: setCorrectionLevel,
setData: setData,
getImage: getImage
}
}
next();
}
module.exports = exports = qrcode;