camera.vue 14 KB

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