let { execSync, exec } = remote.require("child_process");
/**
* 初始化相机驱动
*/
async function initCameraDirve() {
let checkFlags = {
installedFsWebCam: false,
installedLuvcView: false
};
// debugger;
await new Promise(function(resolve) {
let subProcess = exec("dpkg -l|grep 'fswebcam'", function(
err,
checkResult
) {
// debugger;
subProcess.kill();
if (checkResult && checkResult.toLowerCase().indexOf("fswebcam") >= 0) {
checkFlags.installedFsWebCam = true;
}
resolve();
});
});
await new Promise(function(resolve) {
let subProcess = exec("dpkg -l|grep 'luvcview'", function(
err,
checkResult
) {
// debugger;
subProcess.kill();
if (checkResult && checkResult.toLowerCase().indexOf("luvcview") >= 0) {
checkFlags.installedLuvcView = true;
}
resolve();
});
});
// debugger;
if (checkFlags.installedFsWebCam && checkFlags.installedLuvcView) {
console.log("检测到相机相关驱动已全部安装,下次可直接测试,无需再次初始化");
return;
}
let promiseObj = null;
let promiseObjList = [];
// this.Loading("正在安装相机驱动,请确保外网畅通,否则将安装失败");
if (!checkFlags.installedFsWebCam) {
let installResult = await new Promise(function(resolve) {
let subProcess = null;
subProcess = exec("sudo apt-get install fswebcam", function(err, out) {
console.log("fswebcam install pId=" + subProcess.pid);
subProcess.kill();
if (!err) {
resolve({ result: true, msg: "", name: "fswebcam" });
} else {
console.log("install fswebcam fail:" + err);
resolve({ result: false, msg: err, name: "fswebcam" });
}
});
});
if (installResult.result) {
console.log(`${installResult.name}:安装成功`);
} else {
ktLog.e(`${installResult.name}:安装失败(${installResult.msg})`);
}
}
if (!checkFlags.installedLuvcView) {
let installResult = await new Promise(function(resolve) {
exec("sudo apt-get install luvcview", function(err, out) {
if (!err) {
resolve({ result: true, msg: "", name: "luvcview" });
} else {
console.log("install luvcview fail:" + err);
resolve({ result: false, msg: err, name: "luvcview" });
}
});
});
if (installResult.result) {
this.NotifySuccess(`${installResult.name}:安装成功`);
} else {
ktLog.e(`${installResult.name}:安装失败(${installResult.msg})`);
}
}
// $this.HideLoading();
console.log("相机所有相关驱动安装完成,下次可直接测试,无需再次初始化");
return;
}
/**
* 相机
*/
class camera {
constructor() {
//相机是否开启
this.cameraProcess = null;
}
/**
* 开启相机
*/
openCamera() {
if (this.cameraProcess) {
this.stopCamera();
}
if (process.platform == "linux" && process.arch == "arm") {
this.cameraProcess = exec("luvcview -s 1280x720");
}
}
/**
* 拍照
* @param {*} path 文件保存路径
*/
takePhotos(path) {
let $this = this;
if (process.platform == "linux" && process.arch == "arm") {
if (this.cameraProcess) {
this.stopCamera();
setTimeout(function() {
console.log("执行linux命令进行拍照,路径》》" + path);
execSync(`fswebcam -r 1920*1080 ${path}`);
}, 1000);
} else {
try {
console.log("执行linux命令进行拍照,路径" + path);
let re = execSync(`fswebcam -r 1920*1080 ${path}`);
console.log("执行linux命令进行拍照结果" + re);
} catch (error) {
console.log("执行linux命令进行拍照,错误" + error);
}
}
}
}
/**
* 关闭相机
*/
stopCamera() {
try {
if (this.cameraProcess) {
//this.cameraProcess.kill('SIGTERM');
execSync(`sudo kill -9 ${this.cameraProcess.pid}`);
execSync(`sudo killall luvcview`);
}
} catch (e) {
console.log(`Camera close exception:${e.message}`);
} finally {
this.cameraProcess = null;
}
}
坑点:
1.拍照没有生成图片,请检查摄像机是否是好的
2.终端输入ls /dev/video*
查看是否有/dev/video0
如果没有
找不到/dev/video0设备,但是树莓派官方自带的raspistill却能够用起来,
树莓派中的camera module是放在/boot/目录下以固件的形式加载的,不是一个标准的v4l2的摄像头ko驱动,所以加载起来之后会找不到/dev/video0的设备节点,这是因为这个驱动是在底层的,v4l2这个驱动框架还没有加载,
所以要在配置文件,也就是 /etc/下面的modules-load.d / rpi-camera.conf (如果没有则新建文件,这个文件名可能不是叫modules-load.d/rpi-camera.conf,也有可能直接就是/etc/modules,)
打开文件在里面添加一行 <br /> bcm2835-v4l2<br />(v4i2不是v412别写错了)<br /> 这句话意思是在系统启动之后会加载这个文件中模块名,这个模块会在树莓派系统的/lib/modules/xxx/xxx/xxx下面,
添加之后重启系统,就会在/dev/下面发现video0设备节点了。
3.没有打开Camera选项
输入sudo raspi-config先在interfacing option里把camera接口打开(enable)。