faceSea.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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"]),
  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"]),
  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. uni.getSystemInfo({
  112. success: function (res2) {
  113. that.maxWidth = res2.screenWidth;
  114. let base = 4 / 3;
  115. that.maxHeight = that.maxWidth * base;
  116. },
  117. });
  118. that.imgPath = res.tempFilePaths[0]; //这就是要的blod
  119. that.toBase64(that.imgPath); //转base64图片
  120. //图片显示
  121. if (that.tipFlag == "ios") {
  122. let canvas = uni.createCanvasContext("canvas");
  123. canvas.drawImage(that.imgPath, 0, 0, that.maxWidth, that.maxHeight);
  124. canvas.draw();
  125. this.takeToReset(); //图片显示后显示重拍控件
  126. } else {
  127. that.detail(that.imgPath);
  128. }
  129. },
  130. });
  131. },
  132. //安卓机图片修正
  133. async detail(url) {
  134. let Orientation = 1;
  135. //获取图片META信息
  136. await this.getImageTag(url, "Orientation", function (e) {
  137. if (e != undefined) Orientation = e;
  138. });
  139. var img = null;
  140. var canvas = null;
  141. await this.comprossImage(url, function (e) {
  142. img = e.img;
  143. canvas = e.canvas;
  144. });
  145. // console.log(Orientation,"Orientation")
  146. //如果方向角不为1,都需要进行旋转
  147. switch (Orientation) {
  148. case 6: //需要顺时针(向右)90度旋转
  149. this.rotateImg(img, "right", canvas);
  150. break;
  151. case 8: //需要逆时针(向左)90度旋转
  152. this.rotateImg(img, "left", canvas);
  153. break;
  154. case 3: //需要180度旋转 转两次
  155. this.rotateImg(img, "right", canvas, 2);
  156. break;
  157. default:
  158. this.rotateImg(img, "", canvas);
  159. break;
  160. }
  161. },
  162. async comprossImage(imgSrc, func) {
  163. if (!imgSrc) return 0;
  164. return new Promise((resolve, reject) => {
  165. uni.getImageInfo({
  166. src: imgSrc,
  167. success(res) {
  168. let img = new Image();
  169. img.src = res.path;
  170. // console.log(img);
  171. let canvas = uni.createCanvasContext("canvas");
  172. let obj = new Object();
  173. obj.img = img;
  174. obj.canvas = canvas;
  175. resolve(func(obj));
  176. },
  177. });
  178. });
  179. },
  180. getImageTag(file, tag, suc) {
  181. if (!file) return 0;
  182. return new Promise((resolve, reject) => {
  183. /* eslint-disable func-names */
  184. // 箭头函数会修改this,所以这里不能用箭头函数
  185. let imgObj = new Image();
  186. imgObj.src = file;
  187. // console.log(imgObj);
  188. uni.getImageInfo({
  189. src: file,
  190. success(res) {
  191. EXIF.getData(imgObj, function () {
  192. EXIF.getAllTags(this);
  193. let or = EXIF.getTag(this, "Orientation"); //这个Orientation 就是我们判断需不需要旋转的值了,有1、3、6、8
  194. resolve(suc(or));
  195. });
  196. },
  197. });
  198. });
  199. },
  200. rotateImg(img, direction, canvas, times = 1) {
  201. // console.log("开始旋转");
  202. //最小与最大旋转方向,图片旋转4次后回到原方向
  203. var min_step = 0;
  204. var max_step = 3;
  205. if (img == null) return;
  206. //img的高度和宽度不能在img元素隐藏后获取,否则会出错
  207. var height = img.height;
  208. var width = img.width;
  209. let maxWidth = this.maxWidth;
  210. let maxHeight = this.maxHeight;
  211. var step = 0;
  212. if (step == null) {
  213. step = min_step;
  214. }
  215. if (direction == "right") {
  216. step += times;
  217. //旋转到原位置,即超过最大值
  218. step > max_step && (step = min_step);
  219. } else if (direction == "left") {
  220. step -= times;
  221. step < min_step && (step = max_step);
  222. } else {
  223. //不旋转
  224. step = 0;
  225. }
  226. //旋转角度以弧度值为参数
  227. var degree = (step * 90 * Math.PI) / 180;
  228. var ctx = uni.createCanvasContext("canvas");
  229. // console.log(degree);
  230. // console.log(step);
  231. switch (step) {
  232. case 1:
  233. // console.log("右旋转 90度");
  234. width = maxHeight;
  235. height = maxWidth;
  236. ctx.rotate(degree);
  237. ctx.drawImage(img.src, 0, -height, width, height);
  238. ctx.draw();
  239. this.takeToReset();
  240. break;
  241. case 2:
  242. //console.log('旋转 180度')
  243. width = maxWidth;
  244. height = maxHeight;
  245. ctx.rotate(degree);
  246. ctx.drawImage(img.src, -width, -height, width, height);
  247. ctx.draw();
  248. this.takeToReset();
  249. break;
  250. case 3:
  251. // console.log("左旋转 90度");
  252. width = maxHeight;
  253. height = maxWidth;
  254. ctx.rotate(degree);
  255. ctx.drawImage(img.src, -width, 0, width, height);
  256. ctx.draw();
  257. this.takeToReset();
  258. break;
  259. default:
  260. //不旋转
  261. width = maxWidth;
  262. height = maxHeight;
  263. ctx.drawImage(img.src, 0, 0, width, height);
  264. ctx.draw();
  265. this.takeToReset();
  266. break;
  267. }
  268. },
  269. //上传图片
  270. upload() {
  271. let idnum = this.$store.state.idnum;
  272. let image = this.$store.state.imageBase;
  273. this.isShow3 = !this.isShow3;
  274. uni.showToast({
  275. title: "人脸匹配中",
  276. icon: "loading",
  277. mask: true,
  278. duration: 2000,
  279. });
  280. if (idnum && image) {
  281. uni.request({
  282. url: "https://jtishfw.ncjti.edu.cn/yinxin/ncjtSecurityManagement/verifyBase64ImagesWithIDNumber",
  283. data: {
  284. idnum: idnum,
  285. image: image,
  286. },
  287. header: { "content-type": "application/x-www-form-urlencoded" },
  288. method: "POST",
  289. sslVerify: true,
  290. success: ({ data, statusCode, header }) => {
  291. if (data.error) {
  292. uni.showToast({
  293. title: "人脸匹配失败",
  294. icon: "error",
  295. mask: true,
  296. duration: 1000,
  297. });
  298. setTimeout(this.back, 1000);
  299. } else {
  300. this.$store.state.sex = data.sex;
  301. this.$store.state.examNumber = data.examNumber;
  302. setTimeout(this.uploadSucceed, 500);
  303. }
  304. },
  305. fail: () => {
  306. uni.showToast({
  307. title: "人脸提交失败",
  308. icon: "error",
  309. mask: true,
  310. duration: 1000,
  311. });
  312. setTimeout(this.back, 1000);
  313. },
  314. });
  315. } else {
  316. uni.showToast({
  317. title: "image或idnum不能为空",
  318. icon: "error",
  319. mask: true,
  320. duration: 1500,
  321. });
  322. this.isShow3 = !this.isShow3;
  323. }
  324. },
  325. //人脸采集成功
  326. uploadSucceed() {
  327. this.hintWord = "人脸匹配成功";
  328. this.hintImage = "../../static/images/success@2x.png";
  329. this.isShow3 = !this.isShow3;
  330. this.takeToReset();
  331. setTimeout(this.navigateToConfirm, 1500);
  332. },
  333. //图片转Base64
  334. toBase64(path) {
  335. pathToBase64(path)
  336. .then((base64) => {
  337. this.$store.state.imageBase = base64;
  338. console.log(this.$store.state.imageBase);
  339. })
  340. .catch((error) => {
  341. console.error(error);
  342. });
  343. },
  344. //跳转人脸采集确认页面
  345. navigateToConfirm() {
  346. uni.navigateTo({
  347. url: "/pages/confirm/confirm",
  348. });
  349. },
  350. //返回重新拍照
  351. back() {
  352. uni.reLaunch({
  353. url: "/pages/faceSearch/faceSea",
  354. });
  355. },
  356. },
  357. };
  358. </script>
  359. <style lang="scss">
  360. @import url("./css/faceSea.min.css");
  361. </style>