node-red-contrib-usbcamera
Version:
165 lines (151 loc) • 5.97 kB
JavaScript
/**
* 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);
}