watermark.nvue 4.9 KB

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