cardNow.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>H5摄像头(新版浏览器https)(兼容老版浏览器)</title>
  6. <script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js">
  7. </script>
  8. </head>
  9. <body style="background-color: blanchedalmond;display: flex;flex-direction: column;">
  10. <!-- 说明:将网页更改为https访问才行 否者报错:NotSupportedError Only secure origins are allowed (see: https://goo.gl/Y0ZkNV). -->
  11. <!-- video用于显示媒体设备的视频流,自动播放;属性:https://zhuanlan.zhihu.com/p/535917105 -->
  12. <video id="video" autoplay webkit-playsinline="true" playsinline="true"></video>
  13. <!-- transform: rotateY(180deg); 镜像解决 -->
  14. <!-- 可以通过画布canvas渲染,获取使用默认的video也行 -->
  15. <!-- <canvas id="canvas" width="500" height="400" style="transform: rotateY(180deg);"></canvas> -->
  16. <div class="box">
  17. <canvas id="canvas"></canvas>
  18. <span class="text">请将证件完整置于取景框内</span>
  19. <div class="buttonBox">
  20. <!-- 拍照按钮 -->
  21. <img class="buttonImg" id="back" src="../images/back.png" />
  22. <img class="buttonImg" id="capture" src="../images/pai.png" />
  23. <img class="buttonImg" id="fanzhuan" style="visibility: hidden" src="../images/fan.png" />
  24. </div>
  25. </div>
  26. <div id="popup">
  27. <img id="popupImg" alt="" src="">
  28. <div class="popupBox">
  29. <button id="closeBtn">取消</button>
  30. <button id="okBtn">确认</button>
  31. </div>
  32. </div>
  33. <script type="text/javascript">
  34. // 获取弹窗元素
  35. var popup = document.getElementById('popup');
  36. var popupImg = document.getElementById('popupImg');
  37. popup.style.display = 'none';
  38. // 获取关闭按钮元素
  39. var closeBtn = document.getElementById('closeBtn');
  40. var okBtn = document.getElementById('okBtn');
  41. var video = document.getElementById("video");
  42. var canvas = document.getElementById("canvas");
  43. var context = canvas.getContext("2d");
  44. // var ratio = 1;
  45. // var v_t_h = 300,
  46. // v_t_w = window.screen.width;
  47. var v_t_h = 200,
  48. v_t_w = window.screen.width;
  49. var mode = 'environment';
  50. var mediaStreamTrack;
  51. // canvas.width = v_t_w;
  52. // canvas.height = v_t_h;
  53. document.addEventListener('UniAppJSBridgeReady', function() {
  54. okBtn.addEventListener('click', function() {
  55. uni.postMessage({
  56. data: {
  57. base64: popupImg.src
  58. }
  59. });
  60. window.history.back(-1)
  61. });
  62. });
  63. // 弹出弹窗
  64. function openPopup() {
  65. popup.style.display = 'block';
  66. }
  67. // 关闭弹窗
  68. function closePopup() {
  69. takePhoto();
  70. popup.style.display = 'none';
  71. }
  72. // 绑定关闭按钮的点击事件
  73. closeBtn.addEventListener('click', closePopup);
  74. // 访问用户媒体设备的兼容方法
  75. function getUserMedia(constrains, successFun, errorFun) {
  76. //like12 modified,20210628,bug,navigator.mediaDevices为空会导致后面卡死
  77. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  78. //最新标准API(新版浏览器https)
  79. navigator.mediaDevices.getUserMedia(constrains).then(successFun).catch(errorFun);
  80. } else if (navigator.webkitGetUserMedia) {
  81. //like12 modified,20210628,不是这种调用方法 应该为后者
  82. //webkit内核浏览器(老版浏览器)
  83. //navigator.webkitGetUserMedia(constrains).then(successFun).catch(errorFun);
  84. navigator.webkitGetUserMedia({
  85. "video": true
  86. }, successFun, errorFun);
  87. } else if (navigator.mozGetUserMedia) {
  88. //Firefox浏览器
  89. navagator.mozGetUserMedia(constrains).then(successFun).catch(errorFun);
  90. } else if (navigator.getUserMedia) {
  91. //旧版API
  92. navigator.getUserMedia(constrains).then(successFun).catch(errorFun);
  93. }
  94. }
  95. // 成功的回调函数
  96. function successFun(stream) {
  97. //like12 modifed,20210618,Chrome升级后,新版本不再支持该用法
  98. //摄像头视频流显示报错Failed to execute 'createObjectURL' on 'URL'
  99. //研究即时通信的过程中需要调用摄像头,发现报错,原来是谷歌弃用了这个方法,根据官方提示修改即可
  100. //所以原先的代码:video.src = URL.createObjectURL(stream);
  101. //需要被修改为:video.srcObject = stream;
  102. //(新版浏览器https)
  103. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  104. video.srcObject = stream;
  105. }
  106. //(老版浏览器)
  107. else {
  108. //兼容webkit内核浏览器
  109. var CompatibleURL = window.URL || window.webkitURL;
  110. //将视频流设置为video元素的源
  111. //此处的代码将会报错 解决的办法是将video的srcObject属性指向stream即可
  112. video.src = CompatibleURL.createObjectURL(stream);
  113. }
  114. mediaStreamTrack = stream.getTracks()[0];
  115. // let settings = mediaStreamTrack.getSettings()
  116. // // 设置自动对焦
  117. // const track = stream.getVideoTracks()[0];
  118. // const capabilities = track.getCapabilities(); // Check whether focus distance is supported or not.
  119. // if (capabilities.focusMode.includes('continuous')) {
  120. // if (settings.focusMode !== 'continuous') {
  121. // settings.focusMode = 'continuous';
  122. // track.applyConstraints({ advanced: [{ focusMode: 'continuous' }] });
  123. // }
  124. // }
  125. // const { height, width } = settings
  126. // ratio = width / height
  127. // // v_t_w = v_t_h * ratio
  128. // // canvas.width = v_t_w;
  129. // // canvas.height = v_t_h;
  130. // 播放视频
  131. video.play();
  132. // 可以通过画布canvas渲染,获取使用默认的video也行
  133. setInterval(function() {
  134. context.drawImage(video, 0, 0, v_t_w, v_t_h);
  135. // context.beginPath();
  136. // context.arc(centerX, centerY, 70, 0, Math.PI * 2, false); // 绘制
  137. // context.stroke();
  138. }, 10);
  139. }
  140. // 异常的回调函数
  141. function errorFun(error) {
  142. console.log("访问用户媒体设备失败:", error.name, error.message);
  143. alert("访问用户媒体设备失败:" + error.name + " " + error.message);
  144. }
  145. document.getElementById('back').addEventListener('click', function() {
  146. window.history.back(-1)
  147. })
  148. document.getElementById('fanzhuan').addEventListener('click', function() {
  149. changeMode();
  150. })
  151. // 注册拍照按钮的单击事件-截图
  152. document.getElementById("capture").addEventListener("click", function() {
  153. mediaStreamTrack && mediaStreamTrack.stop();
  154. var base64Img = canvas.toDataURL('image/jpg');
  155. //var base64 = canvas.toDataURL('image/jpeg',0.5);// 图片质量0.5
  156. popupImg.src = base64Img;
  157. openPopup()
  158. });
  159. function takePhoto() {
  160. // 开始调用摄像头
  161. //like12 modified,20210628,bug,navigator.mediaDevices为空会导致后面卡死
  162. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia ||
  163. navigator.getUserMedia ||
  164. navigator.webkitGetUserMedia ||
  165. navigator.mozGetUserMedia) {
  166. // 调用用户媒体设备,访问摄像头
  167. getUserMedia({
  168. video: {
  169. // width: { min: 640 },
  170. // height: { min: 480 },
  171. width: 200,
  172. height: 400,
  173. facingMode: mode //设置使用后置摄像头,user为前置摄像头
  174. },
  175. }, successFun, errorFun);
  176. } else {
  177. alert("你的浏览器不支持访问用户媒体设备");
  178. }
  179. }
  180. takePhoto();
  181. function changeMode() {
  182. console.log("翻转摄像头");
  183. mode = mode === 'user' ? 'environment' : 'user';
  184. mediaStreamTrack.stop();
  185. takePhoto();
  186. }
  187. </script>
  188. </body>
  189. <style>
  190. video {
  191. transform: rotateY(180deg);
  192. display: none;
  193. }
  194. .box {
  195. margin: 0 auto;
  196. display: flex;
  197. flex-direction: column;
  198. text-align: center;
  199. height: 98vh;
  200. width: 100%;
  201. }
  202. canvas {
  203. margin-top: 15vh;
  204. /* border-radius: 50%; */
  205. }
  206. .text {
  207. margin-top: 3vh;
  208. color: #888;
  209. }
  210. .buttonBox {
  211. display: flex;
  212. justify-content: space-around;
  213. margin-top: 35vh;
  214. .buttonImg {
  215. width: 15vw;
  216. object-fit: contain;
  217. }
  218. }
  219. .noneImg {
  220. display: none;
  221. }
  222. #popup {
  223. position: fixed;
  224. top: 30%;
  225. left: 50%;
  226. transform: translate(-50%, -50%);
  227. background: #fff;
  228. padding: 20px;
  229. border-radius: 5px;
  230. box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
  231. z-index: 9999;
  232. }
  233. #popup h2 {
  234. margin-top: 0;
  235. }
  236. #closeBtn {
  237. width: 30vw;
  238. height: 4vh;
  239. border-radius: 16px;
  240. }
  241. #okBtn {
  242. width: 30vw;
  243. height: 4vh;
  244. border-radius: 16px;
  245. }
  246. .popupBox {
  247. margin-top: 2vh;
  248. width: 100%;
  249. display: flex;
  250. justify-content: space-around;
  251. }
  252. </style>
  253. </html>