鱼C论坛

 找回密码
 立即注册
查看: 4350|回复: 2

[已解决]调用问题

[复制链接]
发表于 2021-7-7 08:38:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
网页如何调用摄像头?
最佳答案
2021-7-9 09:31:26
之前做过一个用vue做的。开摄像头扫码的东西。里面有两个方法是专门用来开摄像头的
代码有点长。。。但是我懒得把东西提出来了
  1. <template>
  2.   <div>
  3.     <input type="file" accept="video/*" capture="camera">  
  4.     <button @click="openSuccess">open success</button>
  5.     <br/>
  6.     {{userMediaMsg}}
  7.     <br/>
  8.     <video ref="showSuccess" src=""></video>
  9.   </div>
  10. </template>
  11. <script>
  12. import { qrcode } from '../static/js/llqrcode';


  13. // 老的浏览器可能根本没有实现 mediaDevices,所以我们可以先设置一个空的对象
  14. if (navigator.mediaDevices === undefined) {
  15.   navigator.mediaDevices = {};
  16. }

  17. // 一些浏览器部分支持 mediaDevices。我们不能直接给对象设置 getUserMedia
  18. // 因为这样可能会覆盖已有的属性。这里我们只会在没有getUserMedia属性的时候添加它。
  19. if (navigator.mediaDevices.getUserMedia === undefined) {
  20.   navigator.mediaDevices.getUserMedia = function(constraints) {

  21.     // 首先,如果有getUserMedia的话,就获得它
  22.     var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

  23.     // 一些浏览器根本没实现它 - 那么就返回一个error到promise的reject来保持一个统一的接口
  24.     if (!getUserMedia) {
  25.       return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
  26.     }else{}

  27.     // 否则,为老的navigator.getUserMedia方法包裹一个Promise
  28.     return new Promise(function(resolve, reject) {
  29.       getUserMedia.call(navigator, constraints, resolve, reject);
  30.     });
  31.   }
  32. }

  33. export default {
  34.     data(){
  35.         let that=this;
  36.         qrcode.callback=function(e){
  37.             if(e==qrcode.errorContent){
  38.                 console.log("读取失败");
  39.             }else{
  40.                 console.log("读取成功")
  41.                 console.log(e);
  42.             }
  43.             that.qrcode_val=e;
  44.         };

  45.         var canvas=document.createElement("canvas"),
  46.             context=canvas.getContext("2d");

  47.         return {
  48.             qrcode_val:'',
  49.             mediaOpts : {
  50.                 audio: false,
  51.                 video: true,
  52.                 video: { facingMode: "environment"} // 或者 "user"
  53.                 // video: { width: 1280, height: 720 }
  54.                 // video: { facingMode: { exact: "environment" } }// 或者 "user"
  55.             },
  56.             canvas:canvas,
  57.             context:context,
  58.             userMediaMsg:'',
  59.             stype:0
  60.         }
  61.     },
  62.     methods: {
  63.         onchang(e){
  64.             var that=e.target;
  65.             console.log(that.files);
  66.             this.decode_file(that.files);
  67.         },
  68.         /**
  69.          * 打开摄像头
  70.          */
  71.         openSuccess(){
  72.             this.setwebcam();
  73.         },
  74.         /**
  75.          * 方法集合
  76.          */
  77.         getSucessFunc(){
  78.             let that=this;
  79.             
  80.             let video=this.$refs["showSuccess"];
  81.             this.userMediaMsg="getSucessFunc";

  82.             function successFunc(stream) {
  83.                 that.userMediaMsg="nndt"
  84.             that.mediaStreamTrack = stream;
  85.             video = document.querySelector('video');
  86.             if ("srcObject" in video) {
  87.             &#160;video.srcObject = stream
  88.             } else {
  89.             &#160;video.src = window.URL && window.URL.createObjectURL(stream) || stream
  90.             }
  91.             video.play();
  92.             }

  93.             function errorFunc(err) {
  94.             alert(err);
  95.             }
  96.             this.userMediaMsg="getSucessFunc F";

  97.             return {success:successFunc,error:errorFunc};
  98.         },
  99.         drawMedia(){
  100.             let video=this.$ref("showSuccess");
  101.             this.canvas.setAttribute("width", video.videoWidth);
  102.             this.canvas.setAttribute("height", video.videoHeight);
  103.             this.context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
  104.             // 返回解码是否成功
  105.             return (this.qrcode_val=qrcode.decode(this.canvas.toDataURL)===qrcode.errorContent);
  106.             
  107.         },
  108.         scanCode(){
  109.             if(this.drawMedia()){
  110.                 // 失败
  111.                 let that=this;
  112.                 setTimeout(function(){
  113.                     that.scanCode();
  114.                 },500);
  115.             }else{
  116.                 // 成功
  117.                 console.log(this.qrcode_val);
  118.                 this.closeMedia();
  119.             }
  120.         },
  121.         closeMedia(){
  122.             let that=this;
  123.             this.mediaStreamTrack.getVideoTracks().forEach(function (track) {
  124.                 track.stop();
  125.                 that.context1.clearRect(0, 0,that.canvas.width, that.canvas.height); // 清除画布
  126.             &#160;});
  127.         },
  128.     // ------------------------------------------------------
  129.         t_qrcode(){
  130.             alert(this.qrcode_val);
  131.             this.$emit('t_qrcode',this.qrcode_val);
  132.         },

  133.         
  134.         setwebcam()
  135.         {
  136.             var that=this;
  137.             var options = true;
  138.             if(navigator.mediaDevices && navigator.mediaDevices.enumerateDevices)
  139.             {
  140.                 try{
  141.                     navigator.mediaDevices.enumerateDevices()
  142.                     .then(function(devices) {
  143.                     devices.forEach(function(device) {
  144.                         if (device.kind === 'videoinput') {
  145.                         if(device.label.toLowerCase().search("back") >-1)
  146.                             options={'deviceId': {'exact':device.deviceId}, 'facingMode':'environment'} ;
  147.                         }
  148.                         console.log(device.kind + ": " + device.label +" id = " + device.deviceId);
  149.                     });
  150.                     that.setwebcam2(options);
  151.                     });
  152.                 }
  153.                 catch(e)
  154.                 {
  155.                     console.log(e);
  156.                 }
  157.             }
  158.             else{
  159.                 console.log("no navigator.mediaDevices.enumerateDevices" );
  160.                 that.setwebcam2(options);
  161.             }
  162.             
  163.         },

  164.         setwebcam2(options)
  165.         {
  166.             var that=this;
  167.             var fncs=this.getSucessFunc();
  168.             var success=fncs.success,
  169.                 error=fncs.error;
  170.             var stype=this.stype;
  171.             function captureToCanvas(){
  172.                 that.scanCode();
  173.             }
  174.             console.log(options);
  175.                 this.userMediaMsg=stype;
  176.             if(stype==1)
  177.             {
  178.                 setTimeout(captureToCanvas, 500);   
  179.                 return;
  180.             }
  181.             var n=navigator;
  182.             this.userMediaMsg="进入cam2";
  183.             if(n.mediaDevices.getUserMedia)
  184.             {
  185.                 that.userMediaMsg="n.mediaDevices.getUserMedia";
  186.                 n.mediaDevices.getUserMedia({video: options, audio: false}).
  187.                     then(function(stream){
  188.                         that.userMediaMsg="cnmdsb";
  189.                         success(stream);
  190.                     }).catch(function(error){
  191.                         that.userMediaMsg=n.mediaDevices.getUserMedia;
  192.                         error(error)
  193.                     });
  194.             }
  195.             else
  196.             if(n.getUserMedia)
  197.             {
  198.                 webkit=true;
  199.                 that.userMediaMsg="n.getUserMedia";
  200.                 n.getUserMedia({video: options, audio: false}, success, error);
  201.             }
  202.             else
  203.             if(n.webkitGetUserMedia)
  204.             {
  205.                 webkit=true;
  206.                 that.userMediaMsg="n.webkitGetUserMedia";
  207.                 n.webkitGetUserMedia({video:options, audio: false}, success, error);
  208.             }
  209.             this.stype=1;
  210.             setTimeout(captureToCanvas, 500);
  211.         }
  212.     }
  213. }
  214. </script>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-7 12:48:51 | 显示全部楼层
这问题问的特别有水准

评分

参与人数 1荣誉 -1 鱼币 -1 收起 理由
高山 -1 -1 请不要灌水

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-9 09:31:26 | 显示全部楼层    本楼为最佳答案   
之前做过一个用vue做的。开摄像头扫码的东西。里面有两个方法是专门用来开摄像头的
代码有点长。。。但是我懒得把东西提出来了
  1. <template>
  2.   <div>
  3.     <input type="file" accept="video/*" capture="camera">  
  4.     <button @click="openSuccess">open success</button>
  5.     <br/>
  6.     {{userMediaMsg}}
  7.     <br/>
  8.     <video ref="showSuccess" src=""></video>
  9.   </div>
  10. </template>
  11. <script>
  12. import { qrcode } from '../static/js/llqrcode';


  13. // 老的浏览器可能根本没有实现 mediaDevices,所以我们可以先设置一个空的对象
  14. if (navigator.mediaDevices === undefined) {
  15.   navigator.mediaDevices = {};
  16. }

  17. // 一些浏览器部分支持 mediaDevices。我们不能直接给对象设置 getUserMedia
  18. // 因为这样可能会覆盖已有的属性。这里我们只会在没有getUserMedia属性的时候添加它。
  19. if (navigator.mediaDevices.getUserMedia === undefined) {
  20.   navigator.mediaDevices.getUserMedia = function(constraints) {

  21.     // 首先,如果有getUserMedia的话,就获得它
  22.     var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

  23.     // 一些浏览器根本没实现它 - 那么就返回一个error到promise的reject来保持一个统一的接口
  24.     if (!getUserMedia) {
  25.       return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
  26.     }else{}

  27.     // 否则,为老的navigator.getUserMedia方法包裹一个Promise
  28.     return new Promise(function(resolve, reject) {
  29.       getUserMedia.call(navigator, constraints, resolve, reject);
  30.     });
  31.   }
  32. }

  33. export default {
  34.     data(){
  35.         let that=this;
  36.         qrcode.callback=function(e){
  37.             if(e==qrcode.errorContent){
  38.                 console.log("读取失败");
  39.             }else{
  40.                 console.log("读取成功")
  41.                 console.log(e);
  42.             }
  43.             that.qrcode_val=e;
  44.         };

  45.         var canvas=document.createElement("canvas"),
  46.             context=canvas.getContext("2d");

  47.         return {
  48.             qrcode_val:'',
  49.             mediaOpts : {
  50.                 audio: false,
  51.                 video: true,
  52.                 video: { facingMode: "environment"} // 或者 "user"
  53.                 // video: { width: 1280, height: 720 }
  54.                 // video: { facingMode: { exact: "environment" } }// 或者 "user"
  55.             },
  56.             canvas:canvas,
  57.             context:context,
  58.             userMediaMsg:'',
  59.             stype:0
  60.         }
  61.     },
  62.     methods: {
  63.         onchang(e){
  64.             var that=e.target;
  65.             console.log(that.files);
  66.             this.decode_file(that.files);
  67.         },
  68.         /**
  69.          * 打开摄像头
  70.          */
  71.         openSuccess(){
  72.             this.setwebcam();
  73.         },
  74.         /**
  75.          * 方法集合
  76.          */
  77.         getSucessFunc(){
  78.             let that=this;
  79.             
  80.             let video=this.$refs["showSuccess"];
  81.             this.userMediaMsg="getSucessFunc";

  82.             function successFunc(stream) {
  83.                 that.userMediaMsg="nndt"
  84.             that.mediaStreamTrack = stream;
  85.             video = document.querySelector('video');
  86.             if ("srcObject" in video) {
  87.             &#160;video.srcObject = stream
  88.             } else {
  89.             &#160;video.src = window.URL && window.URL.createObjectURL(stream) || stream
  90.             }
  91.             video.play();
  92.             }

  93.             function errorFunc(err) {
  94.             alert(err);
  95.             }
  96.             this.userMediaMsg="getSucessFunc F";

  97.             return {success:successFunc,error:errorFunc};
  98.         },
  99.         drawMedia(){
  100.             let video=this.$ref("showSuccess");
  101.             this.canvas.setAttribute("width", video.videoWidth);
  102.             this.canvas.setAttribute("height", video.videoHeight);
  103.             this.context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
  104.             // 返回解码是否成功
  105.             return (this.qrcode_val=qrcode.decode(this.canvas.toDataURL)===qrcode.errorContent);
  106.             
  107.         },
  108.         scanCode(){
  109.             if(this.drawMedia()){
  110.                 // 失败
  111.                 let that=this;
  112.                 setTimeout(function(){
  113.                     that.scanCode();
  114.                 },500);
  115.             }else{
  116.                 // 成功
  117.                 console.log(this.qrcode_val);
  118.                 this.closeMedia();
  119.             }
  120.         },
  121.         closeMedia(){
  122.             let that=this;
  123.             this.mediaStreamTrack.getVideoTracks().forEach(function (track) {
  124.                 track.stop();
  125.                 that.context1.clearRect(0, 0,that.canvas.width, that.canvas.height); // 清除画布
  126.             &#160;});
  127.         },
  128.     // ------------------------------------------------------
  129.         t_qrcode(){
  130.             alert(this.qrcode_val);
  131.             this.$emit('t_qrcode',this.qrcode_val);
  132.         },

  133.         
  134.         setwebcam()
  135.         {
  136.             var that=this;
  137.             var options = true;
  138.             if(navigator.mediaDevices && navigator.mediaDevices.enumerateDevices)
  139.             {
  140.                 try{
  141.                     navigator.mediaDevices.enumerateDevices()
  142.                     .then(function(devices) {
  143.                     devices.forEach(function(device) {
  144.                         if (device.kind === 'videoinput') {
  145.                         if(device.label.toLowerCase().search("back") >-1)
  146.                             options={'deviceId': {'exact':device.deviceId}, 'facingMode':'environment'} ;
  147.                         }
  148.                         console.log(device.kind + ": " + device.label +" id = " + device.deviceId);
  149.                     });
  150.                     that.setwebcam2(options);
  151.                     });
  152.                 }
  153.                 catch(e)
  154.                 {
  155.                     console.log(e);
  156.                 }
  157.             }
  158.             else{
  159.                 console.log("no navigator.mediaDevices.enumerateDevices" );
  160.                 that.setwebcam2(options);
  161.             }
  162.             
  163.         },

  164.         setwebcam2(options)
  165.         {
  166.             var that=this;
  167.             var fncs=this.getSucessFunc();
  168.             var success=fncs.success,
  169.                 error=fncs.error;
  170.             var stype=this.stype;
  171.             function captureToCanvas(){
  172.                 that.scanCode();
  173.             }
  174.             console.log(options);
  175.                 this.userMediaMsg=stype;
  176.             if(stype==1)
  177.             {
  178.                 setTimeout(captureToCanvas, 500);   
  179.                 return;
  180.             }
  181.             var n=navigator;
  182.             this.userMediaMsg="进入cam2";
  183.             if(n.mediaDevices.getUserMedia)
  184.             {
  185.                 that.userMediaMsg="n.mediaDevices.getUserMedia";
  186.                 n.mediaDevices.getUserMedia({video: options, audio: false}).
  187.                     then(function(stream){
  188.                         that.userMediaMsg="cnmdsb";
  189.                         success(stream);
  190.                     }).catch(function(error){
  191.                         that.userMediaMsg=n.mediaDevices.getUserMedia;
  192.                         error(error)
  193.                     });
  194.             }
  195.             else
  196.             if(n.getUserMedia)
  197.             {
  198.                 webkit=true;
  199.                 that.userMediaMsg="n.getUserMedia";
  200.                 n.getUserMedia({video: options, audio: false}, success, error);
  201.             }
  202.             else
  203.             if(n.webkitGetUserMedia)
  204.             {
  205.                 webkit=true;
  206.                 that.userMediaMsg="n.webkitGetUserMedia";
  207.                 n.webkitGetUserMedia({video:options, audio: false}, success, error);
  208.             }
  209.             this.stype=1;
  210.             setTimeout(captureToCanvas, 500);
  211.         }
  212.     }
  213. }
  214. </script>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-20 01:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表