faceRegister.nvue 6.8 KB

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