faceRegister.nvue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <template>
  2. <view class="main" id="main">
  3. <epii-camera ref="camera" class="epiiCamer" :style="{width:width,height:height}"></epii-camera>
  4. <cover-view>
  5. <image :style="{width:width,height:height}" src="../../static/sl.gif" mode="scaleToFill"></image>
  6. </cover-view>
  7. </view>
  8. </template>
  9. <script>
  10. import http from '../../common/http.js'
  11. export default {
  12. data() {
  13. return {
  14. type: 1,
  15. cameraId: 0,
  16. height: "500rpx",
  17. width: "750rpx",
  18. userId: '',
  19. }
  20. },
  21. onLoad(options) {
  22. this.type = options.type || 1;
  23. this.height = uni.getSystemInfoSync().screenHeight + 'px';
  24. this.$nextTick(() => {
  25. //这里可以做打开摄像头等功能
  26. this.openCamera(1)
  27. })
  28. },
  29. onShow() {
  30. this.userId = uni.getStorageSync('info').id;
  31. },
  32. beforeDestroy() {
  33. this.release();
  34. },
  35. mounted() {
  36. //console.log(this.$refs.camera)
  37. //this.common.hidingLoading();
  38. },
  39. methods: {
  40. takePicture(e) {
  41. const that = this;
  42. that.$refs.camera.takePicture({
  43. base64: true,
  44. quailty: 55,
  45. }, function(data) {
  46. let img = data.img;
  47. that.registerFace(img);
  48. return
  49. });
  50. },
  51. registerFace(img) {
  52. if (!img) {
  53. return;
  54. }
  55. const that = this;
  56. let data = {
  57. id: that.userId,
  58. img: img,
  59. type: that.type
  60. };
  61. let header = {
  62. satoken: uni.getStorageSync('token'),
  63. 'Content-Type': 'application/x-www-form-urlencoded'
  64. };
  65. uni.request({
  66. url: http.ip + '/sp-admin/app/AppUser/faceImg',
  67. data: data,
  68. method: 'POST',
  69. header: header,
  70. dataType: 'json',
  71. success(resp) {
  72. let res = resp.data;
  73. if (res.code == 200) {
  74. that.release();
  75. uni.showToast({
  76. title: that.type == 1 ? '录入成功' : '验证成功'
  77. });
  78. let user = uni.getStorageSync('info');
  79. user.face = 1;
  80. uni.setStorageSync('info', user)
  81. setTimeout(() => {
  82. uni.$emit('face', true);
  83. uni.navigateBack();
  84. uni.hideLoading();
  85. }, 1000)
  86. } else {
  87. uni.showToast({
  88. icon: 'none',
  89. title: res.msg
  90. })
  91. setTimeout(function() {
  92. that.takePicture();
  93. }, 800);
  94. }
  95. }
  96. })
  97. },
  98. release() {
  99. this.$refs.camera.release();
  100. },
  101. openCamera(id) {
  102. if (uni.getSystemInfoSync().platform === "ios") {
  103. let that = this;
  104. this.$refs.camera.openCamera({
  105. cameraId: id,
  106. }, function(data) {
  107. setTimeout(function() {
  108. that.takePicture();
  109. }, 3000);
  110. });
  111. } else {
  112. //android 系统需要考虑 组件的宽高 和摄像头的宽高比例,如果不一致会导致拉伸
  113. this.openCamera_1(id);
  114. }
  115. },
  116. openCamera_1(id) {
  117. //安卓解决拉伸问题,一般有两种思路。
  118. //1. 组件宽高不变,找出最适合的分辨比率
  119. //2. 根据摄像头的分辨率,来重新修改组件的宽和高。
  120. this.cid = id;
  121. let type = 2; //1 组件宽高不变,找出最适合的分辨比率 2根据摄像头的分辨率,来重新修改组件的宽和高。
  122. this.$refs.camera.getSupportedPreviewSizesByCameraId({
  123. cameraId: id
  124. }, (data) => {
  125. let bestHeight_index = 0;
  126. //方法1 组件宽高不变,找出最适合的分辨比率,这样只能减少拉伸,不能解决
  127. if (type == 1) {
  128. let r0 = (this.height * 1.0) / this.width;
  129. let diff = 1000;
  130. for (var i = 0; i < data.sizes.length; i++) {
  131. //找出最合适的分辨率,宽高比例尽量接近组件的(摄像头宽高是反着呢)
  132. let tmp = Math.abs(r0 - (data.sizes[i].width * 1.0) / data.sizes[bestHeight_index]
  133. .width);
  134. if (tmp < diff) {
  135. diff = tmp;
  136. bestHeight_index = i;
  137. }
  138. }
  139. //方法2 先获取摄像头 支持的分辨率,然后修改组件的大小(一般保持宽不变),这样肯定不拉伸
  140. } else if (type == 2) {
  141. for (var i = 0; i < data.sizes.length; i++) {
  142. //找出最大高度对应的分辨比率(宽高是反着呢)
  143. if (data.sizes[i].width > data.sizes[bestHeight_index].width) {
  144. bestHeight_index = i;
  145. }
  146. }
  147. //this.width = data.sizes[max_index].height;
  148. //计算出当前宽度下最适合的高度,然后修改组件的高
  149. /* this.height = ((data.sizes[bestHeight_index].width / (data.sizes[bestHeight_index].height *
  150. 1.0)) * 750) + "rpx"; */
  151. // 3 用最大分辨比率,不考虑拉伸
  152. } else if (type == 3) {
  153. bestHeight_index = 0; //最大的一般是第一个,你也可以遍历后找出最大分辨比率
  154. }
  155. //延迟打开摄像头,组件大小修改需要时间
  156. const that = this;
  157. setTimeout(() => {
  158. this.$refs.camera.openCamera({
  159. cameraId: id,
  160. previewWidth: data.sizes[bestHeight_index].width,
  161. previewHeight: data.sizes[bestHeight_index].height
  162. }, function(data) {
  163. that.takePicture();
  164. });
  165. }, 500);
  166. });
  167. return;
  168. },
  169. switchCamera() {
  170. this.cameraId == 0 ? this.cameraId = 1 : this.cameraId = 0
  171. this.$refs.camera.switchCamera({
  172. cameraId: this.cameraId
  173. }, function(data) {
  174. uni.showModal({
  175. content: JSON.stringify(data)
  176. })
  177. });
  178. },
  179. start() {
  180. this.$refs.camera.startRecord({
  181. videoEncodingBitRate: 3 * 1920 * 1080
  182. });
  183. },
  184. stop() {
  185. this.$refs.camera.stopRecord(function(data) {
  186. //可以通过 uni.compressVideo 压缩视频 记得前缀 file://
  187. uni.showModal({
  188. content: JSON.stringify(data)
  189. })
  190. });
  191. },
  192. release() {
  193. this.$refs.camera.release();
  194. },
  195. flashOpen() {
  196. this.$refs.camera.flashOpen();
  197. },
  198. flashClose() {
  199. this.$refs.camera.flashClose();
  200. },
  201. changeSize() {
  202. //this.height = "1100rpx";
  203. },
  204. getSupportedPreviewSizes() {
  205. this.$refs.camera.getSupportedPreviewSizes((ret) => {
  206. uni.showModal({
  207. content: JSON.stringify(ret)
  208. })
  209. });
  210. }
  211. }
  212. }
  213. </script>
  214. <style scoped>
  215. .main {
  216. position: relative;
  217. width: 750rpx;
  218. flex: 1;
  219. overflow: hidden;
  220. }
  221. .operate-bg {
  222. width: 750rpx;
  223. background-color: #000000;
  224. position: absolute;
  225. left: 0;
  226. bottom: 0;
  227. justify-content: center;
  228. align-items: center;
  229. padding-top: 60rpx;
  230. padding-bottom: 60rpx;
  231. }
  232. .operate {
  233. width: 140rpx;
  234. height: 140rpx;
  235. background-color: #fff;
  236. border-radius: 50%;
  237. border-width: 10rpx;
  238. border-style: solid;
  239. border-color: #453f3f;
  240. }
  241. .change {
  242. width: 96rpx;
  243. height: 96rpx;
  244. background-color: #453f3f;
  245. position: absolute;
  246. right: 60rpx;
  247. bottom: 82rpx;
  248. border-radius: 48rpx;
  249. align-items: center;
  250. justify-content: center;
  251. }
  252. .change-img {
  253. width: 56rpx;
  254. height: 56rpx;
  255. }
  256. .portrait-frame {
  257. width: 750rpx;
  258. flex: 1;
  259. position: absolute;
  260. top: 0;
  261. left: 0;
  262. right: 0;
  263. bottom: 0;
  264. align-items: center;
  265. justify-content: center;
  266. background-color: rgba(0, 0, 0, 0);
  267. }
  268. .pf-img {
  269. width: 633.6rpx;
  270. height: 582rpx;
  271. margin-top: -280rpx;
  272. }
  273. .epiiCamer {
  274. position: absolute;
  275. top: 0;
  276. right: 0;
  277. bottom: 0;
  278. left: 0;
  279. }
  280. </style>