card.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 v_t_w = window.screen.width,
  45. v_t_h = 200;
  46. var mode = 'environment';
  47. var mediaStreamTrack;
  48. // 85.6 54
  49. // v_t_w = v_t_w * 90 / 100
  50. // v_t_h = 54 / 85.6 * v_t_w
  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. // 播放视频
  116. video.play();
  117. // 可以通过画布canvas渲染,获取使用默认的video也行
  118. setInterval(function() {
  119. context.drawImage(video, 0, 0, v_t_w, v_t_h);
  120. // context.beginPath();
  121. // context.arc(centerX, centerY, 70, 0, Math.PI * 2, false); // 绘制
  122. // context.stroke();
  123. }, 10);
  124. }
  125. // 异常的回调函数
  126. function errorFun(error) {
  127. console.log("访问用户媒体设备失败:", error.name, error.message);
  128. alert("访问用户媒体设备失败:" + error.name + " " + error.message);
  129. }
  130. document.getElementById('back').addEventListener('click', function() {
  131. window.history.back(-1)
  132. })
  133. document.getElementById('fanzhuan').addEventListener('click', function() {
  134. changeMode();
  135. })
  136. // 注册拍照按钮的单击事件-截图
  137. document.getElementById("capture").addEventListener("click", function() {
  138. mediaStreamTrack && mediaStreamTrack.stop();
  139. var base64Img = canvas.toDataURL('image/jpg');
  140. //var base64 = canvas.toDataURL('image/jpeg',0.5);// 图片质量0.5
  141. popupImg.src = base64Img;
  142. openPopup()
  143. });
  144. function takePhoto() {
  145. // 开始调用摄像头
  146. //like12 modified,20210628,bug,navigator.mediaDevices为空会导致后面卡死
  147. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia ||
  148. navigator.getUserMedia ||
  149. navigator.webkitGetUserMedia ||
  150. navigator.mozGetUserMedia) {
  151. // 调用用户媒体设备,访问摄像头
  152. getUserMedia({
  153. video: {
  154. width: 200,
  155. height: 400,
  156. facingMode: mode //设置使用后置摄像头,user为前置摄像头
  157. },
  158. }, successFun, errorFun);
  159. } else {
  160. alert("你的浏览器不支持访问用户媒体设备");
  161. }
  162. }
  163. takePhoto();
  164. function changeMode() {
  165. console.log("翻转摄像头");
  166. mode = mode === 'user' ? 'environment' : 'user';
  167. mediaStreamTrack.stop();
  168. takePhoto();
  169. }
  170. </script>
  171. </body>
  172. <style>
  173. video {
  174. transform: rotateY(180deg);
  175. display: none;
  176. }
  177. .box {
  178. margin: 0 auto;
  179. display: flex;
  180. flex-direction: column;
  181. text-align: center;
  182. height: 98vh;
  183. width: 100%;
  184. }
  185. canvas {
  186. margin-top: 15vh;
  187. /* border-radius: 50%; */
  188. }
  189. .text {
  190. margin-top: 3vh;
  191. color: #888;
  192. }
  193. .buttonBox {
  194. display: flex;
  195. justify-content: space-around;
  196. margin-top: 35vh;
  197. .buttonImg {
  198. width: 15vw;
  199. object-fit: contain;
  200. }
  201. }
  202. .noneImg {
  203. display: none;
  204. }
  205. #popup {
  206. position: fixed;
  207. top: 30%;
  208. left: 50%;
  209. transform: translate(-50%, -50%);
  210. background: #fff;
  211. padding: 20px;
  212. border-radius: 5px;
  213. box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
  214. z-index: 9999;
  215. }
  216. #popup h2 {
  217. margin-top: 0;
  218. }
  219. #closeBtn {
  220. width: 30vw;
  221. height: 4vh;
  222. border-radius: 16px;
  223. }
  224. #okBtn {
  225. width: 30vw;
  226. height: 4vh;
  227. border-radius: 16px;
  228. }
  229. .popupBox {
  230. margin-top: 2vh;
  231. width: 100%;
  232. display: flex;
  233. justify-content: space-around;
  234. }
  235. </style>
  236. </html>