faceSea.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <template>
  2. <!-- 人脸识别页面 -->
  3. <view class="content">
  4. <!-- 拍照区域 -->
  5. <view class="cameraField">
  6. <!-- 上传的人脸图片 -->
  7. <canvas
  8. canvas-id="canvas"
  9. id="canvas"
  10. type="2d"
  11. style="width: 100%; height: 100%"
  12. ></canvas>
  13. <!-- 提示框 -->
  14. <view class="hint" v-if="isShow3">
  15. <image :src="hintImage" />
  16. {{ hintWord }}
  17. </view>
  18. <!-- 头像限制框 -->
  19. <image
  20. class="head"
  21. src="../../static/images/head1.png"
  22. mode="scaleToFill"
  23. />
  24. </view>
  25. <!-- 控件区域 -->
  26. <view class="controlField">
  27. <!-- 拍照控件 -->
  28. <view class="control1" v-if="isShow1">
  29. <view class="light">
  30. <!-- <image class="light" src="../../static/images/light.png" /> -->
  31. </view>
  32. <view @click="takePhoto()">
  33. <image class="take" src="../../static/images/take.png" />
  34. </view>
  35. <view class="change">
  36. <!-- <image class="change" src="../../static/images/change.png" /> -->
  37. </view>
  38. </view>
  39. <!-- 重拍/上传控件 -->
  40. <view class="control2" v-if="isShow2">
  41. <view @click="resetPhoto">重拍</view>
  42. <view @click="upload">使用照片</view>
  43. </view>
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. import { EXIF } from "../../node_modules/exif-js/exif"; //引入exif.js判断图片旋转方向
  49. import { mapState, mapMutations } from "vuex"; //引入vuex
  50. import {
  51. pathToBase64,
  52. base64ToPath,
  53. } from "../../js_sdk/mmmm-image-tools/index.js";
  54. export default {
  55. data() {
  56. return {
  57. isShow1: true, //拍照控件
  58. isShow2: false, //重拍/上传控件
  59. isShow3: true, //提示框
  60. // isShow4: false, //“上传中”提示框
  61. hintImage: "../../static/images/hint@2x.png", //提示图标
  62. hintWord: "请勿遮挡面部", //提示文字
  63. imgPath: "", //上传的人脸图片本地路径
  64. tipFlag: "", //手机机型
  65. maxWidth: "", //手机屏幕宽度
  66. maxHeight: "", //手机屏幕高度
  67. };
  68. },
  69. computed: mapState(["position", "userData"]), //vuex.state
  70. onLoad(options) {
  71. //判断设备机型
  72. let that = this;
  73. (function () {
  74. var u = navigator.userAgent;
  75. var isAndroid = u.indexOf("Android") > -1 || u.indexOf("Linux") > -1; //android终端或者uc浏览器
  76. var isiOS = u.indexOf("iPhone") > -1; //苹果手机
  77. if (isiOS) {
  78. that.tipFlag = "ios";
  79. }
  80. if (isAndroid) {
  81. that.tipFlag = "Android";
  82. }
  83. })();
  84. },
  85. methods: {
  86. ...mapMutations(["getPosition", "getUserData"]), //vuex.mutations
  87. //拍照控件变重拍上传控件
  88. takeToReset() {
  89. this.isShow1 = !this.isShow1;
  90. this.isShow2 = !this.isShow2;
  91. },
  92. //拍照
  93. takePhoto() {
  94. this.photo();
  95. },
  96. //重拍
  97. resetPhoto() {
  98. this.isShow1 = !this.isShow1;
  99. this.isShow2 = !this.isShow2;
  100. this.photo();
  101. },
  102. //照片
  103. photo() {
  104. let that = this;
  105. uni.chooseImage({
  106. count: 1,
  107. sourceType: ["camera"],
  108. sizeType: ["compressed"],
  109. success: (res) => {
  110. //压缩所选图片
  111. // let path = res.tempFilePaths[0]; //压缩图路径
  112. // let targetWidth = 150;
  113. // let targetHeight = 200;
  114. // let ctx = uni.createCanvasContext("canvas");
  115. // ctx.drawImage(path, 0, 0, 300, 400); //x设置成1000是因为不让压缩图出现在屏幕
  116. // ctx.draw(false, () => {
  117. // // canvas导出为图片路径
  118. // uni.canvasToTempFilePath({
  119. // canvasId: "canvas",
  120. // fileType: "jpg", //支持jpg或png
  121. // quality: 0.92, //图片质量
  122. // success(res3) {
  123. // let path1 = res3.tempFilePath;
  124. // that.toBase64(path1); //转base64图片
  125. // },
  126. // });
  127. // }),
  128. //绘制图片到页面
  129. that.imgPath = res.tempFilePaths[0]; //这就是要的blod
  130. //获取设备屏幕尺寸
  131. uni.getSystemInfo({
  132. success: function (res2) {
  133. that.maxWidth = res2.windowWidth;
  134. let base = 4 / 3;
  135. that.maxHeight = that.maxWidth * base;
  136. //判断设备机型
  137. if (that.tipFlag == "ios") {
  138. let canvas = uni.createCanvasContext("canvas");
  139. canvas.drawImage(
  140. that.imgPath,
  141. 0,
  142. 0,
  143. that.maxWidth,
  144. that.maxHeight
  145. ); //苹果机图片显示
  146. canvas.draw();
  147. uni.canvasToTempFilePath({
  148. width: that.maxWidth,
  149. height: that.maxHeight,
  150. destWidth: that.maxWidth,
  151. destHeight: that.maxHeight,
  152. canvasId: "canvas",
  153. fileType: "jpg", //支持jpg或png
  154. quality: 1, //图片质量
  155. success(res3) {
  156. let path1 = res3.tempFilePath;
  157. console.log(path1);
  158. that.$store.state.imageBase = path1;
  159. // that.toBase64(path1); //转base64图片
  160. },
  161. });
  162. that.takeToReset(); //图片显示后显示重拍控件
  163. } else {
  164. that.detail(that.imgPath); //安卓机图片绘制
  165. }
  166. },
  167. });
  168. },
  169. });
  170. },
  171. //安卓机图片修正
  172. async detail(url) {
  173. let Orientation = 1;
  174. //获取图片META信息
  175. await this.getImageTag(url, "Orientation", function (e) {
  176. if (e != undefined) Orientation = e;
  177. });
  178. var img = null;
  179. var canvas = null;
  180. await this.comprossImage(url, function (e) {
  181. img = e.img;
  182. canvas = e.canvas;
  183. });
  184. // console.log(Orientation,"Orientation")
  185. //如果方向角不为1,都需要进行旋转
  186. switch (Orientation) {
  187. case 6: //需要顺时针(向右)90度旋转
  188. this.rotateImg(img, "right", canvas);
  189. break;
  190. case 8: //需要逆时针(向左)90度旋转
  191. this.rotateImg(img, "left", canvas);
  192. break;
  193. case 3: //需要180度旋转 转两次
  194. this.rotateImg(img, "right", canvas, 2);
  195. break;
  196. default:
  197. this.rotateImg(img, "", canvas);
  198. break;
  199. }
  200. },
  201. async comprossImage(imgSrc, func) {
  202. if (!imgSrc) return 0;
  203. return new Promise((resolve, reject) => {
  204. uni.getImageInfo({
  205. src: imgSrc,
  206. success(res) {
  207. let img = new Image();
  208. img.src = res.path;
  209. // console.log(img);
  210. let canvas = uni.createCanvasContext("canvas");
  211. let obj = new Object();
  212. obj.img = img;
  213. obj.canvas = canvas;
  214. resolve(func(obj));
  215. },
  216. });
  217. });
  218. },
  219. getImageTag(file, tag, suc) {
  220. if (!file) return 0;
  221. return new Promise((resolve, reject) => {
  222. /* eslint-disable func-names */
  223. // 箭头函数会修改this,所以这里不能用箭头函数
  224. let imgObj = new Image();
  225. imgObj.src = file;
  226. // console.log(imgObj);
  227. uni.getImageInfo({
  228. src: file,
  229. success(res) {
  230. EXIF.getData(imgObj, function () {
  231. EXIF.getAllTags(this);
  232. let or = EXIF.getTag(this, "Orientation"); //这个Orientation 就是我们判断需不需要旋转的值了,有1、3、6、8
  233. resolve(suc(or));
  234. });
  235. },
  236. });
  237. });
  238. },
  239. rotateImg(img, direction, canvas, times = 1) {
  240. // console.log("开始旋转");
  241. //最小与最大旋转方向,图片旋转4次后回到原方向
  242. var min_step = 0;
  243. var max_step = 3;
  244. if (img == null) return;
  245. //img的高度和宽度不能在img元素隐藏后获取,否则会出错
  246. var height = img.height;
  247. var width = img.width;
  248. let maxWidth = this.maxWidth;
  249. let maxHeight = this.maxHeight;
  250. var step = 0;
  251. if (step == null) {
  252. step = min_step;
  253. }
  254. if (direction == "right") {
  255. step += times;
  256. //旋转到原位置,即超过最大值
  257. step > max_step && (step = min_step);
  258. } else if (direction == "left") {
  259. step -= times;
  260. step < min_step && (step = max_step);
  261. } else {
  262. //不旋转
  263. step = 0;
  264. }
  265. //旋转角度以弧度值为参数
  266. var degree = (step * 90 * Math.PI) / 180;
  267. var ctx = uni.createCanvasContext("canvas");
  268. // console.log(degree);
  269. // console.log(step);
  270. switch (step) {
  271. case 1:
  272. // console.log("右旋转 90度");
  273. width = maxHeight;
  274. height = maxWidth;
  275. ctx.rotate(degree);
  276. ctx.drawImage(img.src, 0, -height, width, height);
  277. ctx.draw();
  278. uni.canvasToTempFilePath({
  279. width: maxHeight,
  280. height: maxWidth,
  281. destWidth: maxHeight,
  282. destHeight: maxWidth,
  283. canvasId: "canvas",
  284. fileType: "jpg", //支持jpg或png
  285. quality: 1, //图片质量
  286. success(res3) {
  287. let path1 = res3.tempFilePath;
  288. console.log(path1);
  289. this.$store.state.imageBase = path1;
  290. // that.toBase64(path1); //转base64图片
  291. },
  292. });
  293. this.takeToReset();
  294. break;
  295. case 2:
  296. //console.log('旋转 180度')
  297. width = maxWidth;
  298. height = maxHeight;
  299. ctx.rotate(degree);
  300. ctx.drawImage(img.src, -width, -height, width, height);
  301. ctx.draw();
  302. uni.canvasToTempFilePath({
  303. width: maxWidth,
  304. height: maxHeight,
  305. destWidth: maxWidth,
  306. destHeight: maxHeight,
  307. canvasId: "canvas",
  308. fileType: "jpg", //支持jpg或png
  309. quality: 1, //图片质量
  310. success(res3) {
  311. let path1 = res3.tempFilePath;
  312. console.log(path1);
  313. this.$store.state.imageBase = path1;
  314. // that.toBase64(path1); //转base64图片
  315. },
  316. });
  317. this.takeToReset();
  318. break;
  319. case 3:
  320. // console.log("左旋转 90度");
  321. width = maxHeight;
  322. height = maxWidth;
  323. ctx.rotate(degree);
  324. ctx.drawImage(img.src, -width, 0, width, height);
  325. ctx.draw();
  326. uni.canvasToTempFilePath({
  327. width: maxHeight,
  328. height: maxWidth,
  329. destWidth: maxHeight,
  330. destHeight: maxWidth,
  331. canvasId: "canvas",
  332. fileType: "jpg", //支持jpg或png
  333. quality: 1, //图片质量
  334. success(res3) {
  335. let path1 = res3.tempFilePath;
  336. console.log(path1);
  337. this.$store.state.imageBase = path1;
  338. // that.toBase64(path1); //转base64图片
  339. },
  340. });
  341. this.takeToReset();
  342. break;
  343. default:
  344. //不旋转
  345. width = maxWidth;
  346. height = maxHeight;
  347. ctx.drawImage(img.src, 0, 0, width, height);
  348. ctx.draw();
  349. uni.canvasToTempFilePath({
  350. width: maxWidth,
  351. height: maxHeight,
  352. destWidth: maxWidth,
  353. destHeight: maxHeight,
  354. canvasId: "canvas",
  355. fileType: "jpg", //支持jpg或png
  356. quality: 1, //图片质量
  357. success(res3) {
  358. let path1 = res3.tempFilePath;
  359. console.log(path1);
  360. this.$store.state.imageBase = path1;
  361. // that.toBase64(path1); //转base64图片
  362. },
  363. });
  364. this.takeToReset();
  365. break;
  366. }
  367. },
  368. //上传图片
  369. upload() {
  370. let idnum = this.$store.state.idnum;
  371. let image = this.$store.state.imageBase;
  372. this.isShow3 = !this.isShow3; //关闭提示框
  373. uni.showToast({
  374. title: "人脸匹配中",
  375. icon: "loading",
  376. mask: true,
  377. duration: 2000,
  378. });
  379. if (idnum && image) {
  380. uni.request({
  381. url: "https://jtishfw.ncjti.edu.cn/yinxin/ncjtSecurityManagement/verifyBase64ImagesWithIDNumber",
  382. data: {
  383. idnum: idnum,
  384. image: image,
  385. },
  386. header: { "content-type": "application/x-www-form-urlencoded" },
  387. method: "POST",
  388. sslVerify: true,
  389. success: ({ data, statusCode, header }) => {
  390. if (data.error) {
  391. uni.showToast({
  392. title: "人脸匹配失败",
  393. icon: "error",
  394. mask: true,
  395. duration: 1000,
  396. });
  397. setTimeout(this.back, 1000);
  398. } else {
  399. this.$store.state.sex = data.sex;
  400. this.$store.state.examNumber = data.examNumber;
  401. setTimeout(this.uploadSucceed, 500);
  402. }
  403. },
  404. fail: () => {
  405. uni.showToast({
  406. title: "人脸提交失败",
  407. icon: "error",
  408. mask: true,
  409. duration: 1000,
  410. });
  411. setTimeout(this.back, 1000);
  412. },
  413. });
  414. } else {
  415. uni.showToast({
  416. title: "image或idnum不能为空",
  417. icon: "error",
  418. mask: true,
  419. duration: 1500,
  420. });
  421. this.isShow3 = !this.isShow3; //显示提示框
  422. }
  423. },
  424. //人脸采集成功
  425. uploadSucceed() {
  426. this.hintWord = "人脸匹配成功";
  427. this.hintImage = "../../static/images/success@2x.png";
  428. this.isShow3 = !this.isShow3;
  429. this.takeToReset();
  430. setTimeout(this.navigateToConfirm, 1500);
  431. },
  432. //图片转Base64
  433. toBase64(path) {
  434. pathToBase64(path)
  435. .then((base64) => {
  436. this.$store.state.imageBase = base64;
  437. console.log(this.$store.state.imageBase);
  438. })
  439. .catch((error) => {
  440. console.error(error);
  441. });
  442. },
  443. //跳转人脸采集确认页面
  444. navigateToConfirm() {
  445. uni.navigateTo({
  446. url: "/pages/confirm/confirm",
  447. });
  448. },
  449. //返回重新拍照
  450. back() {
  451. uni.reLaunch({
  452. url: "/pages/faceSearch/faceSea",
  453. });
  454. },
  455. },
  456. };
  457. </script>
  458. <style lang="scss">
  459. @import url("./css/faceSea.min.css");
  460. </style>