index.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>H5摄像头(新版浏览器https)(兼容老版浏览器)</title>
  6. </head>
  7. <body>
  8. <!-- 说明:将网页更改为https访问才行 否者报错:
  9. NotSupportedError Only secure origins are allowed (see: https://goo.gl/Y0ZkNV). -->
  10. <!-- video用于显示媒体设备的视频流,自动播放;属性:https://zhuanlan.zhihu.com/p/535917105 -->
  11. <video id="video" autoplay webkit-playsinline="true" playsinline="true"
  12. style="width: 20px;height: 400px;transform: rotateY(180deg);display:none;"></video>
  13. <!-- transform: rotateY(180deg); 镜像解决 -->
  14. <!-- 可以通过画布canvas渲染,获取使用默认的video也行 -->
  15. <!-- <canvas id="canvas" width="500" height="400" style="transform: rotateY(180deg);"></canvas> -->
  16. <canvas id="canvas"></canvas>
  17. <!-- 拍照按钮 -->
  18. <div><button id="capture" style="color: blue;">拍照</button></div>
  19. <!-- 描绘video截图 -->
  20. <img id="img" style="color: red;" alt="" src="">
  21. <script type="text/javascript">
  22. var video = document.getElementById("video");
  23. var img = document.getElementById("img");
  24. var canvas = document.getElementById("canvas");
  25. var context = canvas.getContext("2d");
  26. var v_t_w = 200,
  27. v_t_h = 200;
  28. // 访问用户媒体设备的兼容方法
  29. function getUserMedia(constrains, successFun, errorFun) {
  30. //like12 modified,20210628,bug,navigator.mediaDevices为空会导致后面卡死
  31. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  32. //最新标准API(新版浏览器https)
  33. navigator.mediaDevices.getUserMedia(constrains).then(successFun).catch(errorFun);
  34. } else if (navigator.webkitGetUserMedia) {
  35. //like12 modified,20210628,不是这种调用方法 应该为后者
  36. //webkit内核浏览器(老版浏览器)
  37. //navigator.webkitGetUserMedia(constrains).then(successFun).catch(errorFun);
  38. navigator.webkitGetUserMedia({
  39. "video": true
  40. }, successFun, errorFun);
  41. } else if (navigator.mozGetUserMedia) {
  42. //Firefox浏览器
  43. navagator.mozGetUserMedia(constrains).then(successFun).catch(errorFun);
  44. } else if (navigator.getUserMedia) {
  45. //旧版API
  46. navigator.getUserMedia(constrains).then(successFun).catch(errorFun);
  47. }
  48. }
  49. // 成功的回调函数
  50. function successFun(stream) {
  51. //like12 modifed,20210618,Chrome升级后,新版本不再支持该用法
  52. //摄像头视频流显示报错Failed to execute 'createObjectURL' on 'URL'
  53. //研究即时通信的过程中需要调用摄像头,发现报错,原来是谷歌弃用了这个方法,根据官方提示修改即可
  54. //所以原先的代码:video.src = URL.createObjectURL(stream);
  55. //需要被修改为:video.srcObject = stream;
  56. //(新版浏览器https)
  57. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  58. video.srcObject = stream;
  59. }
  60. //(老版浏览器)
  61. else {
  62. //兼容webkit内核浏览器
  63. var CompatibleURL = window.URL || window.webkitURL;
  64. //将视频流设置为video元素的源
  65. //此处的代码将会报错 解决的办法是将video的srcObject属性指向stream即可
  66. video.src = CompatibleURL.createObjectURL(stream);
  67. }
  68. // 播放视频
  69. video.play();
  70. // 可以通过画布canvas渲染,获取使用默认的video也行
  71. setInterval(function() {
  72. var radius = 70;
  73. // 圆的中心坐标
  74. var centerX = canvas.width / 2;
  75. var centerY = canvas.height / 2;
  76. canvas.width = v_t_w;
  77. canvas.height = v_t_h;
  78. context.drawImage(video, 0, 0, v_t_w, v_t_h);
  79. context.beginPath();
  80. context.arc(centerX, centerY, 70, 0, Math.PI * 2, false); // 绘制
  81. context.stroke();
  82. }, 10);
  83. }
  84. // 异常的回调函数
  85. function errorFun(error) {
  86. console.log("访问用户媒体设备失败:", error.name, error.message);
  87. alert("访问用户媒体设备失败:" + error.name + " " + error.message);
  88. }
  89. // 注册拍照按钮的单击事件-截图
  90. document.getElementById("capture").addEventListener("click", function() {
  91. canvas.width = v_t_w;
  92. canvas.height = v_t_h;
  93. // context.strokeRect(60, 60, 100, 100);
  94. context.beginPath();
  95. context.arc(95, 95, 70, 0, Math.PI * 2, true); // 绘制
  96. context.closePath(); // 结束路径绘制
  97. context.clip(); // 根据路径切割画布内容
  98. context.drawImage(video, 0, 0, v_t_w, v_t_h);
  99. var base64Img = canvas.toDataURL('image/jpg');
  100. //var base64 = canvas.toDataURL('image/jpeg',0.5);// 图片质量0.5
  101. img.src = base64Img;
  102. console.log(base64Img);
  103. });
  104. // 开始调用摄像头
  105. //like12 modified,20210628,bug,navigator.mediaDevices为空会导致后面卡死
  106. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia ||
  107. navigator.getUserMedia ||
  108. navigator.webkitGetUserMedia ||
  109. navigator.mozGetUserMedia) {
  110. // 调用用户媒体设备,访问摄像头
  111. getUserMedia({
  112. video: {
  113. width: v_t_w,
  114. height: v_t_h
  115. }
  116. }, successFun, errorFun);
  117. } else {
  118. alert("你的浏览器不支持访问用户媒体设备");
  119. }
  120. </script>
  121. </body>
  122. </html>