WaterMarkCamera.nvue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <template>
  2. <view class="custom-watermark-camera" :style="{ width: windowWidth, height: windowHeight }">
  3. <live-pusher id="livePusher" ref="livePusher" class="livePusher" mode="FHD" beauty="0" whiteness="0"
  4. :aspect="aspect" min-bitrate="1000" audio-quality="16KHz" device-position="back" :auto-focus="true"
  5. :muted="true" :enable-camera="true" :enable-mic="false" :zoom="false" @statechange="statechange"
  6. :style="{ width: windowWidth, height: windowHeight }"></live-pusher>
  7. <cover-view class="remark">
  8. <cover-view class="mark-text name">{{ username }}</cover-view>
  9. <cover-view class="mark-text time">{{ time }}</cover-view>
  10. <cover-view class="mark-text address">{{ address }}</cover-view>
  11. <cover-view class="mark-text lnglat">{{ lnglat }}</cover-view>
  12. </cover-view>
  13. <view class="menu">
  14. <!--底部菜单区域背景-->
  15. <cover-image class="menu-mask" src="/static/images/components/camera/bg.png"></cover-image>
  16. <!--返回键-->
  17. <cover-image class="menu-back" @tap="back" src="/static/images/components/camera/back.png"></cover-image>
  18. <!--快门键-->
  19. <cover-image class="menu-snapshot" @tap="snapshot"
  20. src="/static/images/components/camera/circle.png"></cover-image>
  21. <!--反转键-->
  22. <cover-image class="menu-flip" @tap="flip" src="/static/images/components/camera/reverse.png"></cover-image>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. let _this = null;
  28. import moment from 'moment';
  29. export default {
  30. data() {
  31. return {
  32. poenCarmeInterval: null, //打开相机的轮询
  33. aspect: '2:3', //比例
  34. windowWidth: '', //屏幕可用宽度
  35. windowHeight: '', //屏幕可用高度
  36. camerastate: false, //相机准备好了
  37. livePusher: null, //流视频对象
  38. snapshotsrc: null, //快照
  39. eventChannel: null, //通信
  40. timer: null,
  41. username: "用户名",
  42. time: "",
  43. address: "",
  44. lnglat: ""
  45. };
  46. },
  47. onLoad(e) {
  48. _this = this;
  49. this.initCamera();
  50. this.eventChannel = this.getOpenerEventChannel();
  51. },
  52. onReady() {
  53. this.livePusher = uni.createLivePusherContext('livePusher', this);
  54. this.startPreview(); //开启预览并设置摄像头
  55. this.poenCarme();
  56. },
  57. onShow() {
  58. this.getAddress()
  59. this.time = moment().format('YYYY-MM-DD HH:mm:ss')
  60. clearInterval(this.timer)
  61. this.timer = setInterval(() => {
  62. this.getAddress()
  63. this.time = moment().format('YYYY-MM-DD HH:mm:ss')
  64. }, 5000)
  65. },
  66. methods: {
  67. getAddress() {
  68. console.log('获取位置')
  69. uni.getLocation({
  70. type: 'gcj02',
  71. geocode: true,
  72. isHighAccuracy: true,
  73. success: (res) => {
  74. console.log(res)
  75. this.address = res.address.province + res.address.city + res.address.district + res
  76. .address.street + res.address.streetNum + res.address.poiName
  77. this.lnglat = res.longitude + ', ' + res.latitude
  78. }
  79. })
  80. },
  81. //轮询打开
  82. poenCarme() {
  83. //#ifdef APP-PLUS
  84. if (plus.os.name == 'Android') {
  85. this.poenCarmeInterval = setInterval(function() {
  86. console.log(_this.camerastate);
  87. if (!_this.camerastate) _this.startPreview();
  88. }, 2500);
  89. }
  90. //#endif
  91. },
  92. //初始化相机
  93. initCamera() {
  94. uni.getSystemInfo({
  95. success: function(res) {
  96. _this.windowWidth = res.windowWidth;
  97. _this.windowHeight = res.windowHeight;
  98. let zcs = _this.aliquot(_this.windowWidth, _this.windowHeight);
  99. _this.aspect = (_this.windowWidth / zcs) + ':' + (_this.windowHeight / zcs);
  100. // console.log('画面比例:'+_this.aspect);
  101. }
  102. });
  103. },
  104. //整除数计算
  105. aliquot(x, y) {
  106. if (x % y == 0) return y;
  107. return this.aliquot(y, x % y);
  108. },
  109. //开始预览
  110. startPreview() {
  111. this.livePusher.startPreview({
  112. success: a => {
  113. console.log(a)
  114. }
  115. });
  116. },
  117. //停止预览
  118. stopPreview() {
  119. this.livePusher.stopPreview({
  120. success: a => {
  121. _this.camerastate = false;
  122. }
  123. });
  124. },
  125. //状态
  126. statechange(e) {
  127. //状态改变
  128. console.log(e);
  129. if (e.detail.code == 1007) {
  130. _this.camerastate = true;
  131. } else if (e.detail.code == -1301) {
  132. _this.camerastate = false;
  133. }
  134. },
  135. //返回
  136. back() {
  137. uni.navigateBack();
  138. },
  139. //抓拍
  140. snapshot() {
  141. //震动
  142. uni.vibrateShort({
  143. success: function() {
  144. console.log('success');
  145. }
  146. });
  147. //拍照
  148. this.livePusher.snapshot({
  149. success: e => {
  150. _this.snapshotsrc = e.message.tempImagePath;
  151. _this.stopPreview();
  152. // _this.setImage();
  153. this.eventChannel.emit('setImage', {
  154. path: e.message.tempImagePath,
  155. text: {
  156. username: this.username,
  157. time: this.time,
  158. address: this.address,
  159. lnglat: this.lnglat
  160. }
  161. })
  162. uni.navigateBack();
  163. }
  164. });
  165. },
  166. //反转
  167. flip() {
  168. this.livePusher.switchCamera();
  169. },
  170. //设置
  171. setImage() {
  172. let pages = getCurrentPages();
  173. let prevPage = pages[pages.length - 2];
  174. console.log(prevPage.$vm)
  175. prevPage.$vm.setImage({
  176. path: _this.snapshotsrc
  177. });
  178. }
  179. }
  180. };
  181. </script>
  182. <style lang="scss">
  183. .custom-watermark-camera {
  184. justify-content: center;
  185. align-items: center;
  186. .remark {
  187. position: absolute;
  188. left: 0;
  189. top: 100rpx;
  190. padding: 100rpx;
  191. color: #fff;
  192. width: 750rpx;
  193. .mark-text {
  194. font-size: 40rpx;
  195. color: #fff;
  196. }
  197. .address {
  198. font-size: 28rpx;
  199. }
  200. }
  201. .menu {
  202. position: absolute;
  203. left: 0;
  204. bottom: 0;
  205. width: 750rpx;
  206. height: 180rpx;
  207. z-index: 98;
  208. align-items: center;
  209. justify-content: center;
  210. .menu-mask {
  211. position: absolute;
  212. left: 0;
  213. bottom: 0;
  214. width: 750rpx;
  215. height: 180rpx;
  216. z-index: 98;
  217. }
  218. .menu-back {
  219. position: absolute;
  220. left: 30rpx;
  221. bottom: 50rpx;
  222. width: 80rpx;
  223. height: 80rpx;
  224. z-index: 99;
  225. align-items: center;
  226. justify-content: center;
  227. }
  228. .menu-snapshot {
  229. width: 130rpx;
  230. height: 130rpx;
  231. z-index: 99;
  232. }
  233. .menu-flip {
  234. position: absolute;
  235. right: 30rpx;
  236. bottom: 50rpx;
  237. width: 80rpx;
  238. height: 80rpx;
  239. z-index: 99;
  240. align-items: center;
  241. justify-content: center;
  242. }
  243. }
  244. }
  245. </style>