media.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. <template>
  2. <view>
  3. <u-popup v-model="isShow" mode="bottom" border-radius="40" :mask-close-able="false">
  4. <view class="photoBox" v-if="isShow">
  5. <view class="photoTop">
  6. <view class="centersq">请正视手机屏幕 <button @click="showenter" type="primary" style="margin-left: 20px;">{{showCenter?"收起":"隐藏"}}</button></view>
  7. </view>
  8. <view class="photoCenter" v-show="showCenter">
  9. <view class="center_camera" v-if="isTaking">
  10. <!-- #ifdef MP-WEIXIN -->
  11. <camera device-position="front" flash="off" @error="error"
  12. style="width: 400px; height: 400px;margin: 0 auto;">
  13. <!-- 加人脸框 -->
  14. </camera>
  15. <!-- #endif -->
  16. <!-- #ifdef H5 -->
  17. <video :controls="false" v-show="status !== 3" id="video" width="400" height="400"
  18. class="photo_v"></video>
  19. <video id="video1" v-show="status === 3" class="photo_v2" autoplay controls :src="url"></video>
  20. <view class="mask" v-if="false"></view>
  21. <!-- #endif -->
  22. </view>
  23. <view class="custom" v-if="!isTaking">
  24. <!-- #ifdef MP-WEIXIN -->
  25. <image :src="avatarUrl" mode=""></image>
  26. <!-- #endif -->
  27. <!-- #ifdef H5 -->
  28. <image :src="faceUrl" mode=""></image>
  29. <!-- #endif -->
  30. </view>
  31. </view>
  32. <view class="btns">
  33. <!-- <view class="btnResult" v-if="isTaking" @click="takePhoto"
  34. >拍照</view
  35. > -->
  36. <view v-if="isTaking" class="takePhoto_btn">
  37. <view style="width: 100rpx; height: 2rpx"></view>
  38. <view class="">
  39. <button style="float: left;" :type="status === 3 ? 'primary' : 'primary'" class="pos"
  40. @click="luzhi" :disabled="status === 2">{{
  41. status === 1 ? "开始录制3秒" : status === 2 ? "录制中" : "提交"
  42. }}</button>
  43. <button @click="again" type="primary" style="float: left;
  44. margin-left: 20px;" v-if="status === 3">重录</button>
  45. <!-- <view class="btnResult" @click="luzhi()">重拍</view> -->
  46. <!-- <view class="square"></view> -->
  47. </view>
  48. <view class="rights" @click="takePhTips()">
  49. <text>抽查提示</text>
  50. <u-icon name="arrow-right" color="#FFFFFF" size="30"></u-icon>
  51. </view>
  52. </view>
  53. <view class="btnResult" v-if="!isTaking" @click="reTake">重拍</view>
  54. <view class="btnResult" v-if="!isTaking" @click="submit">确认</view>
  55. </view>
  56. </view>
  57. </u-popup>
  58. <!-- 播放前拍照end -->
  59. <u-popup v-model="showSet" :mask-close-able="false" mode="center" border-radius="24">
  60. <view style="
  61. align-items: center;
  62. padding: 0 40rpx;
  63. display: flex;
  64. flex-direction: column;
  65. justify-content: center;
  66. ">
  67. <view style="
  68. font-weight: bold;
  69. color: #333333;
  70. font-size: 30rpx;
  71. margin-top: 30rpx;
  72. ">温馨提示</view>
  73. <view style="
  74. width: 457rpx;
  75. color: #666666;
  76. font-size: 30rpx;
  77. margin-top: 30rpx;
  78. ">学习过程中需要拍照验证学员身份, 拍照功能需要使用您的相机。
  79. 是否授权使用?</view>
  80. <view style="margin: 40rpx 0">
  81. <button open-type="openSetting" @bindopensetting="openSetting" class="btnSet">
  82. 去授权
  83. </button>
  84. </view>
  85. </view>
  86. </u-popup>
  87. <!-- 拍照提示 -->
  88. <u-popup v-model="popupPhotoShow" mode="bottom" border-radius="30" :mask-close-able='false'>
  89. <view class="popup_box">
  90. <view class="head">
  91. <image src="/static/learn/photo_head.png" class="photo_head"></image>
  92. </view>
  93. <view class="warn">温馨提示</view>
  94. <view class="contents">
  95. <view class="words">1、本次为抽查检测,需要录制3秒的视频。</view>
  96. <view class="words">2、请保证摄像头正对自己,避免头像偏左或者偏右。</view>
  97. <view class="words">3、请保证拍照环境光线充足(照片太暗或曝光会降低验证通过率)。</view>
  98. <view class="words">4、请保证整个头像在人脸识别区域内,脸部无遮挡装饰物(佩戴眼镜会降低通过率)。</view>
  99. <view class="words">5、如果下面视频中出现黑屏,摄像头可能被其他进程占用,请关闭其他调用摄像头的程序,重新刷新当前页面重新拍照识别。</view>
  100. </view>
  101. <view class="take_photo" @click="openCamera">
  102. 去录像
  103. </view>
  104. </view>
  105. </u-popup>
  106. </view>
  107. </template>
  108. <script>
  109. import myCompressImage from "@/common/compressPhoto.js";
  110. import PopupPhoto from "./index.vue";
  111. export default {
  112. name: "SaasMiniprogramCamera",
  113. inject: ["paramsFn"],
  114. props: {
  115. visible: {
  116. type: Boolean,
  117. default: false,
  118. },
  119. },
  120. data() {
  121. return {
  122. isShow: false,
  123. isTaking: true,
  124. avatarUrl: "",
  125. isCameraAuth: false,
  126. showSet: false,
  127. popupPhotoShow: false,
  128. status: 1, //1未录制 2录制中 3录制完成
  129. takeVideoModal: false,
  130. mediaRecorder: null,
  131. videoblob: null,
  132. url: null,
  133. faceUrl: null,
  134. showCenter:true
  135. };
  136. },
  137. methods: {
  138. showcenter(){
  139. this.showCenter=!this.showCenter;
  140. },
  141. error(err) {
  142. console.log(err)
  143. },
  144. async submit() {
  145. if (this.uploadLock) {
  146. return;
  147. }
  148. this.uploadLock = true;
  149. let imgUrl = await this.imageInfos()
  150. let compareFaceData = await this.faceRecognition(imgUrl);
  151. if (compareFaceData.code === 200) {
  152. this.$emit("submitTakeVideo", this.videoblob);
  153. this.closeCamera();
  154. } else {
  155. uni.showToast({
  156. title: compareFaceData.msg,
  157. icon: "none",
  158. duration: 2000,
  159. });
  160. setTimeout(() => {
  161. if (!this.isTaking) {
  162. this.reTake();
  163. }
  164. }, 2000);
  165. }
  166. },
  167. async imageInfos() {
  168. let resPath = await myCompressImage(this.avatarUrl || this.faceUrl, 50);
  169. const waitUpload = await this.$method.uploadFile(resPath, 0);
  170. return waitUpload;
  171. },
  172. closeCamera() {
  173. this.isShow = false;
  174. this.showSet = false;
  175. this.popupPhotoShow = false;
  176. this.status=1;
  177. },
  178. openCamera() {
  179. console.log("openCamera");
  180. this.uploadLock = false;
  181. this.popupPhotoShow = false;
  182. // 同一个商品只谈一次提示
  183. // let popupList = uni.getStorageSync("popupList") || [];
  184. // if (!popupList.includes(this.goodsId)) {
  185. // popupList.push(this.goodsId);
  186. // uni.setStorageSync("popupList", popupList);
  187. // this.popupPhotoShow = true;
  188. // return;
  189. // }
  190. // #ifdef MP-WEIXIN
  191. // 屏幕亮度
  192. uni.setKeepScreenOn({
  193. keepScreenOn: true,
  194. });
  195. uni.getSetting({
  196. success: (res) => {
  197. if (res.authSetting["scope.camera"]) {
  198. this.isCameraAuth = true;
  199. this.showCamera();
  200. } else {
  201. wx.authorize({
  202. scope: "scope.camera",
  203. success: () => {
  204. this.isCameraAuth = true;
  205. this.showCamera();
  206. },
  207. fail: () => {
  208. this.isCameraAuth = false;
  209. this.showSet = true;
  210. },
  211. });
  212. }
  213. },
  214. fail: (res) => {},
  215. });
  216. // #endif
  217. // #ifdef H5
  218. if (
  219. (window.navigator.mediaDevices &&
  220. window.navigator.mediaDevices.getUserMedia) ||
  221. window.navigator.getUserMedia ||
  222. window.navigator.webkitGetUserMedia ||
  223. window.navigator.mozGetUserMedia
  224. ) {
  225. console.log("getUserMedia----");
  226. // 调用用户媒体设备, 访问摄像头
  227. this.getUserMedia({
  228. video: {
  229. width: 400,
  230. height: 400,
  231. facingMode: "user",
  232. },
  233. },
  234. this.photographSuccess,
  235. this.photographError
  236. );
  237. } else {
  238. console.log("1111没有摄像");
  239. this.photographError();
  240. }
  241. // #endif
  242. },
  243. showCamera() {
  244. this.isTaking = true;
  245. this.isShow = true;
  246. this.showCenter=true;
  247. },
  248. //确认拍照
  249. takePhoto() {
  250. // #ifdef MP-WEIXIN
  251. const ctx = uni.createCameraContext();
  252. ctx.startRecord
  253. ctx.takePhoto({
  254. quality: "high",
  255. success: (res) => {
  256. this.avatarUrl = res.tempImagePath;
  257. console.log("开始拍照this.avatarUrl:", this.avatarUrl);
  258. this.isTaking = false;
  259. },
  260. fail: (err) => {},
  261. });
  262. // #endif
  263. // #ifdef H5
  264. this.luzhi();
  265. // const canvas = document.createElement("canvas");
  266. // canvas.width = 400;
  267. // canvas.height = 400;
  268. // const context = canvas.getContext("2d");
  269. // const box = document.querySelector(".photo_v");
  270. // const video = box.querySelector("video");
  271. // context.drawImage(video, 0, 0, 400, 400);
  272. // this.faceUrl = canvas.toDataURL("image/png");
  273. // this.isTaking = false;
  274. // #endif
  275. },
  276. takePhTips() {
  277. this.popupPhotoShow = true;
  278. this.isShow = false;
  279. this.isTaking = false;
  280. },
  281. /**
  282. * 人脸匹配
  283. */
  284. faceRecognition(url) {
  285. return new Promise((resolve) => {
  286. // // #ifdef MP-WEIXIN
  287. // let fileSystem = uni.getFileSystemManager();
  288. // fileSystem.readFile({
  289. // filePath: `${this.avatarUrl}`,
  290. // encoding: "base64",
  291. // position: 0,
  292. // success: (res) => {
  293. // let base64 = "data:image/jpg;base64," + res.data;
  294. // this.CompareFace(base64, resolve);
  295. // },
  296. // fail(err) {
  297. // // this.$u.toast('人脸识别错误!')
  298. // console.error(err, "err-----人脸识别错误");
  299. // },
  300. // });
  301. // // #endif
  302. // // #ifdef H5
  303. // this.CompareFace(this.faceUrl, resolve);
  304. // // #endif
  305. this.CompareFace(url, resolve);
  306. });
  307. },
  308. CompareFace(url, resolve) {
  309. let timer = setTimeout(() => {
  310. uni.showToast({
  311. icon: "none",
  312. title: "录像超时,请重新录制视频",
  313. duration: 2000,
  314. success: () => {
  315. setTimeout(() => {
  316. uni.navigateBack();
  317. }, 1000);
  318. },
  319. });
  320. }, 10 * 1000);
  321. this.$api
  322. .faceCertificationCompareFace({
  323. urlA: url,
  324. // imageA: url,
  325. orderGoodsId: this.params.orderGoodsId,
  326. gradeId: this.params.gradeId,
  327. })
  328. .then((res) => {
  329. clearTimeout(timer);
  330. console.log(res, "人脸识别成功res");
  331. resolve(res.data);
  332. })
  333. .catch((err) => {
  334. clearTimeout(timer);
  335. // 当前网络延迟,
  336. console.log("人脸识别错误:", err);
  337. uni.showModal({
  338. content: "当前网络延迟",
  339. showCancel: false,
  340. success: (resultst) => {
  341. if (resultst.confirm) {
  342. uni.navigateBack();
  343. }
  344. },
  345. });
  346. });
  347. },
  348. photographSuccess(stream) {
  349. console.log("有摄像头---", stream);
  350. this.isCameraAuth = true;
  351. this.showCamera();
  352. this.$nextTick(() => {
  353. const box = document.querySelector(".photo_v");
  354. const video = box.querySelector("video");
  355. video.srcObject = stream;
  356. this.mediaRecorder = new MediaRecorder(stream);
  357. video.play();
  358. });
  359. },
  360. photographError(err) {
  361. console.log("没有摄像头:", err);
  362. uni.showModal({
  363. title: "提示",
  364. content: "课程学习需要开启摄像头进行拍照,经检测您的设备无摄像头可使用,请检测环境是否支持。",
  365. cancelText: "取消",
  366. confirmText: "确定",
  367. showCancel: false,
  368. success: (res) => {
  369. if (res.confirm) {
  370. uni.navigateBack();
  371. } else if (res.cancel) {}
  372. },
  373. });
  374. },
  375. getUserMedia(constraints, success, error) {
  376. console.log("getUserMedia===", constraints);
  377. if (window.navigator.mediaDevices.getUserMedia) {
  378. // 最新的标准API
  379. window.navigator.mediaDevices
  380. .getUserMedia(constraints)
  381. .then(success)
  382. .catch(error);
  383. } else if (window.navigator.webkitGetUserMedia) {
  384. // webkit核心浏览器
  385. window.navigator.webkitGetUserMedia(constraints, success, error);
  386. } else if (window.navigator.mozGetUserMedia) {
  387. // firfox浏览器
  388. window.navigator.mozGetUserMedia(constraints, success, error);
  389. } else if (window.navigator.getUserMedia) {
  390. // 旧版API
  391. window.navigator.getUserMedia(constraints, success, error);
  392. }
  393. },
  394. again() {
  395. this.url = null;
  396. this.status = 1;
  397. this.showCenter=true;
  398. },
  399. luzhi() {
  400. if (this.status === 3) {
  401. //完成提交
  402. this.submit();
  403. return;
  404. }
  405. this.onCatchPhoto();
  406. this.mediaRecorder.start();
  407. this.status = 2;
  408. setTimeout(() => {
  409. this.stop();
  410. this.status = 3;
  411. }, 3500);
  412. },
  413. //拍照
  414. onCatchPhoto() {
  415. const canvas = document.createElement("canvas");
  416. canvas.width = 400;
  417. canvas.height = 400;
  418. const context = canvas.getContext("2d");
  419. const box = document.querySelector(".photo_v");
  420. const video = box.querySelector("video");
  421. context.drawImage(video, 0, 0, 400, 400);
  422. this.faceUrl = canvas.toDataURL("image/png");
  423. // console.log(this.faceUrl,'faceUrl')
  424. },
  425. stop() {
  426. this.mediaRecorder.stop();
  427. this.mediaRecorder.ondataavailable = (event) => {
  428. //this.$message.success("录制完成");
  429. // console.log(event.data, 'data')
  430. // 尝试转换为mp4类型
  431. const blob = event.data.slice(0, event.data.size, "video/mp4");
  432. this.url = URL.createObjectURL(blob);
  433. this.videoblob = blob;
  434. console.log(event.data, 'url')
  435. // const file = new window.File([blob], "videoss.mp4", {
  436. // type: 'video/mp4',
  437. // });
  438. // console.log(file, 'file')
  439. };
  440. },
  441. },
  442. computed: {
  443. params() {
  444. return this.paramsFn();
  445. },
  446. goodsId() {
  447. return this.params.goodsId;
  448. },
  449. },
  450. components: {
  451. PopupPhoto,
  452. },
  453. watch: {
  454. visible(val) {
  455. if (val) {
  456. this.takePhTips()//this.openCamera();
  457. } else {
  458. this.closeCamera();
  459. }
  460. },
  461. },
  462. };
  463. </script>
  464. <style lang="scss" scoped>
  465. .btnSet {
  466. width: 440rpx;
  467. height: 80rpx;
  468. background: #007aff;
  469. border-radius: 40rpx;
  470. color: #ffffff;
  471. font-size: 28rpx;
  472. line-height: 80rpx;
  473. }
  474. .photoBox {
  475. width: 100%;
  476. .photoTop {
  477. width: 100%;
  478. height: 74rpx;
  479. border-radius: 20px 20px 0px 0px;
  480. background-color: #ffffff;
  481. display: flex;
  482. align-items: center;
  483. justify-content: center;
  484. padding: 0rpx 38rpx;
  485. .centersq {
  486. color: #333;
  487. font-size: 30rpx;
  488. font-weight: 500;
  489. }
  490. }
  491. .photoCenter {
  492. width: 750rpx;
  493. height: 75vh;
  494. position: relative;
  495. .center_camera {
  496. width: 100%;
  497. height: 75vh;
  498. position: fixed;
  499. background-color: rgba(0, 0, 0, .8);
  500. .head_take {
  501. width: 100%;
  502. height: 75vh;
  503. display: flex;
  504. flex-direction: column;
  505. }
  506. .headTake_up {
  507. width: 100%;
  508. height: 100rpx;
  509. }
  510. .headTake_minddle {
  511. display: flex;
  512. .min_img {
  513. width: 500rpx;
  514. height: 550rpx;
  515. }
  516. .min_left,
  517. .min_right {
  518. flex: 1;
  519. height: 550rpx;
  520. }
  521. }
  522. .headTake_down {
  523. width: 100%;
  524. flex: 1;
  525. }
  526. .color {
  527. background-color: #333;
  528. opacity: 0.5;
  529. }
  530. .photo_v {
  531. display: block;
  532. margin: 0 auto;
  533. width: 400px;
  534. height: 400px;
  535. }
  536. .photo_v2 {
  537. display: block;
  538. margin: 0 auto;
  539. width: 400px;
  540. height: 400px;
  541. }
  542. .mask {
  543. width: 500rpx;
  544. height: 550rpx;
  545. position: absolute;
  546. top: 100rpx;
  547. left: 0;
  548. right: 0;
  549. bottom: 0;
  550. margin: 0 auto;
  551. box-shadow: 0 0 0 2000px rgba(0, 0, 0, 0.4);
  552. }
  553. }
  554. .custom {
  555. width: 750rpx;
  556. height: 75vh;
  557. position: absolute;
  558. z-index: 1000;
  559. top: 0;
  560. left: 0;
  561. background-color: rgba(0, 0, 0, .8);
  562. image {
  563. display: block;
  564. margin: 0 auto;
  565. width: 400px;
  566. height: 400px;
  567. }
  568. }
  569. }
  570. .btns {
  571. display: flex;
  572. .takePhoto_btn {
  573. width: 100%;
  574. display: flex;
  575. align-items: center;
  576. justify-content: space-between;
  577. background: #a9a7a9;
  578. padding: 40rpx 26rpx;
  579. .middle_btn {
  580. width: 120rpx;
  581. height: 120rpx;
  582. border-radius: 40rpx;
  583. border: 4rpx solid #ffffff;
  584. display: flex;
  585. align-items: center;
  586. justify-content: center;
  587. }
  588. .square {
  589. width: 96rpx;
  590. height: 96rpx;
  591. background: #ffffff;
  592. border-radius: 28rpx;
  593. }
  594. .rights {
  595. font-size: 32rpx;
  596. font-weight: 500;
  597. color: #ffffff;
  598. }
  599. }
  600. .btnResult {
  601. height: 100rpx;
  602. flex: 1;
  603. background-color: #07c160;
  604. text-align: center;
  605. line-height: 100rpx;
  606. color: #fff;
  607. font-size: 32rpx;
  608. font-weight: bold;
  609. }
  610. }
  611. }
  612. .popup_box {
  613. width: 100%;
  614. height: 1100rpx;
  615. display: flex;
  616. align-items: center;
  617. flex-direction: column;
  618. padding: 0rpx 40rpx;
  619. .photo_head {
  620. width: 240rpx;
  621. height: 240rpx;
  622. margin: 80rpx 0rpx 48rpx;
  623. }
  624. .warn {
  625. width: 100%;
  626. font-size: 36rpx;
  627. font-weight: bold;
  628. color: #222222;
  629. margin-bottom: 40rpx;
  630. }
  631. .words {
  632. font-size: 28rpx;
  633. color: #666666;
  634. line-height: 33rpx;
  635. margin-bottom: 24rpx;
  636. }
  637. .take_photo {
  638. width: 412rpx;
  639. height: 84rpx;
  640. line-height: 84rpx;
  641. text-align: center;
  642. background: #01C65A;
  643. border-radius: 16rpx;
  644. font-size: 36rpx;
  645. font-weight: 500;
  646. color: #FFFFFF;
  647. margin-top: 100rpx;
  648. }
  649. }
  650. </style>