card.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 = 200,
  46. v_t_w = window.screen.width;
  47. var mode = 'environment';
  48. var mediaStreamTrack;
  49. // canvas.width = v_t_w;
  50. // canvas.height = v_t_h;
  51. document.addEventListener('UniAppJSBridgeReady', function() {
  52. okBtn.addEventListener('click', function() {
  53. uni.postMessage({
  54. data: {
  55. base64: popupImg.src
  56. }
  57. });
  58. window.history.back(-1)
  59. });
  60. });
  61. // 弹出弹窗
  62. function openPopup() {
  63. popup.style.display = 'block';
  64. }
  65. // 关闭弹窗
  66. function closePopup() {
  67. takePhoto();
  68. popup.style.display = 'none';
  69. }
  70. // 绑定关闭按钮的点击事件
  71. closeBtn.addEventListener('click', closePopup);
  72. // 访问用户媒体设备的兼容方法
  73. function getUserMedia(constrains, successFun, errorFun) {
  74. //like12 modified,20210628,bug,navigator.mediaDevices为空会导致后面卡死
  75. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  76. //最新标准API(新版浏览器https)
  77. navigator.mediaDevices.getUserMedia(constrains).then(successFun).catch(errorFun);
  78. } else if (navigator.webkitGetUserMedia) {
  79. //like12 modified,20210628,不是这种调用方法 应该为后者
  80. //webkit内核浏览器(老版浏览器)
  81. //navigator.webkitGetUserMedia(constrains).then(successFun).catch(errorFun);
  82. navigator.webkitGetUserMedia({
  83. "video": true
  84. }, successFun, errorFun);
  85. } else if (navigator.mozGetUserMedia) {
  86. //Firefox浏览器
  87. navagator.mozGetUserMedia(constrains).then(successFun).catch(errorFun);
  88. } else if (navigator.getUserMedia) {
  89. //旧版API
  90. navigator.getUserMedia(constrains).then(successFun).catch(errorFun);
  91. }
  92. }
  93. // 成功的回调函数
  94. function successFun(stream) {
  95. //like12 modifed,20210618,Chrome升级后,新版本不再支持该用法
  96. //摄像头视频流显示报错Failed to execute 'createObjectURL' on 'URL'
  97. //研究即时通信的过程中需要调用摄像头,发现报错,原来是谷歌弃用了这个方法,根据官方提示修改即可
  98. //所以原先的代码:video.src = URL.createObjectURL(stream);
  99. //需要被修改为:video.srcObject = stream;
  100. //(新版浏览器https)
  101. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  102. video.srcObject = stream;
  103. }
  104. //(老版浏览器)
  105. else {
  106. //兼容webkit内核浏览器
  107. var CompatibleURL = window.URL || window.webkitURL;
  108. //将视频流设置为video元素的源
  109. //此处的代码将会报错 解决的办法是将video的srcObject属性指向stream即可
  110. video.src = CompatibleURL.createObjectURL(stream);
  111. }
  112. mediaStreamTrack = stream.getTracks()[0];
  113. let settings = mediaStreamTrack.getSettings()
  114. const {
  115. height,
  116. width
  117. } = settings
  118. ratio = width / height
  119. v_t_w = v_t_h * ratio
  120. canvas.width = v_t_w;
  121. canvas.height = v_t_h;
  122. // 播放视频
  123. video.play();
  124. // 可以通过画布canvas渲染,获取使用默认的video也行
  125. setInterval(function() {
  126. context.drawImage(video, 0, 0, v_t_w, v_t_h);
  127. // context.beginPath();
  128. // context.arc(centerX, centerY, 70, 0, Math.PI * 2, false); // 绘制
  129. // context.stroke();
  130. }, 10);
  131. }
  132. // 异常的回调函数
  133. function errorFun(error) {
  134. console.log("访问用户媒体设备失败:", error.name, error.message);
  135. alert("访问用户媒体设备失败:" + error.name + " " + error.message);
  136. }
  137. document.getElementById('back').addEventListener('click', function() {
  138. window.history.back(-1)
  139. })
  140. document.getElementById('fanzhuan').addEventListener('click', function() {
  141. changeMode();
  142. })
  143. // 注册拍照按钮的单击事件-截图
  144. document.getElementById("capture").addEventListener("click", function() {
  145. mediaStreamTrack && mediaStreamTrack.stop();
  146. var base64Img = canvas.toDataURL('image/jpg');
  147. //var base64 = canvas.toDataURL('image/jpeg',0.5);// 图片质量0.5
  148. popupImg.src = base64Img;
  149. openPopup()
  150. });
  151. function takePhoto() {
  152. // 开始调用摄像头
  153. //like12 modified,20210628,bug,navigator.mediaDevices为空会导致后面卡死
  154. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia ||
  155. navigator.getUserMedia ||
  156. navigator.webkitGetUserMedia ||
  157. navigator.mozGetUserMedia) {
  158. // 调用用户媒体设备,访问摄像头
  159. getUserMedia({
  160. video: {
  161. width: {
  162. min: 640
  163. },
  164. height: {
  165. min: 480
  166. },
  167. facingMode: mode //设置使用后置摄像头,user为前置摄像头
  168. },
  169. }, successFun, errorFun);
  170. } else {
  171. alert("你的浏览器不支持访问用户媒体设备");
  172. }
  173. }
  174. takePhoto();
  175. function changeMode() {
  176. console.log("翻转摄像头");
  177. mode = mode === 'user' ? 'environment' : 'user';
  178. mediaStreamTrack.stop();
  179. takePhoto();
  180. }
  181. </script>
  182. </body>
  183. <style>
  184. video {
  185. transform: rotateY(180deg);
  186. display: none;
  187. }
  188. .box {
  189. margin: 0 auto;
  190. display: flex;
  191. flex-direction: column;
  192. text-align: center;
  193. height: 98vh;
  194. width: 100%;
  195. }
  196. canvas {
  197. margin-top: 5vh;
  198. /* border-radius: 50%; */
  199. }
  200. .text {
  201. margin-top: 3vh;
  202. color: #888;
  203. }
  204. .buttonBox {
  205. display: flex;
  206. justify-content: space-around;
  207. margin-top: 15vh;
  208. .buttonImg {
  209. width: 15vw;
  210. object-fit: contain;
  211. }
  212. }
  213. .noneImg {
  214. display: none;
  215. }
  216. #popup {
  217. position: fixed;
  218. top: 30%;
  219. left: 50%;
  220. transform: translate(-50%, -50%);
  221. background: #fff;
  222. padding: 20px;
  223. border-radius: 5px;
  224. box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
  225. z-index: 9999;
  226. }
  227. #popup h2 {
  228. margin-top: 0;
  229. }
  230. #closeBtn {
  231. width: 30vw;
  232. height: 4vh;
  233. border-radius: 16px;
  234. }
  235. #okBtn {
  236. width: 30vw;
  237. height: 4vh;
  238. border-radius: 16px;
  239. }
  240. .popupBox {
  241. margin-top: 2vh;
  242. width: 100%;
  243. display: flex;
  244. justify-content: space-around;
  245. }
  246. </style>
  247. </html>