-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.js
94 lines (79 loc) · 2.69 KB
/
server.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
// http://restify.com/
var restify = require('restify');
var fs = require('fs');
var server = restify.createServer({
certificate: fs.readFileSync('key-cert.pem'),
key: fs.readFileSync('key.pem'),
name: 'MyApp',
});
var dbr = require('../../index.js');
// Get a license key from https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform
dbr.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==");
var barcodeTypes = dbr.barcodeTypes;
server.use(restify.bodyParser());
server.get(/.*/, restify.serveStatic({
directory: __dirname,
default: 'index.html'
}));
server.post('/dbr', function create(req, res, next) {
// console.log(req.body);
// fs.writeFile("log.txt", req.body, function (err) {
// if (err) {
// return console.log(err);
// }
// console.log("Base64 saved!");
// });
// var data = new Buffer(req.body, 'base64');
// var file = __dirname + '/' + new Date().getTime() + '.png';
// var file = __dirname + '/' + 'test.png';
// fs.writeFile(file, data, function (err) {
// dbr.decodeFileAsync(file, barcodeTypes, function (err, msg) {
// fs.unlink(file, function (err) {
// console.log('Removed cached: ' + file);
// });
// var final_result = "";
// var hasResult = false;
// for (index in msg) {
// hasResult = true;
// var result = msg[index]
// final_result += "value: " + result['value'] + "; ";
// console.log(result['format']);
// console.log(result['value']);
// console.log("##################");
// }
// if (!hasResult) {
// final_result = "No barcode detected. ";
// }
// final_result += new Date();
// res.send(200, final_result);
// next();
// }, "");
// });
// let image = str(req.body);
// console.log(image);
let data = Buffer.from(req.body);
dbr.decodeBase64Async(data.toString('utf8'), barcodeTypes, function (err, msg) {
// fs.unlink(file, function (err) {
// console.log('Removed cached: ' + file);
// });
var final_result = "";
var hasResult = false;
for (let index in msg) {
hasResult = true;
var result = msg[index]
final_result += result['value'] + "; ";
console.log(result['format']);
console.log(result['value']);
console.log("##################");
}
if (!hasResult) {
final_result = "No barcode detected. ";
}
final_result += new Date();
res.send(200, final_result);
next();
}, "");
});
server.listen(2018, function () {
console.log('REST server listening at %s', server.url);
});