faceRegister.nvue 6.8 KB

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