face-old.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. <span class="text">请正对摄像头</span>
  18. <canvas class="canvas" id="canvas"></canvas>
  19. </div>
  20. <div class="wenziBox">
  21. <div class="zhuyi">
  22. <p class="line"></p>
  23. <span>注意事项</span>
  24. <p class="line"></p>
  25. </div>
  26. <div class="imgBox">
  27. <div class="item">
  28. <img class="buttonImg" src="../images/face.png" alt="" />
  29. <span class="span">正脸</span>
  30. </div>
  31. <div class="item">
  32. <img class="buttonImg" src="../images/deng.png" alt="" />
  33. <span class="span">光线</span>
  34. </div>
  35. <div class="item">
  36. <img class="buttonImg" src="../images/qingxi.png" alt="" />
  37. <span class="span">清晰</span>
  38. </div>
  39. </div>
  40. </div>
  41. <div class="buttonBox">
  42. <!-- 拍照按钮 -->
  43. <img class="buttonImg" id="back" src="../images/back.png" />
  44. <img class="buttonImg" id="capture" src="../images/pai.png" />
  45. <img class="buttonImg" id="fanzhuan" src="../images/fan.png" />
  46. </div>
  47. <div id="popup">
  48. <img id="popupImg" alt="" src="">
  49. <div class="popupBox">
  50. <button id="closeBtn">取消</button>
  51. <button id="okBtn">确认</button>
  52. </div>
  53. </div>
  54. <script type="text/javascript">
  55. // 获取弹窗元素
  56. var popup = document.getElementById('popup');
  57. var popupImg = document.getElementById('popupImg');
  58. popup.style.display = 'none';
  59. // 获取关闭按钮元素
  60. var closeBtn = document.getElementById('closeBtn');
  61. var okBtn = document.getElementById('okBtn');
  62. var video = document.getElementById("video");
  63. var canvas = document.getElementById("canvas");
  64. var context = canvas.getContext("2d");
  65. var v_t_w = 200,
  66. v_t_h = 200;
  67. var mode = 'environment';
  68. var mediaStreamTrack;
  69. document.addEventListener('UniAppJSBridgeReady', function() {
  70. okBtn.addEventListener('click', function() {
  71. uni.postMessage({
  72. data: {
  73. faceBase64: popupImg.src
  74. }
  75. });
  76. window.history.back(-1)
  77. });
  78. });
  79. // 弹出弹窗
  80. function openPopup() {
  81. popup.style.display = 'block';
  82. }
  83. // 关闭弹窗
  84. function closePopup() {
  85. takePhoto();
  86. popup.style.display = 'none';
  87. }
  88. // 绑定关闭按钮的点击事件
  89. closeBtn.addEventListener('click', closePopup);
  90. // 访问用户媒体设备的兼容方法
  91. function getUserMedia(constrains, successFun, errorFun) {
  92. //like12 modified,20210628,bug,navigator.mediaDevices为空会导致后面卡死
  93. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  94. //最新标准API(新版浏览器https)
  95. navigator.mediaDevices.getUserMedia(constrains).then(successFun).catch(errorFun);
  96. } else if (navigator.webkitGetUserMedia) {
  97. //like12 modified,20210628,不是这种调用方法 应该为后者
  98. //webkit内核浏览器(老版浏览器)
  99. //navigator.webkitGetUserMedia(constrains).then(successFun).catch(errorFun);
  100. navigator.webkitGetUserMedia({
  101. "video": true
  102. }, successFun, errorFun);
  103. } else if (navigator.mozGetUserMedia) {
  104. //Firefox浏览器
  105. navagator.mozGetUserMedia(constrains).then(successFun).catch(errorFun);
  106. } else if (navigator.getUserMedia) {
  107. //旧版API
  108. navigator.getUserMedia(constrains).then(successFun).catch(errorFun);
  109. }
  110. }
  111. // 成功的回调函数
  112. function successFun(stream) {
  113. //like12 modifed,20210618,Chrome升级后,新版本不再支持该用法
  114. //摄像头视频流显示报错Failed to execute 'createObjectURL' on 'URL'
  115. //研究即时通信的过程中需要调用摄像头,发现报错,原来是谷歌弃用了这个方法,根据官方提示修改即可
  116. //所以原先的代码:video.src = URL.createObjectURL(stream);
  117. //需要被修改为:video.srcObject = stream;
  118. //(新版浏览器https)
  119. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  120. video.srcObject = stream;
  121. }
  122. //(老版浏览器)
  123. else {
  124. //兼容webkit内核浏览器
  125. var CompatibleURL = window.URL || window.webkitURL;
  126. //将视频流设置为video元素的源
  127. //此处的代码将会报错 解决的办法是将video的srcObject属性指向stream即可
  128. video.src = CompatibleURL.createObjectURL(stream);
  129. }
  130. mediaStreamTrack = stream.getTracks()[0];
  131. // 播放视频
  132. video.play();
  133. // 可以通过画布canvas渲染,获取使用默认的video也行
  134. setInterval(function() {
  135. // canvas.width = v_t_w;
  136. // canvas.height = v_t_h;
  137. // context.drawImage(video, 0, 0, v_t_w, v_t_h);
  138. canvas.width = v_t_w;
  139. canvas.height = v_t_h;
  140. // context.strokeRect(60, 60, 100, 100);
  141. context.beginPath();
  142. context.arc(v_t_w / 2, v_t_w / 2, v_t_w / 2, 0, Math.PI * 2, true); // 绘制
  143. context.closePath(); // 结束路径绘制
  144. context.clip(); // 根据路径切割画布内容
  145. context.drawImage(video, 0, 0, v_t_w, v_t_h);
  146. // context.beginPath();
  147. // context.arc(centerX, centerY, 70, 0, Math.PI * 2, false); // 绘制
  148. // context.stroke();
  149. }, 10);
  150. }
  151. // 异常的回调函数
  152. function errorFun(error) {
  153. console.log("访问用户媒体设备失败:", error.name, error.message);
  154. alert("访问用户媒体设备失败:" + error.name + " " + error.message);
  155. }
  156. document.getElementById('fanzhuan').addEventListener('click', function() {
  157. changeMode();
  158. })
  159. document.getElementById('back').addEventListener('click', function() {
  160. window.history.back(-1)
  161. })
  162. // 注册拍照按钮的单击事件-截图
  163. document.getElementById("capture").addEventListener("click", function() {
  164. canvas.width = v_t_w;
  165. canvas.height = v_t_h;
  166. // context.strokeRect(60, 60, 100, 100);
  167. // context.beginPath();
  168. // context.arc(v_t_w / 2, v_t_w / 2, v_t_w / 2, 0, Math.PI * 2, true); // 绘制
  169. // context.closePath(); // 结束路径绘制
  170. // context.clip(); // 根据路径切割画布内容
  171. context.drawImage(video, 0, 0, v_t_w, v_t_h);
  172. mediaStreamTrack && mediaStreamTrack.stop();
  173. var base64Img = canvas.toDataURL('image/jpg');
  174. //var base64 = canvas.toDataURL('image/jpeg',0.5);// 图片质量0.5
  175. popupImg.src = base64Img;
  176. // console.log(base64Img);
  177. openPopup()
  178. });
  179. function takePhoto() {
  180. // 开始调用摄像头
  181. //like12 modified,20210628,bug,navigator.mediaDevices为空会导致后面卡死
  182. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia ||
  183. navigator.getUserMedia ||
  184. navigator.webkitGetUserMedia ||
  185. navigator.mozGetUserMedia) {
  186. // 调用用户媒体设备,访问摄像头
  187. getUserMedia({
  188. video: {
  189. width: v_t_w,
  190. height: v_t_h,
  191. facingMode: mode //设置使用后置摄像头,user为前置摄像头
  192. },
  193. }, successFun, errorFun);
  194. } else {
  195. alert("你的浏览器不支持访问用户媒体设备");
  196. }
  197. }
  198. takePhoto();
  199. function changeMode() {
  200. console.log("翻转摄像头");
  201. mode = mode === 'user' ? 'environment' : 'user';
  202. mediaStreamTrack.stop();
  203. takePhoto();
  204. }
  205. </script>
  206. </body>
  207. <style scoped>
  208. video {
  209. /* transform: rotateY(180deg); */
  210. display: none;
  211. transform: scaleX(-1);
  212. -o-transform: scaleX(-1);
  213. -webkit-transform: scaleX(-1);
  214. filter:fliph;
  215. -ms-filter:fliph;
  216. }
  217. .box {
  218. margin: 12vh auto;
  219. display: flex;
  220. flex-direction: column;
  221. text-align: center;
  222. .canvas {
  223. margin-top: 6vh;
  224. border-radius: 50%;
  225. }
  226. .text {
  227. /* position: absolute; */
  228. /* align-items: center; */
  229. }
  230. }
  231. .wenziBox {
  232. .zhuyi {
  233. display: flex;
  234. flex-direction: row;
  235. justify-content: space-around;
  236. .line {
  237. border-bottom: 1px solid black;
  238. width: 20vw;
  239. }
  240. }
  241. .imgBox {
  242. display: flex;
  243. flex-direction: row;
  244. justify-content: space-around;
  245. margin-top: 2vh;
  246. .item {
  247. display: flex;
  248. flex-direction: column;
  249. .span {
  250. text-align: center;
  251. }
  252. }
  253. }
  254. }
  255. .buttonBox {
  256. display: flex;
  257. justify-content: space-around;
  258. margin-top: 10vh;
  259. }
  260. .buttonImg {
  261. width: 15vw;
  262. object-fit: contain;
  263. margin-bottom: 1vh;
  264. }
  265. .noneImg {
  266. display: none;
  267. }
  268. #popup {
  269. display: flex;
  270. justify-content: center;
  271. position: fixed;
  272. width: 100%;
  273. top: 30%;
  274. left: 50%;
  275. transform: translate(-50%, -50%);
  276. background: #fff;
  277. padding: 20px;
  278. border-radius: 5px;
  279. box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
  280. z-index: 9999;
  281. }
  282. #popup h2 {
  283. margin-top: 0;
  284. }
  285. #closeBtn {
  286. width: 30vw;
  287. height: 4vh;
  288. border-radius: 16px;
  289. }
  290. #okBtn {
  291. width: 30vw;
  292. height: 4vh;
  293. border-radius: 16px;
  294. }
  295. #popupImg {
  296. margin-left: 25vw;
  297. }
  298. .popupBox {
  299. margin-top: 2vh;
  300. width: 100%;
  301. display: flex;
  302. justify-content: space-around;
  303. }
  304. </style>
  305. </html>