-
Notifications
You must be signed in to change notification settings - Fork 59
/
index.js
68 lines (54 loc) · 1.6 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
/*
* barcode generator
*/
var fs = require('fs'),
path = require('path');
module.exports = function (moduleName, options) {
options.data = options.data || '';
options.barcolor = options.barcolor || '#000';
options.bgcolor = options.bgcolor || '#FFF';
options.w = options.width || 0;
options.h = options.height || 0;
options.type = (options.type) ? options.type.toUpperCase().trim() : 'PNG';
return new Barcode(moduleName, options);
}
function Barcode(moduleName, options) {
this.barcode = require('./lib/' + moduleName.toLowerCase());
this.options = options;
}
Barcode.prototype.getStream = function (callback) {
this.barcode.createCode(this.options, function (err, stream) {
callback(err, stream);
});
}
Barcode.prototype.saveImage = function (outfile, callback) {
this.getStream(function (err, stream) {
if (err) return callback(err, '');
var ws = fs.createWriteStream(outfile);
stream.pipe(ws);
stream.on('end', function () {
callback(false, outfile);
});
stream.on('error', function (err) {
callback(err, '');
});
});
}
Barcode.prototype.getBase64 = function (callback) {
var type = this.options.type || 'PNG';
this.getStream(function (err, stream) {
if (err) return callback(err, '');
var imgBufs = [];
stream.on('data', function (chunk) {
imgBufs.push(chunk);
});
stream.on('end', function () {
var src = 'data:image/' + type + ';base64,';
src += Buffer.concat(imgBufs).toString('base64');
callback(false, src);
});
stream.on('error', function (err) {
callback(err, '')
});
});
}