-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcameralogic.js
executable file
·117 lines (82 loc) · 2.36 KB
/
cameralogic.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var fs = require('fs');
var mkdirp = require('mkdirp');
var spawn = require('child_process').spawn;
var cameraInUse = false;
var clickNB = -1;
exports.LD = 100;
exports.SD = 300;
exports.HD = 600;
exports.UHD = 1200;
//is directory missing ?
var stats = fs.lstatSync('./raw');
if (!stats.isDirectory()) {
mkdirp('./raw', function(err) {
console.log ('created missing raw folder');
});
}
var stats = fs.lstatSync('./pictures');
if (!stats.isDirectory()) {
mkdirp('./pictures', function(err) {
console.log ('created missing pictures folder');
});
}
//get the current clickNB
console.log('trying to determine clickNB');
fs.readdir( './raw', function(err, list) {
if(err)
throw err;
var regex = new RegExp("img(\\d+)\.tiff");
list.forEach( function(item) {
var matches = item.match(regex);
if( matches ) {
if(matches[1] > clickNB){
clickNB = parseInt(matches[1]) + 1 ;
console.log('found photo for click : ' + matches[1]);
}
}else{
console.log(matches);
}
});
});
exports.snap = function (definition){
return scan(definition);
}
exports.preview = function (){
return 'preview';
}
function scan(definition){
if(!cameraInUse){
cameraInUse=true;
var imageIndex = clickNB;
var scanProcess = spawn('scanimage', ['-x', '300', '-y', '300', '--format=tiff'/*, '>','./raw/img'+imageIndex+'.tiff'*/]);
fs.writeFile('./raw/img'+imageIndex+'.tiff','',function(){
console.log('file initiated');
})
scanProcess.stdout.on('data', function(data){
fs.appendFile('./raw/img'+imageIndex+'.tiff', data, function(){
console.log('worte chunk');
})
});
scanProcess.stderr.on('data', function(data){
console.log('' + data);
})
scanProcess.on('close', function(code,signal){
console.log("scan done");
var convertProcess = spawn('convert', ['-quality','60', './raw/img'+imageIndex+'.tiff', './pictures/img'+imageIndex+'.jpg'])
convertProcess.stderr.on('data', function(data){
console.log('' + data);
})
convertProcess.on('close', function(code,signal){
console.log("convert done");
console.log(arguments)
//scanned ended
clickNB++;
cameraInUse = false;
});
});
return true;
}else{
//cannot scan, it is already in use !
return false;
}
}