2017-05-27 25 views
0

我试图从相机模块中连续传输相机图片。你知道一旦前一次产卵完成后如何发射另一颗卵子? 目前我收到一个错误,“startCamera”函数在子进程执行“exit”事件后没有找到。如何在出口循环中运行nodejs产卵

var spawn = require('child_process').spawn; 
module.exports = { 
cameraProcess: null, 
startCamera: function() { 
    var isWin = /^win/.test(process.platform); 
    var path = require('path'); 
    if (isWin) { 
     this.cameraProcess = spawn(path.join(__dirname, 'vfwgrab', 'VFWGrab.exe')); 
    } else { 
     var args = ["-w", "640", "-h", "480", "-o", "./public/stream/stream.jpg", "-t", "999999999", "-tl", "2000"]; 
     this.cameraProcess = spawn('raspistill', args); 
    } 
    this.cameraProcess.on('exit', this.loopCamera); 
}, 
loopCamera: function (code) { 
    this.startCamera(); //start the next spawn once the previous finished 
} 
}; 

回答

0

现在我解决了这个问题,将函数移出出口,然后在出口中引用它们。请让我知道这是否是推荐的方式。

var spawn = require('child_process').spawn; 
var cameraProcess = null; 
var startCamera = function() { 
    var isWin = /^win/.test(process.platform); 
    var path = require('path'); 
    if (isWin) { 
    this.cameraProcess = spawn(path.join(__dirname, 'vfwgrab', 'VFWGrab.exe')); 
    } else { 
    var args = ["-w", "640", "-h", "480", "-o", "./public/stream/stream.jpg", "-t", "999999999", "-tl", "2000"]; 
    this.cameraProcess = spawn('raspistill', args); 
    } 
    this.cameraProcess.on('exit', loopCamera); 
} 
var loopCamera = function (code) { 
    startCamera(); 
} 
module.exports = { 
    startCamera: startCamera, 
};