-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·84 lines (74 loc) · 2.41 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var express = require('express'),
Jimp = require("jimp");
var app = express();
app.listen(3000, function() {
console.log('Example app listening on port 3000!');
});
app.get('/photo', function(req, res) {
var takePhoto = new run_cmd(
'gphoto2',
['--set-config', 'shutterspeed=46', '--capture-image-and-download', '--force-overwrite'],
commandOutputAppender,
cbRunCommandResizeAndManupulateTheImage);
});
app.get('/test', function(req, res) {
var testCommand = new run_cmd(
'cat',
['mock'],
commandOutputAppender,
cbRunCommandResizeAndManupulateTheImage);
});
function commandOutputAppender(commander, buffer) {
commander.stdout += buffer.toString();
}
function cbRunCommandResizeAndManupulateTheImage() {
var result = takePhoto.stdout.replace('undefined', '');
resizePhoto(
parsePathOfPhoto(result)[2],
function(err, image) {
if (err) {
console.error(err);
res.send('error');
} else {
var imgSrc = 'data:' + Jimp.MIME_JPEG + ';base64,' + new Buffer(image, 'binary').toString('base64');
res.send('<html><head></head><body><img style="width: 40%;" src="' + imgSrc + '" ></body></html>');
}
});
}
function cbResizePhotoAddToHtml(err, image) {
if (err) {
console.error(err);
res.send('error');
} else {
var imgSrc = 'data:' + Jimp.MIME_JPEG + ';base64,' + new Buffer(image, 'binary').toString('base64');
res.send('<html><head></head><body><img style="width: 40%;" src="' + imgSrc + '" ></body></html>');
}
}
function resizePhoto(path, cb) {
Jimp
.read(path)
.then(function(image) {
image
.scale(0.5)
.quality(85)
.getBuffer(Jimp.MIME_JPEG, function(err, buffer) {
if (err) throw err;
cb(null, buffer);
});
}).catch(function(err) {
cb(err, null);
});
}
function parsePathOfPhoto(resultOfGphoto2) {
var regex = /(Saving file as )(.*.jpg)(\n)/;
return regex.exec(resultOfGphoto2);
}
function run_cmd(cmd, args, cb, end) {
var spawn = require('child_process').spawn,
child = spawn(cmd, args),
me = this;
child.stdout.on('data', function(buffer) {
cb(me, buffer)
});
child.stdout.on('end', end);
}