camera.nvue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. @statechange="statechange"
  20. :style="{ width: windowWidth, height: windowHeight }"
  21. ></live-pusher>
  22. <view class="menu">
  23. <!--底部菜单区域背景-->
  24. <cover-image class="menu-mask" src="/static/live-camera/bar.png"></cover-image>
  25. <!--返回键-->
  26. <cover-image class="menu-back" @tap="back" src="/static/live-camera/back.png"></cover-image>
  27. <!--快门键-->
  28. <cover-image class="menu-snapshot" @tap="snapshot" src="/static/live-camera/shutter.png"></cover-image>
  29. <!--反转键-->
  30. <cover-image class="menu-flip" @tap="flip" src="/static/live-camera/flip.png"></cover-image>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. let _this = null;
  36. export default {
  37. data() {
  38. return {
  39. poenCarmeInterval:null,//打开相机的轮询
  40. aspect: '2:3', //比例
  41. windowWidth: '', //屏幕可用宽度
  42. windowHeight: '', //屏幕可用高度
  43. camerastate: false, //相机准备好了
  44. livePusher: null, //流视频对象
  45. snapshotsrc: null //快照
  46. };
  47. },
  48. onLoad(e) {
  49. _this = this;
  50. this.initCamera();
  51. },
  52. onReady() {
  53. this.livePusher = uni.createLivePusherContext('livePusher', this);
  54. this.startPreview(); //开启预览并设置摄像头
  55. this.poenCarme();
  56. },
  57. methods: {
  58. //轮询打开
  59. poenCarme(){
  60. //#ifdef APP-PLUS
  61. if (plus.os.name == 'Android') {
  62. this.poenCarmeInterval = setInterval(function() {
  63. console.log(_this.camerastate);
  64. if (!_this.camerastate) _this.startPreview();
  65. }, 2500);
  66. }
  67. //#endif
  68. },
  69. //初始化相机
  70. initCamera() {
  71. uni.getSystemInfo({
  72. success: function(res) {
  73. _this.windowWidth = res.windowWidth;
  74. _this.windowHeight = res.windowHeight;
  75. let zcs = _this.aliquot(_this.windowWidth,_this.windowHeight);
  76. _this.aspect = (_this.windowWidth/zcs)+':'+(_this.windowHeight/zcs);
  77. console.log('画面比例:'+_this.aspect);
  78. }
  79. });
  80. },
  81. //整除数计算
  82. aliquot(x, y) {
  83. if (x % y == 0) return y;
  84. return this.aliquot(y, x % y);
  85. },
  86. //开始预览
  87. startPreview() {
  88. this.livePusher.startPreview({
  89. success: a => {
  90. console.log(a)
  91. }
  92. });
  93. },
  94. //停止预览
  95. stopPreview() {
  96. this.livePusher.stopPreview({
  97. success: a => {
  98. _this.camerastate = false; //标记相机未启动
  99. }
  100. });
  101. },
  102. //状态
  103. statechange(e) {
  104. //状态改变
  105. console.log(e);
  106. if (e.detail.code == 1007) {
  107. _this.camerastate = true;
  108. } else if (e.detail.code == -1301) {
  109. _this.camerastate = false;
  110. }
  111. },
  112. //返回
  113. back() {
  114. uni.navigateBack();
  115. },
  116. //抓拍
  117. snapshot() {
  118. //震动
  119. uni.vibrateShort({
  120. success: function () {
  121. console.log('success');
  122. }
  123. });
  124. //拍照
  125. this.livePusher.snapshot({
  126. success: e => {
  127. _this.snapshotsrc = e.message.tempImagePath;
  128. _this.stopPreview();
  129. _this.setImage();
  130. uni.navigateBack();
  131. }
  132. });
  133. },
  134. //反转
  135. flip() {
  136. this.livePusher.switchCamera();
  137. },
  138. //设置
  139. setImage() {
  140. let pages = getCurrentPages();
  141. let prevPage = pages[pages.length - 2]; //上一个页面
  142. //直接调用上一个页面的setImage()方法,把数据存到上一个页面中去
  143. prevPage.$vm.setImage({ path: _this.snapshotsrc });
  144. }
  145. }
  146. };
  147. </script>
  148. <style lang="scss">
  149. .live-camera {
  150. justify-content: center;
  151. align-items: center;
  152. .menu {
  153. position: absolute;
  154. left: 0;
  155. bottom: 0;
  156. width: 750rpx;
  157. height: 180rpx;
  158. z-index: 98;
  159. align-items: center;
  160. justify-content: center;
  161. .menu-mask {
  162. position: absolute;
  163. left: 0;
  164. bottom: 0;
  165. width: 750rpx;
  166. height: 180rpx;
  167. z-index: 98;
  168. }
  169. .menu-back {
  170. position: absolute;
  171. left: 30rpx;
  172. bottom: 50rpx;
  173. width: 80rpx;
  174. height: 80rpx;
  175. z-index: 99;
  176. align-items: center;
  177. justify-content: center;
  178. }
  179. .menu-snapshot {
  180. width: 130rpx;
  181. height: 130rpx;
  182. z-index: 99;
  183. }
  184. .menu-flip {
  185. position: absolute;
  186. right: 30rpx;
  187. bottom: 50rpx;
  188. width: 80rpx;
  189. height: 80rpx;
  190. z-index: 99;
  191. align-items: center;
  192. justify-content: center;
  193. }
  194. }
  195. }
  196. </style>