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. type:that.type
  61. },
  62. header: {
  63. satoken: uni.getStorageSync('token')
  64. },
  65. success(resp) {
  66. let res = JSON.parse(resp.data);
  67. if (res.code == 200) {
  68. that.release();
  69. uni.showToast({
  70. title: that.type == 1 ? '录入成功' : '验证成功'
  71. });
  72. let user = uni.getStorageSync('info');
  73. user.face = 1;
  74. uni.setStorageSync('info', user)
  75. setTimeout(() => {
  76. uni.$emit('face', true);
  77. uni.navigateBack();
  78. uni.hideLoading();
  79. }, 1000)
  80. } else {
  81. uni.showToast({
  82. icon: 'none',
  83. title: res.msg
  84. })
  85. setTimeout(function() {
  86. that.takePicture();
  87. }, 800);
  88. }
  89. }
  90. })
  91. },
  92. release() {
  93. this.$refs.camera.release();
  94. },
  95. openCamera(id) {
  96. if (uni.getSystemInfoSync().platform === "ios") {
  97. let that = this;
  98. this.$refs.camera.openCamera({
  99. cameraId: id,
  100. }, function(data) {
  101. setTimeout(function() {
  102. that.takePicture();
  103. }, 1000);
  104. });
  105. } else {
  106. //android 系统需要考虑 组件的宽高 和摄像头的宽高比例,如果不一致会导致拉伸
  107. this.openCamera_1(id);
  108. }
  109. },
  110. openCamera_1(id) {
  111. //安卓解决拉伸问题,一般有两种思路。
  112. //1. 组件宽高不变,找出最适合的分辨比率
  113. //2. 根据摄像头的分辨率,来重新修改组件的宽和高。
  114. this.cid = id;
  115. let type = 2; //1 组件宽高不变,找出最适合的分辨比率 2根据摄像头的分辨率,来重新修改组件的宽和高。
  116. this.$refs.camera.getSupportedPreviewSizesByCameraId({
  117. cameraId: id
  118. }, (data) => {
  119. let bestHeight_index = 0;
  120. //方法1 组件宽高不变,找出最适合的分辨比率,这样只能减少拉伸,不能解决
  121. if (type == 1) {
  122. let r0 = (this.height * 1.0) / this.width;
  123. let diff = 1000;
  124. for (var i = 0; i < data.sizes.length; i++) {
  125. //找出最合适的分辨率,宽高比例尽量接近组件的(摄像头宽高是反着呢)
  126. let tmp = Math.abs(r0 - (data.sizes[i].width * 1.0) / data.sizes[bestHeight_index]
  127. .width);
  128. if (tmp < diff) {
  129. diff = tmp;
  130. bestHeight_index = i;
  131. }
  132. }
  133. //方法2 先获取摄像头 支持的分辨率,然后修改组件的大小(一般保持宽不变),这样肯定不拉伸
  134. } else if (type == 2) {
  135. for (var i = 0; i < data.sizes.length; i++) {
  136. //找出最大高度对应的分辨比率(宽高是反着呢)
  137. if (data.sizes[i].width > data.sizes[bestHeight_index].width) {
  138. bestHeight_index = i;
  139. }
  140. }
  141. //this.width = data.sizes[max_index].height;
  142. //计算出当前宽度下最适合的高度,然后修改组件的高
  143. /* this.height = ((data.sizes[bestHeight_index].width / (data.sizes[bestHeight_index].height *
  144. 1.0)) * 750) + "rpx"; */
  145. // 3 用最大分辨比率,不考虑拉伸
  146. } else if (type == 3) {
  147. bestHeight_index = 0; //最大的一般是第一个,你也可以遍历后找出最大分辨比率
  148. }
  149. //延迟打开摄像头,组件大小修改需要时间
  150. const that = this;
  151. setTimeout(() => {
  152. this.$refs.camera.openCamera({
  153. cameraId: id,
  154. previewWidth: data.sizes[bestHeight_index].width,
  155. previewHeight: data.sizes[bestHeight_index].height
  156. }, function(data) {
  157. that.takePicture();
  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>