1. let { execSync, exec } = remote.require("child_process");
    2. /**
    3. * 初始化相机驱动
    4. */
    5. async function initCameraDirve() {
    6. let checkFlags = {
    7. installedFsWebCam: false,
    8. installedLuvcView: false
    9. };
    10. // debugger;
    11. await new Promise(function(resolve) {
    12. let subProcess = exec("dpkg -l|grep 'fswebcam'", function(
    13. err,
    14. checkResult
    15. ) {
    16. // debugger;
    17. subProcess.kill();
    18. if (checkResult && checkResult.toLowerCase().indexOf("fswebcam") >= 0) {
    19. checkFlags.installedFsWebCam = true;
    20. }
    21. resolve();
    22. });
    23. });
    24. await new Promise(function(resolve) {
    25. let subProcess = exec("dpkg -l|grep 'luvcview'", function(
    26. err,
    27. checkResult
    28. ) {
    29. // debugger;
    30. subProcess.kill();
    31. if (checkResult && checkResult.toLowerCase().indexOf("luvcview") >= 0) {
    32. checkFlags.installedLuvcView = true;
    33. }
    34. resolve();
    35. });
    36. });
    37. // debugger;
    38. if (checkFlags.installedFsWebCam && checkFlags.installedLuvcView) {
    39. console.log("检测到相机相关驱动已全部安装,下次可直接测试,无需再次初始化");
    40. return;
    41. }
    42. let promiseObj = null;
    43. let promiseObjList = [];
    44. // this.Loading("正在安装相机驱动,请确保外网畅通,否则将安装失败");
    45. if (!checkFlags.installedFsWebCam) {
    46. let installResult = await new Promise(function(resolve) {
    47. let subProcess = null;
    48. subProcess = exec("sudo apt-get install fswebcam", function(err, out) {
    49. console.log("fswebcam install pId=" + subProcess.pid);
    50. subProcess.kill();
    51. if (!err) {
    52. resolve({ result: true, msg: "", name: "fswebcam" });
    53. } else {
    54. console.log("install fswebcam fail:" + err);
    55. resolve({ result: false, msg: err, name: "fswebcam" });
    56. }
    57. });
    58. });
    59. if (installResult.result) {
    60. console.log(`${installResult.name}:安装成功`);
    61. } else {
    62. ktLog.e(`${installResult.name}:安装失败(${installResult.msg})`);
    63. }
    64. }
    65. if (!checkFlags.installedLuvcView) {
    66. let installResult = await new Promise(function(resolve) {
    67. exec("sudo apt-get install luvcview", function(err, out) {
    68. if (!err) {
    69. resolve({ result: true, msg: "", name: "luvcview" });
    70. } else {
    71. console.log("install luvcview fail:" + err);
    72. resolve({ result: false, msg: err, name: "luvcview" });
    73. }
    74. });
    75. });
    76. if (installResult.result) {
    77. this.NotifySuccess(`${installResult.name}:安装成功`);
    78. } else {
    79. ktLog.e(`${installResult.name}:安装失败(${installResult.msg})`);
    80. }
    81. }
    82. // $this.HideLoading();
    83. console.log("相机所有相关驱动安装完成,下次可直接测试,无需再次初始化");
    84. return;
    85. }
    86. /**
    87. * 相机
    88. */
    89. class camera {
    90. constructor() {
    91. //相机是否开启
    92. this.cameraProcess = null;
    93. }
    94. /**
    95. * 开启相机
    96. */
    97. openCamera() {
    98. if (this.cameraProcess) {
    99. this.stopCamera();
    100. }
    101. if (process.platform == "linux" && process.arch == "arm") {
    102. this.cameraProcess = exec("luvcview -s 1280x720");
    103. }
    104. }
    105. /**
    106. * 拍照
    107. * @param {*} path 文件保存路径
    108. */
    109. takePhotos(path) {
    110. let $this = this;
    111. if (process.platform == "linux" && process.arch == "arm") {
    112. if (this.cameraProcess) {
    113. this.stopCamera();
    114. setTimeout(function() {
    115. console.log("执行linux命令进行拍照,路径》》" + path);
    116. execSync(`fswebcam -r 1920*1080 ${path}`);
    117. }, 1000);
    118. } else {
    119. try {
    120. console.log("执行linux命令进行拍照,路径" + path);
    121. let re = execSync(`fswebcam -r 1920*1080 ${path}`);
    122. console.log("执行linux命令进行拍照结果" + re);
    123. } catch (error) {
    124. console.log("执行linux命令进行拍照,错误" + error);
    125. }
    126. }
    127. }
    128. }
    129. /**
    130. * 关闭相机
    131. */
    132. stopCamera() {
    133. try {
    134. if (this.cameraProcess) {
    135. //this.cameraProcess.kill('SIGTERM');
    136. execSync(`sudo kill -9 ${this.cameraProcess.pid}`);
    137. execSync(`sudo killall luvcview`);
    138. }
    139. } catch (e) {
    140. console.log(`Camera close exception:${e.message}`);
    141. } finally {
    142. this.cameraProcess = null;
    143. }
    144. }

    坑点:
    1.拍照没有生成图片,请检查摄像机是否是好的
    2.终端输入ls /dev/video*
    查看是否有/dev/video0
    如果没有
    找不到/dev/video0设备,但是树莓派官方自带的raspistill却能够用起来,

    树莓派中的camera module是放在/boot/目录下以固件的形式加载的,不是一个标准的v4l2的摄像头ko驱动,所以加载起来之后会找不到/dev/video0的设备节点,这是因为这个驱动是在底层的,v4l2这个驱动框架还没有加载,

    1. 所以要在配置文件,也就是 /etc/下面的modules-load.d / rpi-camera.conf (如果没有则新建文件,这个文件名可能不是叫modules-load.d/rpi-camera.conf,也有可能直接就是/etc/modules,)
    2. 打开文件在里面添加一行 <br /> bcm2835-v4l2<br />(v4i2不是v412别写错了)<br /> 这句话意思是在系统启动之后会加载这个文件中模块名,这个模块会在树莓派系统的/lib/modules/xxx/xxx/xxx下面,

    添加之后重启系统,就会在/dev/下面发现video0设备节点了。

    3.没有打开Camera选项
    输入sudo raspi-config先在interfacing option里把camera接口打开(enable)。