video.nvue 5.9 KB

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