forked from gaelmotte/scan-me
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcameralogic.js
executable file
·179 lines (123 loc) · 4.26 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var fs = require('fs');
var mkdirp = require('mkdirp');
var child_process = require('child_process');
var spawn = child_process.spawn;
var cameraInUse = true;
var clickNB = -1;
exports.definitions = {
uld:50,
ld:100,
sd:200,
hd:300,
uhd:400
}
//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) {
console.log('reading file name : ' + item);
var matches = item.match(regex);
console.log(matches);
if( matches ) {
if(parseInt(matches[1]) > clickNB){
clickNB = parseInt(matches[1]) + 1 ;
console.log('found photo for click : ' + matches[1]);
}
}else{
console.log(matches);
}
});
cameraInUse = false;
console.log("camera initiated with click nb = " + clickNB);
});
exports.getClickNB = function(){
return clickNB;
}
exports.getLastPictureFilename = function(){
return '/img'+(clickNB-1)+'.jpg';
}
exports.snap = function (definition, progress, done){
return scan(definition, progress, done);
}
exports.preview = function (){
return 'preview';
}
function scan(definition, progress, done){
console.log('scan called');
if(!cameraInUse){
console.log('scan commencing');
cameraInUse=true;
var imageIndex = clickNB;
var scanProcess = spawn('scanimage', ['-p','-x', '200', '-y', '200', '-l', '5', '-t', '55', '--format=tiff', '--resolution='+definition]);
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(){
})
});
var progressIndex = 1;
var progressStep = 10;
scanProcess.stderr.on('data', function(data){
//progress is written to stderr.
//let's process it to strip useless text and send it to the progress callbak
var regex = new RegExp("Progress: (\\d+)\..%");
var matches = (""+data).match(regex);
if( matches ) {
var progresspercentage = parseInt(matches[1]);
if(progresspercentage >= progressIndex * progressStep){
//consider trying to stream | convert the unfinished file every 10%
var previewProcess = spawn('sh',['-c','./do_preview.sh ./raw/img'+imageIndex+'.tiff '+ progresspercentage]);
var buffers = [];
previewProcess.stdout.on('data',function(imagedata){
buffers.push(imagedata);
})
previewProcess.stderr.on('data',function(imagedata){
console.log("got error"+ imagedata);
})
previewProcess.stdout.on('end', function(){
progress(progresspercentage, (Buffer.concat(buffers)).toString('base64'));
})
//previewProcess.on('close', function(code,signal){
// progress(progresspercentage, (Buffer.concat(buffers)).toString('base64'));
// console.log("reached progress threshold and conversion done");
// console.log((Buffer.concat(buffers)).toString('base64'));
//})
progressIndex++;
}else{
progress(matches[1], null);
}
}
});
var convertProcess = spawn('convert', ['tiff:-','-quality','60', './pictures/img'+imageIndex+'.jpg']);
scanProcess.stdout.pipe(convertProcess.stdin);
convertProcess.on('close', function(code,signal){
console.log("convert done");
//scanned ended
clickNB++;
cameraInUse = false;
//lets call the done callback with the id of the picture
done('img'+clickNB+'.jpg');
});
return true;
}else{
//cannot scan, it is already in use !
return false;
}
}