WaterMarkCamera.nvue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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/黑色背景.png"></cover-image>
  16. <!--返回键-->
  17. <cover-image class="menu-back" @tap="back" src="/static/images/components/camera/返回.png"></cover-image>
  18. <!--快门键-->
  19. <cover-image class="menu-snapshot" @tap="snapshot"
  20. src="/static/images/components/camera/圆圈.png"></cover-image>
  21. <!--反转键-->
  22. <cover-image class="menu-flip" @tap="flip" src="/static/images/components/camera/反转.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. uni.getLocation({
  69. type: 'gcj02',
  70. geocode: true,
  71. isHighAccuracy: true,
  72. success: (res) => {
  73. this.address = res.address.province + res.address.city + res.address.district + res
  74. .address.street + res.address.streetNum + res.address.poiName
  75. this.lnglat = res.longitude + ', ' + res.latitude
  76. }
  77. })
  78. },
  79. //轮询打开
  80. poenCarme() {
  81. //#ifdef APP-PLUS
  82. if (plus.os.name == 'Android') {
  83. this.poenCarmeInterval = setInterval(function() {
  84. console.log(_this.camerastate);
  85. if (!_this.camerastate) _this.startPreview();
  86. }, 2500);
  87. }
  88. //#endif
  89. },
  90. //初始化相机
  91. initCamera() {
  92. uni.getSystemInfo({
  93. success: function(res) {
  94. _this.windowWidth = res.windowWidth;
  95. _this.windowHeight = res.windowHeight;
  96. let zcs = _this.aliquot(_this.windowWidth, _this.windowHeight);
  97. _this.aspect = (_this.windowWidth / zcs) + ':' + (_this.windowHeight / zcs);
  98. // console.log('画面比例:'+_this.aspect);
  99. }
  100. });
  101. },
  102. //整除数计算
  103. aliquot(x, y) {
  104. if (x % y == 0) return y;
  105. return this.aliquot(y, x % y);
  106. },
  107. //开始预览
  108. startPreview() {
  109. this.livePusher.startPreview({
  110. success: a => {
  111. console.log(a)
  112. }
  113. });
  114. },
  115. //停止预览
  116. stopPreview() {
  117. this.livePusher.stopPreview({
  118. success: a => {
  119. _this.camerastate = false;
  120. }
  121. });
  122. },
  123. //状态
  124. statechange(e) {
  125. //状态改变
  126. console.log(e);
  127. if (e.detail.code == 1007) {
  128. _this.camerastate = true;
  129. } else if (e.detail.code == -1301) {
  130. _this.camerastate = false;
  131. }
  132. },
  133. //返回
  134. back() {
  135. uni.navigateBack();
  136. },
  137. //抓拍
  138. snapshot() {
  139. //震动
  140. uni.vibrateShort({
  141. success: function() {
  142. console.log('success');
  143. }
  144. });
  145. //拍照
  146. this.livePusher.snapshot({
  147. success: e => {
  148. _this.snapshotsrc = e.message.tempImagePath;
  149. _this.stopPreview();
  150. // _this.setImage();
  151. this.eventChannel.emit('setImage', {
  152. path: e.message.tempImagePath,
  153. text: {
  154. username: this.username,
  155. time: this.time,
  156. address: this.address,
  157. lnglat: this.lnglat
  158. }
  159. })
  160. uni.navigateBack();
  161. }
  162. });
  163. },
  164. //反转
  165. flip() {
  166. this.livePusher.switchCamera();
  167. },
  168. //设置
  169. setImage() {
  170. let pages = getCurrentPages();
  171. let prevPage = pages[pages.length - 2];
  172. console.log(prevPage.$vm)
  173. prevPage.$vm.setImage({
  174. path: _this.snapshotsrc
  175. });
  176. }
  177. }
  178. };
  179. </script>
  180. <style lang="scss">
  181. .custom-watermark-camera {
  182. justify-content: center;
  183. align-items: center;
  184. .remark {
  185. position: absolute;
  186. left: 0;
  187. top: 100rpx;
  188. padding: 100rpx;
  189. color: #fff;
  190. width: 750rpx;
  191. .mark-text {
  192. font-size: 40rpx;
  193. color: #fff;
  194. }
  195. .address {
  196. font-size: 28rpx;
  197. }
  198. }
  199. .menu {
  200. position: absolute;
  201. left: 0;
  202. bottom: 0;
  203. width: 750rpx;
  204. height: 180rpx;
  205. z-index: 98;
  206. align-items: center;
  207. justify-content: center;
  208. .menu-mask {
  209. position: absolute;
  210. left: 0;
  211. bottom: 0;
  212. width: 750rpx;
  213. height: 180rpx;
  214. z-index: 98;
  215. }
  216. .menu-back {
  217. position: absolute;
  218. left: 30rpx;
  219. bottom: 50rpx;
  220. width: 80rpx;
  221. height: 80rpx;
  222. z-index: 99;
  223. align-items: center;
  224. justify-content: center;
  225. }
  226. .menu-snapshot {
  227. width: 130rpx;
  228. height: 130rpx;
  229. z-index: 99;
  230. }
  231. .menu-flip {
  232. position: absolute;
  233. right: 30rpx;
  234. bottom: 50rpx;
  235. width: 80rpx;
  236. height: 80rpx;
  237. z-index: 99;
  238. align-items: center;
  239. justify-content: center;
  240. }
  241. }
  242. }
  243. </style>