forked from soldair/node-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqrcode.js
More file actions
74 lines (61 loc) · 1.61 KB
/
Copy pathqrcode.js
File metadata and controls
74 lines (61 loc) · 1.61 KB
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
var QRCodeDraw = require(__dirname+'/lib/qrcode-draw')
, Canvas = require('canvas')
, fs = require('fs');
//EXPORTS
exports.QRCodeDraw = QRCodeDraw;
// returns Canvas Object with qr code drawn on it
var draw = exports.draw = function(text,cb){
//NOTE the width and height are determined from within the qr code lib and are not configurable from the outside yet
QRCodeDraw.draw(new Canvas(200,200),text,function(error,canvas){
cb(error,canvas)
});
}
//returns data uri for drawn qrcode png
var dataURL = exports.toDataURL = function(text,cb){
draw(text,function(error,canvas){
if(error) {
cb(error,'');
} else {
canvas.toDataURL(cb);
}
});
}
//synchronous PNGStream
var pngStream = exports.toPNGStream = function (text, WSpath, cb) {
var out = fs.createWriteStream(WSpath);
draw(text,function (error,canvas) {
if(error) {
cb(error,'');
} else {
stream = canvas.createPNGStream();
}
});
stream.on('data', function (chunk) {
out.write(chunk);
});
stream.on('end', function () {
console.log('saved png');
});
}
//returns bytes written to file
exports.save = function(path,text,cb){
draw(text,function(error,canvas){
var fd,buf,fdAndBuf = function(){
fs.write(fd, buf, 0, buf.length, 0, function(err,written){
fs.close(fd);
cb(err,written);
});
};
//run async calls at the same time ish so they can take advantage of the others idle time
canvas.toBuffer(function(err, _buf){
if(err) return cb(err,0);
buf = _buf
if(fd) fdAndBuf();
});
fs.open(path, 'w', 0666, function(err,_fd){
if(err) return cb(err,0);
fd = _fd
if(buf) fdAndBuf();
});
});
}