forked from soldair/node-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qrcode.js
194 lines (152 loc) · 4.31 KB
/
qrcode.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
*copyright Ryan Day 2012
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* this is the main server side application file for node-qrcode.
* these exports use serverside canvas api methods for file IO and buffers
*
*/
var QRCodeLib = require(__dirname+'/lib/qrcode-draw')
, terminalRender = require(__dirname+'/lib/termialrender.js')
, Canvas = require('canvas')
, fs = require('fs');
var QRCodeDraw = QRCodeLib.QRCodeDraw,
QRCode = QRCodeLib.QRCode;
//EXPORTS
//
// breaking change to 0.1 this used to be an instance. now it returns the constructor.
//
exports.QRCodeDraw = QRCodeDraw;
//
// export error correct levels.
//
exports.errorCorrectLevels = QRCodeLib.QRErrorCorrectLevel;
/*
* provide an api to return the max characters allowed for given dimensions, and miniumum error correction level
* the qr code library will always use the maximum error correction level for the given numbar of chars constrained by size
*/
exports.getMaxChars = function(minErrorCorrectionLevel,width,moduleScale){
//TODO THIS NEEDS TO WORK
console.log('this doesnt work yet. comming soon =)');
};
// returns Canvas Object with qr code drawn on it
/*
* String text, optional Object options, Function callback
*/
var draw = exports.draw = function(text,options,cb){
var args = Array.prototype.slice.call(arguments);
cb = args.pop();
if(typeof cb != 'function') {
throw new TypeError('last argument must be a function');
}
text = args.shift();
options = args.shift()||{};
var textKeys = {'minimum':"L",'medium':"M",'high':"Q",'max':"H"}
if(options.errorCorrectLevel) {
var ec = options.errorCorrectLevel;
if(textKeys[ec]){
options.errorCorrectLevel = textKeys[ec];
}
}
//-------------^^^^^^^^^
//NOTE the width and height are determined from within the qr code lib and are not configurable from the outside yet
var drawInstance = new QRCodeDraw();
drawInstance.draw(new Canvas(200,200),text,options,function(error,canvas){
cb(error,canvas)
});
};
//returns data uri for drawn qrcode png
exports.toDataURL = exports.toDataURI = function(text,options,cb){
if(typeof options == 'function') {
cb = options;
options = {};
}
draw(text,options,function(error,canvas){
if(error) {
cb(error);
} else {
canvas.toDataURL(cb);
}
});
}
//synchronous PNGStream
exports.toPNGStream = function (text, WSpath, options,cb) {
if(typeof options == 'function'){
cb = options;
options = {};
}
var out = fs.createWriteStream(WSpath);
draw(text, options, function (error,canvas) {
if(error) {
cb(error,'');
} else {
stream = canvas.createPNGStream();
}
stream.pipe(out);
stream.on('end', function () {
cb(error,'');
});
stream.pipe(out);
});
return out;
}
//returns bytes written to file
exports.save = function(path,text,options,cb){
if(typeof options == 'function'){
cb = options;
options = {};
}
draw(text, options, function(error,canvas){
var fd,buf,fdAndBuf = function(){
fs.write(fd, buf, 0, buf.length, 0, function(error,written){
fs.close(fd);
if(cb) cb(error,written);
});
};
//run non dependent async calls at the same time ish
canvas.toBuffer(function(error, _buf){
if(error) return cb(error,0);
buf = _buf
if(fd) fdAndBuf();
});
fs.open(path, 'w', 0666, function(err,_fd){
if(error) return cb(error,0);
fd = _fd
if(buf) fdAndBuf();
});
});
};
//
//this returns an array of points that have either a 0 or 1 value representing 0 for light and 1 for dark
//these values include points in the white edge of the qrcode because that edge is actually part of the spec
//
exports.drawBitArray = function(text,options,cb){
if(typeof options == 'function'){
cb = options;
options = {};
}
var drawInstance = new QRCodeDraw();
drawInstance.drawBitArray(text,function(error,bits,width){
cb(error,bits,width);
});
}
//
// draw qr in your terminal!
//
exports.drawText = function(text,options,cb){
if(typeof options == 'function'){
cb = options;
options = {};
}
var drawInstance = new QRCodeDraw();
drawInstance.drawBitArray(text,function(error,bits,width){
if (!error) {
var code = terminalRender.renderBits(bits,width);
cb(error,code);
} else {
cb(error,null);
}
});
}