card.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="applicable-device" content="mobile">
  6. <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
  7. <title>H5摄像头(新版浏览器https)(兼容老版浏览器)</title>
  8. <!-- <script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js">
  9. </script> -->
  10. <script type="text/javascript" src="js/uni.webview.1.5.2.js">
  11. </script>
  12. <link rel="stylesheet" href="css/weui.min.css">
  13. <link rel="stylesheet" href="css/jquery-weui.css">
  14. </head>
  15. <body style="background-color: blanchedalmond;display: flex;flex-direction: column;">
  16. <!-- 说明:将网页更改为https访问才行 否者报错:NotSupportedError Only secure origins are allowed (see: https://goo.gl/Y0ZkNV). -->
  17. <!-- video用于显示媒体设备的视频流,自动播放;属性:https://zhuanlan.zhihu.com/p/535917105 -->
  18. <canvas id="canvas" ></canvas>
  19. <!-- transform: rotateY(180deg); 镜像解决 -->
  20. <!-- 可以通过画布canvas渲染,获取使用默认的video也行 -->
  21. <!-- <canvas id="canvas" width="500" height="400" style="transform: rotateY(180deg);"></canvas> -->
  22. <div class="box">
  23. <!-- <canvas id="canvas"></canvas> -->
  24. <div style="position: relative;width: 100%;margin-top: 15vh;">
  25. <video id="video" autoplay webkit-playsinline="true" playsinline="true"></video>
  26. <div class="imgStyle" id=imgStyle></div>
  27. </div>
  28. <span class="text">请将证件完整置于取景框内</span>
  29. <div class="buttonBox">
  30. <!-- 拍照按钮 -->
  31. <img class="buttonImg" id="back" src="../images/back.png" />
  32. <img class="buttonImg" id="capture" src="../images/pai.png" />
  33. <img class="buttonImg" id="fanzhuan" src="../images/fan.png" />
  34. <!-- <a href="javascript:op_Menu();" class="weui-btn weui-btn_mini weui-btn_primary" style="height: 4vh;">切换摄像头</a> -->
  35. </div>
  36. </div>
  37. <div id="popup">
  38. <img id="popupImg" width="100%" alt="" src="">
  39. <div class="popupBox">
  40. <button id="closeBtn" style="margin-right: 10px;">取消</button>
  41. <button id="okBtn">确认</button>
  42. </div>
  43. </div>
  44. <script src="js/jquery-2.1.4.js"></script>
  45. <script src="js/jquery-weui.js"></script>
  46. <script type="text/javascript">
  47. var isQian = false; //是前置摄像头
  48. // 获取弹窗元素
  49. var popup = document.getElementById('popup');
  50. var popupImg = document.getElementById('popupImg');
  51. popup.style.display = 'none';
  52. // 获取关闭按钮元素
  53. var closeBtn = document.getElementById('closeBtn');
  54. var okBtn = document.getElementById('okBtn');
  55. var video = document.getElementById("video");
  56. var canvas = document.getElementById("canvas");
  57. var context = canvas.getContext("2d");
  58. var imgStyle = document.getElementById("imgStyle");
  59. var idealw=540;
  60. var idealh=856;
  61. var v_t_w = 540,
  62. v_t_h = 856;
  63. // var v_t_w = window.screen.width,
  64. // v_t_h = 200;
  65. var mode = 'environment';
  66. var mediaStreamTrack;
  67. var selectedDeviceId;
  68. var isLoding=false;
  69. var dataMock = [];
  70. let cardType = localStorage.getItem("cardType")
  71. if (cardType === 'front') {
  72. imgStyle.style.backgroundImage = "url('camera_idcard_front.png')";
  73. } else {
  74. imgStyle.style.backgroundImage = "url('camera_idcard_back.png')";
  75. }
  76. // 85.6 54
  77. // v_t_w = v_t_w * 90 / 100
  78. // v_t_h = 54 / 85.6 * v_t_w
  79. canvas.width = v_t_w;
  80. canvas.height = v_t_h;
  81. document.addEventListener('UniAppJSBridgeReady', function() {
  82. okBtn.addEventListener('click', function() {
  83. uni.postMessage({
  84. data: {
  85. base64: popupImg.src
  86. }
  87. });
  88. window.history.back(-1)
  89. });
  90. });
  91. // 弹出弹窗
  92. function openPopup() {
  93. popup.style.display = 'block';
  94. }
  95. // 关闭弹窗
  96. function closePopup() {
  97. changeFacingMode();
  98. popup.style.display = 'none';
  99. }
  100. // 绑定关闭按钮的点击事件
  101. closeBtn.addEventListener('click', closePopup);
  102. //切换摄像头
  103. function changeVideo(value, index) {
  104. if (!value) {
  105. return
  106. }
  107. localStorage.setItem('deviceIndex', index);
  108. if (mediaStreamTrack) {
  109. mediaStreamTrack.stop();
  110. }
  111. getUserMedia({
  112. video: {
  113. deviceId: value,
  114. // deviceId:{
  115. // exact:value,
  116. // },
  117. // width: 200,
  118. // height: window.screen.width,
  119. width: { ideal: idealw },
  120. height: { ideal: idealh }
  121. // facingMode: mode //设置使用后置摄像头,user为前置摄像头
  122. },
  123. }, successFun, errorFun);
  124. }
  125. // 访问用户媒体设备的兼容方法
  126. function getUserMedia(constrains, successFun, errorFun) {
  127. //like12 modified,20210628,bug,navigator.mediaDevices为空会导致后面卡死
  128. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  129. //最新标准API(新版浏览器https)
  130. navigator.mediaDevices.getUserMedia(constrains).then(successFun).catch(errorFun);
  131. } else if (navigator.webkitGetUserMedia) {
  132. //like12 modified,20210628,不是这种调用方法 应该为后者
  133. //webkit内核浏览器(老版浏览器)
  134. //navigator.webkitGetUserMedia(constrains).then(successFun).catch(errorFun);
  135. navigator.webkitGetUserMedia({
  136. "video": true
  137. }, successFun, errorFun);
  138. } else if (navigator.mozGetUserMedia) {
  139. //Firefox浏览器
  140. navagator.mozGetUserMedia(constrains).then(successFun).catch(errorFun);
  141. } else if (navigator.getUserMedia) {
  142. //旧版API
  143. navigator.getUserMedia(constrains).then(successFun).catch(errorFun);
  144. }
  145. }
  146. function op_Menu() {
  147. // alert("data" + dataMock.length);
  148. $.actions({
  149. title: "切换摄像头",
  150. onClose: function() {
  151. console.log("close");
  152. },
  153. actions: dataMock.map((item, index) => {
  154. return {
  155. text: item.text,
  156. className: "color-primary",
  157. onClick: function() {
  158. isQian = false;
  159. document.querySelector("video").style.cssText =
  160. "width: 100%; height: 200px;-webkit-playsinline: true; playsinline: true;transform:rotateY(0deg);";
  161. if (item.text.toLowerCase().includes("front") || item.text.toLowerCase()
  162. .includes("前置")) {
  163. isQian = true;
  164. document.querySelector("video").style.cssText =
  165. "width: 100%; height: 200px;-webkit-playsinline: true; playsinline: true;transform:rotateY(180deg);";
  166. }
  167. changeVideo(item.value, index);
  168. }
  169. }
  170. })
  171. });
  172. }
  173. // 成功的回调函数
  174. function successFun(stream) {
  175. //like12 modifed,20210618,Chrome升级后,新版本不再支持该用法
  176. //摄像头视频流显示报错Failed to execute 'createObjectURL' on 'URL'
  177. //研究即时通信的过程中需要调用摄像头,发现报错,原来是谷歌弃用了这个方法,根据官方提示修改即可
  178. //所以原先的代码:video.src = URL.createObjectURL(stream);
  179. //需要被修改为:video.srcObject = stream;
  180. //(新版浏览器https)
  181. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  182. video.srcObject = stream;
  183. }
  184. //(老版浏览器)
  185. else {
  186. //兼容webkit内核浏览器
  187. var CompatibleURL = window.URL || window.webkitURL;
  188. //将视频流设置为video元素的源
  189. //此处的代码将会报错 解决的办法是将video的srcObject属性指向stream即可
  190. video.src = CompatibleURL.createObjectURL(stream);
  191. }
  192. mediaStreamTrack = stream.getTracks()[0];
  193. let settings = mediaStreamTrack.getSettings();
  194. // 获取分辨率信息
  195. v_t_w = settings.width;
  196. v_t_h = settings.height;
  197. //video.width=v_t_w;
  198. //video.height=v_t_h;
  199. canvas.width = v_t_w;
  200. canvas.height = v_t_h;
  201. // 播放视频
  202. video.play();
  203. // 可以通过画布canvas渲染,获取使用默认的video也行
  204. // setInterval(function() {
  205. // context.drawImage(video, 0, 0, v_t_w, v_t_h);
  206. // }, 20);
  207. // function draw() {
  208. // context.drawImage(video, 0, 0, v_t_w, v_t_h);
  209. // requestAnimationFrame(draw);
  210. // }
  211. // draw();
  212. isLoding=false
  213. }
  214. // 异常的回调函数
  215. function errorFun(error) {
  216. isLoding=false
  217. console.log("访问用户媒体设备失败:", error.name, error.message);
  218. alert("访问用户媒体设备失败:" + error.name + " " + error.message);
  219. }
  220. document.getElementById('back').addEventListener('click', function() {
  221. window.history.back(-1)
  222. })
  223. document.getElementById('fanzhuan').addEventListener('click', function() {
  224. if(isLoding)
  225. {
  226. return;
  227. }
  228. if(dataMock&&dataMock.length<3)
  229. {
  230. changeMode();
  231. }else{
  232. op_Menu();
  233. }
  234. })
  235. // 注册拍照按钮的单击事件-截图
  236. document.getElementById("capture").addEventListener("click", function() {
  237. // 获取ID为"myElement"的元素
  238. var element = document.getElementById("popup");
  239. // 获取该元素的样式属性
  240. var style = window.getComputedStyle(element);
  241. // 例如,获取元素的背景颜色
  242. var displayStyle = style.display;
  243. if(displayStyle!='none')
  244. {
  245. return ;
  246. }
  247. // 画布截取
  248. canvas.width = video.videoWidth;
  249. canvas.height = video.videoHeight;
  250. if (isQian == true) {
  251. context.translate(canvas.width, 0);
  252. context.scale(-1, 1);
  253. }
  254. context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
  255. mediaStreamTrack && mediaStreamTrack.stop();
  256. var base64Img = canvas.toDataURL('image/jpeg');
  257. //var base64 = canvas.toDataURL('image/jpeg',0.5);// 图片质量0.5
  258. popupImg.src = base64Img;
  259. openPopup()
  260. });
  261. function takePhoto() {
  262. // 开始调用摄像头
  263. //like12 modified,20210628,bug,navigator.mediaDevices为空会导致后面卡死
  264. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia ||
  265. navigator.getUserMedia ||
  266. navigator.webkitGetUserMedia ||
  267. navigator.mozGetUserMedia) {
  268. navigator.mediaDevices.enumerateDevices().then(function(devices) {
  269. let number = 1;
  270. dataMock=[];
  271. devices.forEach(function(device, index) {
  272. if (device.kind === 'videoinput') {
  273. // var option = document.createElement('option');
  274. let label = '摄像头' + number;
  275. number++;
  276. if (device.label && device.label.search('back') !== -1) {
  277. label = "后置" + label;
  278. }
  279. if (device.label && device.label.search('front') !== -1) {
  280. label = "前置" + label;
  281. }
  282. // option.text = label;
  283. // option.value = device.deviceId;
  284. dataMock.push({
  285. text: label,
  286. value: device.deviceId
  287. })
  288. }
  289. })
  290. if (localStorage.getItem('deviceIndex')) {
  291. selectedDeviceId = dataMock[localStorage.getItem('deviceIndex')] ? dataMock[localStorage.getItem(
  292. 'deviceIndex')].value : '';
  293. }
  294. // 调用用户媒体设备,访问摄像头
  295. changeFacingMode()
  296. });
  297. } else {
  298. alert("你的浏览器不支持访问用户媒体设备");
  299. }
  300. }
  301. function changeFacingMode(){
  302. isLoding=true
  303. if(!selectedDeviceId||dataMock.length<3){
  304. getUserMedia({
  305. video: {
  306. width: {
  307. ideal: idealw
  308. },
  309. height: {
  310. ideal: idealh
  311. },
  312. // width: 200,
  313. // height: window.screen.width,
  314. // focusMode: "mode",
  315. facingMode: mode //设置使用后置摄像头,user为前置摄像头
  316. },
  317. }, successFun, errorFun);
  318. }
  319. else{
  320. getUserMedia({
  321. video: {
  322. deviceId: selectedDeviceId,
  323. // width: 200,
  324. // height: window.screen.width,
  325. width: {
  326. ideal: idealw
  327. },
  328. height: {
  329. ideal: idealh
  330. },
  331. focusMode:"continuous",
  332. // facingMode: mode //设置使用后置摄像头,user为前置摄像头
  333. },
  334. }, successFun, errorFun);
  335. }
  336. }
  337. takePhoto();
  338. function changeMode() {
  339. console.log("翻转摄像头");
  340. mode = mode === 'user' ? 'environment' : 'user';
  341. mediaStreamTrack.stop();
  342. isQian = false;
  343. document.querySelector("video").style.cssText =
  344. "width: 100%; height: 200px;-webkit-playsinline: true; playsinline: true;transform:rotateY(0deg);";
  345. if (mode.toLowerCase().includes("user")) {
  346. isQian = true;
  347. document.querySelector("video").style.cssText =
  348. "width: 100%; height: 200px;-webkit-playsinline: true; playsinline: true;transform:rotateY(180deg);";
  349. }
  350. changeFacingMode();
  351. }
  352. </script>
  353. </body>
  354. <style>
  355. .imgStyle {
  356. width: 300px;
  357. height: 190px;
  358. /* margin-top: 50px; */
  359. position: absolute;
  360. /* border: 1px solid red; */
  361. position: absolute;
  362. top: 50%;
  363. left: 50%;
  364. transform: translate(-50%, -50%);
  365. z-index: 10;
  366. background-image: url('camera_idcard_front.png');
  367. background-size: 100% 100%;
  368. }
  369. video {
  370. /* transform: rotateY(180deg);
  371. display: none; */
  372. width: 100%;
  373. height: 200px;
  374. }
  375. .box {
  376. margin: 0 auto;
  377. display: flex;
  378. flex-direction: column;
  379. text-align: center;
  380. height: 98vh;
  381. width: 100%;
  382. }
  383. canvas {
  384. /* width: 100%; */
  385. display: none;
  386. }
  387. .text {
  388. margin-top: 3vh;
  389. color: #888;
  390. /* color: red; */
  391. }
  392. .buttonBox {
  393. display: flex;
  394. justify-content: space-around;
  395. margin-top: 35vh;
  396. align-items: center;
  397. }
  398. .buttonImg {
  399. /* margin-left: 10vw; */
  400. width: 15vw;
  401. object-fit: contain;
  402. }
  403. .noneImg {
  404. display: none;
  405. }
  406. #popup {
  407. position: fixed;
  408. top: 30%;
  409. left: 50%;
  410. transform: translate(-50%, -50%);
  411. background: #fff;
  412. padding: 20px;
  413. border-radius: 5px;
  414. box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
  415. z-index: 9999;
  416. }
  417. #popup h2 {
  418. margin-top: 0;
  419. }
  420. #closeBtn {
  421. width: 30vw;
  422. height: 4vh;
  423. border-radius: 16px;
  424. }
  425. #okBtn {
  426. width: 30vw;
  427. height: 4vh;
  428. border-radius: 16px;
  429. }
  430. .popupBox {
  431. margin-top: 2vh;
  432. width: 100%;
  433. display: flex;
  434. justify-content: space-around;
  435. }
  436. </style>
  437. </html>