idphoto.nvue 5.1 KB

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