forked from makaishi2/node-red-contrib-usbcamera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusbcamera.js
164 lines (151 loc) · 5.83 KB
/
usbcamera.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
/**
* Copyright 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Authors:
* - M. Akaishi
**/
module.exports = function(RED) {
// "use strict";
// required modules
const fsext = require("fs-extra");
const uuidv4 = require('uuid/v4');
const os = require('os');
const NodeWebcam = require( "node-webcam" );
// default consts
const settings = RED.settings;
const uuid = uuidv4();
const homedir = os.homedir();
const defdir = homedir + "/Pictures/";
// USB Camera Take Photo Node
function USBCameraTakePhotoNode(config) {
// Create this node
RED.nodes.createNode(this,config);
this.name = config.name;
var node = this;
node.on('input', function(msg) {
console.log("camera node start");
console.log(config);
// required value
var filemode = config.filemode;
var filedefpath = config.filedefpath; // 1 or 0
var fileformat = config.fileformat;
var resolution = config.resolution;
// option value
var filepath = config.filepath + "/";
var filename = config.filename;
var filefqn;
var opts = {
width: 640,
height: 320,
quality: 100,
delay: 2,
saveShots: true,
output: "jpeg",
device: false,
callbackReturn: "location",
verbose: false
};
node.status({fill:"green",shape:"dot",text:"connected"});
// Overwrite message value
if((msg.filemode) && (msg.filemode !== "")) {
filemode = msg.filemode;
}
if ((msg.filename) && (msg.filename.trim() !== "")) {
filename = msg.filename;
}
if ((msg.fileformat) && (msg.fileformat.trim() !== "")) {
fileformat = msg.fileformat;
}
if ((msg.resolution) && (msg.resolution !== "")) {
resolution = msg.resolution;
}
// Overwrite as default path
if ( filedefpath === "1" ) {
filepath = defdir;
}
// Buffer Mode
if ( filemode === "0" ) {
opts.callbackReturn = 'buffer';
}
// Generate File Name
if (filemode === "2" || !(filename) ) {
filename = "pic_" + uuid + '.jpg';
}
filefqn = filepath + filename;
if ( filemode !== "0" ) {
var chk_result = fsext.existsSync(filepath);
if ( !chk_result ) {
fsext.mkdirsSync(filepath);
console.log( "created dir: " + filepath);
}
}
if (RED.settings.verbose) { node.log("usbcamera:"+filefqn); }
// Set Resolution
if (resolution == "1") {
opts.width = 320; opts.height = 240;
} else if (resolution == "2" ) {
opts.width = 640; opts.height = 480;
} else if (resolution == "3" ) {
opts.width = 800; opts.height = 600;
} else if (resolution == "4" ) {
opts.width = 1024; opts.height = 768;
} else if (resolution == "5" ) {
opts.width = 1920; opts.height = 1080;
} else {
opts.width = 2592; opts.height = 1944;
}
if (RED.settings.verbose) { node.log(opts); }
//Creates webcam instance
console.log(opts);
var retry = 0;
(function () {
var callee = arguments.callee;
var Webcam = NodeWebcam.create( opts );
//Will automatically append location output type
Webcam.capture( filefqn, function( err, data ) {
if ( err ) {
retry = retry + 1;
if ( retry < 5 ) {
console.log( "retry: " + retry);
setTimeout(callee, 2000);
return;
}
console.error("USB Camera error: "+ err);
msg.payload = {error: "USB Camera error: "+ err};
node.status({});
node.send(msg);
return;
}
msg.payload = data;
if ( filemode === "0" ) {
msg.filename = "";
msg.fileformat = "";
msg.filepath = "";
console.log("USB Camera: written to buffer with success! retry=" + retry);
} else {
msg.filename = filename;
msg.filepath = filepath;
msg.fileformat = fileformat;
console.log("USB Camera: written to " + data + " with success! retry=" + retry);
}
node.status({});
node.send(msg);
Webcam.clear();
console.log( "camera node end");
});
})();
});
}
RED.nodes.registerType("usbcamera",USBCameraTakePhotoNode);
}