camera.vue 14 KB

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