forked from soldair/node-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqrcode.js
More file actions
149 lines (117 loc) · 3.5 KB
/
Copy pathqrcode.js
File metadata and controls
149 lines (117 loc) · 3.5 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
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
/*
*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;
/*
* 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()||{};
//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,cb){
draw(text,function(error,canvas){
if(error) {
cb(error,'');
} else {
canvas.toDataURL(cb);
}
});
}
//synchronous 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.pipe(out);
stream.on('end', function () {
cb(error,'');
});
stream.pipe(out);
});
return out;
}
//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(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,cb){
var drawInstance = new QRCodeDraw();
drawInstance.drawBitArray(text,function(error,bits,width){
cb(error,bits,width);
});
}
exports.drawText = function(text,cb){
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);
}
});
}