portrait.nvue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <view class="live-camera" :style="{ width: windowWidth, height: windowHeight }">
  3. <live-pusher
  4. id="livePusher"
  5. ref="livePusher"
  6. class="livePusher"
  7. mode="FHD"
  8. beauty="0"
  9. whiteness="0"
  10. :aspect="aspect"
  11. min-bitrate="1000"
  12. audio-quality="16KHz"
  13. device-position="back"
  14. :auto-focus="true"
  15. :muted="true"
  16. :enable-camera="true"
  17. :enable-mic="false"
  18. :zoom="false"
  19. :style="{ width: windowWidth, height: windowHeight }"
  20. ></live-pusher>
  21. <!--提示语-->
  22. <cover-view class="remind">
  23. <text class="remind-text" style="">{{ message }}</text>
  24. </cover-view>
  25. <!--辅助线-->
  26. <cover-view class="outline">
  27. <cover-image class="outline" src="/static/live-camera/outline/portrait.png" style=""></cover-image>
  28. </cover-view>
  29. <view class="menu">
  30. <!--底部菜单区域背景-->
  31. <cover-image class="menu-mask" src="/static/live-camera/bar.png"></cover-image>
  32. <!--返回键-->
  33. <cover-image class="menu-back" @tap="back" src="/static/live-camera/back.png"></cover-image>
  34. <!--快门键-->
  35. <cover-image class="menu-snapshot" @tap="snapshot" src="/static/live-camera/shutter.png"></cover-image>
  36. <!--反转键-->
  37. <cover-image class="menu-flip" @tap="flip" src="/static/live-camera/flip.png"></cover-image>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. let _this = null;
  43. export default {
  44. data() {
  45. return {
  46. message:'请拍摄苛学加',//提示
  47. aspect: '2:3', //比例
  48. windowWidth: '', //屏幕可用宽度
  49. windowHeight: '', //屏幕可用高度
  50. camerastate: false, //相机准备好了
  51. livePusher: null, //流视频对象
  52. snapshotsrc: null //快照
  53. };
  54. },
  55. onLoad(e) {
  56. _this = this;
  57. this.initCamera();
  58. },
  59. onReady() {
  60. this.livePusher = uni.createLivePusherContext('livePusher', this);
  61. this.startPreview(); //开启预览并设置摄像头
  62. },
  63. methods: {
  64. //初始化相机
  65. initCamera() {
  66. uni.getSystemInfo({
  67. success: function(res) {
  68. _this.windowWidth = res.windowWidth;
  69. _this.windowHeight = res.windowHeight;
  70. let zcs = _this.aliquot(_this.windowWidth, _this.windowHeight);
  71. _this.aspect = _this.windowWidth / zcs + ':' + _this.windowHeight / zcs;
  72. }
  73. });
  74. },
  75. //整除数计算
  76. aliquot(x, y) {
  77. if (x % y == 0) return y;
  78. return this.aliquot(y, x % y);
  79. },
  80. //开始预览
  81. startPreview() {
  82. this.livePusher.startPreview({
  83. success: a => {
  84. if (a.errMsg == 'startPreview:ok' || a.errMsg == 'operateLivePusher:ok') {
  85. _this.camerastate = true; //标记相机启动成功
  86. }
  87. }
  88. });
  89. },
  90. //停止预览
  91. stopPreview() {
  92. this.livePusher.stopPreview({
  93. success: a => {
  94. _this.camerastate = false; //标记相机未启动
  95. }
  96. });
  97. },
  98. //返回
  99. back() {
  100. uni.navigateBack();
  101. },
  102. //抓拍
  103. snapshot() {
  104. this.livePusher.snapshot({
  105. success: e => {
  106. _this.snapshotsrc = e.message.tempImagePath;
  107. _this.stopPreview();
  108. _this.setImage();
  109. uni.navigateBack();
  110. }
  111. });
  112. },
  113. //反转
  114. flip() {
  115. this.livePusher.switchCamera();
  116. },
  117. //设置
  118. setImage() {
  119. let pages = getCurrentPages();
  120. let prevPage = pages[pages.length - 2]; //上一个页面
  121. //直接调用上一个页面的setImage()方法,把数据存到上一个页面中去
  122. prevPage.$vm.setImage({ path: _this.snapshotsrc });
  123. }
  124. }
  125. };
  126. </script>
  127. <style lang="scss">
  128. .live-camera {
  129. justify-content: center;
  130. align-items: center;
  131. .outline {
  132. position: absolute;
  133. top: 0;
  134. left: 0;
  135. bottom: 0;
  136. width: 750rpx;
  137. height: 1125rpx;
  138. z-index: 99;
  139. align-items: center;
  140. justify-content: center;
  141. }
  142. .remind {
  143. position: absolute;
  144. top: 880rpx;
  145. width: 750rpx;
  146. z-index: 100;
  147. align-items: center;
  148. justify-content: center;
  149. .remind-text {
  150. color: #dddddd;
  151. font-weight: bold;
  152. }
  153. }
  154. .menu {
  155. position: absolute;
  156. left: 0;
  157. bottom: 0;
  158. width: 750rpx;
  159. height: 180rpx;
  160. z-index: 98;
  161. align-items: center;
  162. justify-content: center;
  163. .menu-mask {
  164. position: absolute;
  165. left: 0;
  166. bottom: 0;
  167. width: 750rpx;
  168. height: 180rpx;
  169. z-index: 98;
  170. }
  171. .menu-back {
  172. position: absolute;
  173. left: 30rpx;
  174. bottom: 50rpx;
  175. width: 80rpx;
  176. height: 80rpx;
  177. z-index: 99;
  178. align-items: center;
  179. justify-content: center;
  180. }
  181. .menu-snapshot {
  182. width: 130rpx;
  183. height: 130rpx;
  184. z-index: 99;
  185. }
  186. .menu-flip {
  187. position: absolute;
  188. right: 30rpx;
  189. bottom: 50rpx;
  190. width: 80rpx;
  191. height: 80rpx;
  192. z-index: 99;
  193. align-items: center;
  194. justify-content: center;
  195. }
  196. }
  197. }
  198. </style>