/
node-qrDecode.js
102 lines (88 loc) · 2.71 KB
/
node-qrDecode.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
'use strict';
const jsQr = require('jsqr');
const jimp = require('jimp');
const request = require('request');
const getQrCode = (url) => {
return new Promise((resolve, reject) => {
let requestSettings = {
url: url,
method: 'GET',
encoding: null //to get a binary buffer
};
request(requestSettings, (error, response, body) => {
if(error){
reject(error);
}
else{
resolve(response);
}
});
});
}
const decodeImageFromBuffer = (msg, buffer, node, RED) => {
//analyse QrCode
jimp.read(buffer, (err, image) => {
if(err){
return node.error(err);
}
//Decode
let jsonQrData = jsQr.decodeQRFromImage(image.bitmap.data, image.bitmap.width, image.bitmap.height);
//safe parsing of qrCode message to json object
try {
msg[node.output] = JSON.parse(jsonQrData);
} catch (ex) {
//if it is an URL
if(jsonQrData.match(/^http(s)?:\/\//)){
//Save the full URL
let urlTab, params, paramsTab, url = jsonQrData;
msg[node.output] = {};
msg[node.output].url = url;
urlTab = url.split('?');
if(urlTab.length > 1){
params = urlTab[1];
msg[node.output].params = {};
paramsTab = params.split('&');
//Save every params of the URL in an object
for(let e of paramsTab){
let [name,value]=e.split('=');
msg[node.output].params[name]=value;
}
}
}
else{
msg[node.output] = jsonQrData;
}
}
return node.send(msg);
});
}
// --------------------------------------------------------------------------
// NODE-RED
// --------------------------------------------------------------------------
module.exports = function (RED) {
const node_qrDecode = function (config) {
RED.nodes.createNode(this, config);
this.path = config.path || "prompt.attachments[0].contentUrl";
this.output = config.output || "payload";
this.source = config.source || "URL";
this.on('input', (msg) => {
//get data (URL or Buffer)
let data = RED.util.evaluateNodeProperty(this.path,"msg",this,msg);
// source is a URL
if(this.source === "URL"){
//load image
getQrCode(data).then((response) => {
let imgBuffer = response.body;
decodeImageFromBuffer(msg,imgBuffer,this,RED);
}).catch((err) => {
return this.error(err);
});
}
// source is a Buffer
else if(this.source === "Buffer"){
decodeImageFromBuffer(msg,data,this,RED);
}
});
}
RED.nodes.registerType('qrDecode', node_qrDecode);
}