idcard.nvue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <template>
  2. <view class="live-camera" :style="{ width: windowWidth, height: windowHeight }">
  3. <view class="preview" :style="{ width: windowWidth, height: windowHeight - 80 }">
  4. <live-pusher
  5. id="livePusher"
  6. ref="livePusher"
  7. class="livePusher"
  8. mode="FHD"
  9. beauty="0"
  10. whiteness="0"
  11. :aspect="aspect"
  12. min-bitrate="1000"
  13. audio-quality="16KHz"
  14. device-position="back"
  15. :auto-focus="true"
  16. :muted="true"
  17. :enable-camera="true"
  18. :enable-mic="false"
  19. :zoom="false"
  20. @statechange="statechange"
  21. :style="{ width: cameraWidth, height: cameraHeight }"
  22. ></live-pusher>
  23. <!--提示语-->
  24. <cover-view class="remind">
  25. <text class="remind-text" style="">{{ message }}</text>
  26. </cover-view>
  27. <!--辅助线-->
  28. <cover-view class="outline-box" :style="{ width: windowWidth, height: windowHeight - 80 }">
  29. <cover-image
  30. class="outline-img"
  31. :src="dotype == 'idcardface' ? '/static/live-camera/outline/idcardface.png' : '/static/live-camera/outline/idcardbadge.png'"
  32. style=""
  33. ></cover-image>
  34. </cover-view>
  35. </view>
  36. <view class="menu">
  37. <!--底部菜单区域背景-->
  38. <cover-image class="menu-mask" src="/static/live-camera/bar.png"></cover-image>
  39. <!--返回键-->
  40. <cover-image class="menu-back" @tap="back" src="/static/live-camera/back.png"></cover-image>
  41. <!--快门键-->
  42. <cover-image class="menu-snapshot" @tap="snapshot" src="/static/live-camera/shutter.png"></cover-image>
  43. <!--反转键-->
  44. <cover-image class="menu-flip" @tap="flip" src="/static/live-camera/flip.png"></cover-image>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. let _this = null;
  50. export default {
  51. data() {
  52. return {
  53. poenCarmeInterval: null, //打开相机的轮询
  54. dotype: 'face', //操作类型
  55. message: '', //提示
  56. aspect: '2:3', //比例
  57. cameraWidth: '', //相机画面宽度
  58. cameraHeight: '', //相机画面宽度
  59. windowWidth: '', //屏幕可用宽度
  60. windowHeight: '', //屏幕可用高度
  61. camerastate: false, //相机准备好了
  62. livePusher: null, //流视频对象
  63. snapshotsrc: null //快照
  64. };
  65. },
  66. onLoad(e) {
  67. _this = this;
  68. this.dotype = e.dotype;
  69. this.initCamera();
  70. },
  71. onReady() {
  72. this.livePusher = uni.createLivePusherContext('livePusher', this);
  73. this.startPreview(); //开启预览并设置摄像头
  74. this.poenCarme();
  75. },
  76. methods: {
  77. //轮询打开
  78. poenCarme() {
  79. //#ifdef APP-PLUS
  80. if (plus.os.name == 'Android') {
  81. this.poenCarmeInterval = setInterval(function() {
  82. console.log(_this.camerastate);
  83. if (!_this.camerastate) _this.startPreview();
  84. }, 2500);
  85. }
  86. //#endif
  87. },
  88. //初始化相机
  89. initCamera() {
  90. //处理安卓手机异步授权问题
  91. uni.getSystemInfo({
  92. success: function(res) {
  93. _this.windowWidth = res.windowWidth;
  94. _this.windowHeight = res.windowHeight;
  95. _this.cameraWidth = res.windowWidth;
  96. _this.cameraHeight = res.windowWidth * 1.5;
  97. }
  98. });
  99. },
  100. //开始预览
  101. startPreview() {
  102. this.livePusher.startPreview({
  103. success: a => {
  104. console.log(a);
  105. }
  106. });
  107. },
  108. //停止预览
  109. stopPreview() {
  110. this.livePusher.stopPreview({
  111. success: a => {
  112. _this.camerastate = false; //标记相机未启动
  113. }
  114. });
  115. },
  116. //状态
  117. statechange(e) {
  118. //状态改变
  119. console.log(e);
  120. if (e.detail.code == 1007) {
  121. _this.camerastate = true;
  122. } else if (e.detail.code == -1301) {
  123. _this.camerastate = false;
  124. }
  125. },
  126. //返回
  127. back() {
  128. uni.navigateBack();
  129. },
  130. //抓拍
  131. snapshot() {
  132. this.livePusher.snapshot({
  133. success: e => {
  134. _this.snapshotsrc = e.message.tempImagePath;
  135. _this.stopPreview();
  136. _this.setImage();
  137. uni.navigateBack();
  138. }
  139. });
  140. },
  141. //反转
  142. flip() {
  143. this.livePusher.switchCamera();
  144. },
  145. //设置
  146. setImage() {
  147. let pages = getCurrentPages();
  148. let prevPage = pages[pages.length - 2]; //上一个页面
  149. //直接调用上一个页面的setImage()方法,把数据存到上一个页面中去
  150. prevPage.$vm.setImage({ path: _this.snapshotsrc, dotype: this.dotype });
  151. }
  152. }
  153. };
  154. </script>
  155. <style lang="scss">
  156. .live-camera {
  157. .preview {
  158. justify-content: center;
  159. align-items: center;
  160. .outline-box {
  161. position: absolute;
  162. top: 0;
  163. left: 0;
  164. bottom: 0;
  165. z-index: 99;
  166. align-items: center;
  167. justify-content: center;
  168. .outline-img {
  169. width: 750rpx;
  170. height: 1125rpx;
  171. }
  172. }
  173. .remind {
  174. position: absolute;
  175. top: 880rpx;
  176. width: 750rpx;
  177. z-index: 100;
  178. align-items: center;
  179. justify-content: center;
  180. .remind-text {
  181. color: #dddddd;
  182. font-weight: bold;
  183. }
  184. }
  185. }
  186. .menu {
  187. position: absolute;
  188. left: 0;
  189. bottom: 0;
  190. width: 750rpx;
  191. height: 180rpx;
  192. z-index: 98;
  193. align-items: center;
  194. justify-content: center;
  195. .menu-mask {
  196. position: absolute;
  197. left: 0;
  198. bottom: 0;
  199. width: 750rpx;
  200. height: 180rpx;
  201. z-index: 98;
  202. }
  203. .menu-back {
  204. position: absolute;
  205. left: 30rpx;
  206. bottom: 50rpx;
  207. width: 80rpx;
  208. height: 80rpx;
  209. z-index: 99;
  210. align-items: center;
  211. justify-content: center;
  212. }
  213. .menu-snapshot {
  214. width: 130rpx;
  215. height: 130rpx;
  216. z-index: 99;
  217. }
  218. .menu-flip {
  219. position: absolute;
  220. right: 30rpx;
  221. bottom: 50rpx;
  222. width: 80rpx;
  223. height: 80rpx;
  224. z-index: 99;
  225. align-items: center;
  226. justify-content: center;
  227. }
  228. }
  229. }
  230. </style>