index.nvue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <view>
  3. <live-pusher id="livePusher" ref="livePusher" class="livePusher" mode="FHD" beauty="0" whiteness="0"
  4. device-position="back" :auto-focus="true" :muted="true" :enable-camera="true" :enable-mic="true"
  5. :zoom="true" :style="[{height: cameraHeight ,width:'750rpx'}]">
  6. </live-pusher>
  7. <cover-image v-if="coverImage" class="cover-image" :style="[{height:cameraHeight+'px',width:'750rpx'}]"
  8. :src="coverImage" />
  9. <view class="camera-options" :style="[{height:optionsHeight+'px'}]">
  10. <view class="camera-options-left camera-item">
  11. <image class="camera-item-image" src="/static/images/back.png" mode="scaleToFill"
  12. @click="handleInstruct('back')"></image>
  13. </view>
  14. <view class="camera-options-center camera-item">
  15. <image class="camera-item-image" src="/static/images/shutter.png" mode="scaleToFill"
  16. @click="handleInstruct('shutter')"></image>
  17. </view>
  18. <!-- <view class="camera-options-ritht camera-item" @click="handleInstruct('reversal')">
  19. <image class="camera-item-image" src="/static/images/reversal.png" mode="scaleToFill"></image>
  20. </view> -->
  21. <view class="camera-options-ritht camera-item">
  22. <image class="camera-item-image" src="/static/images/album.png" mode="scaleToFill"
  23. @click="handleInstruct('album')"></image>
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. // 这个组件仅限APP使用!!!
  30. import config from '../config.js'
  31. export default {
  32. props: {
  33. coverImageType: { //遮罩层的类型 (必须在config里面的定义的)
  34. type: String,
  35. default: 'portrait'
  36. }
  37. },
  38. data() {
  39. return {
  40. livePusher: null,
  41. ready: true,
  42. cameraHeight: '', //相机画面宽度
  43. optionsHeight: '', //操作台高度
  44. coverImage: null,
  45. }
  46. },
  47. mounted() {
  48. this.cameraHeight = uni.getSystemInfoSync().screenHeight * 0.82
  49. this.optionsHeight = uni.getSystemInfoSync().screenHeight * 0.18
  50. this.init()
  51. try {
  52. this.coverImage = config[this.coverImageType]
  53. } catch (e) {
  54. uni.showToast({
  55. title: '传入的coverImageType不存在',
  56. icon: 'none'
  57. })
  58. }
  59. },
  60. methods: {
  61. init() {
  62. this.livePusher = uni.createLivePusherContext('livePusher', this);
  63. setTimeout(() => {
  64. this.startPreview()
  65. }, 1000)
  66. },
  67. startPreview() {
  68. this.livePusher.startPreview({
  69. success: () => {
  70. console.log('相机初始化成功');
  71. switch (plus.os.name) {
  72. case 'Android':
  73. break;
  74. case 'iOS':
  75. this.livePusher.switchCamera()
  76. break
  77. }
  78. },
  79. fail: (err) => {
  80. console.log(err)
  81. }
  82. });
  83. },
  84. handleInstruct(instruct) {
  85. switch (instruct) {
  86. // 返回
  87. case 'back':
  88. this.$emit('back')
  89. break;
  90. // 快门
  91. case 'shutter':
  92. if (this.ready) {
  93. this.ready = false
  94. this.livePusher.snapshot({
  95. success: (res) => {
  96. console.log(res)
  97. this.ready = true
  98. this.$emit('getImage', res.message.tempImagePath)
  99. }
  100. })
  101. }
  102. break;
  103. // 反转
  104. case 'reversal':
  105. this.livePusher.switchCamera();
  106. break;
  107. // 相册
  108. case 'album':
  109. uni.chooseImage({
  110. count: 1, //默认9
  111. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  112. sourceType: ['album'], //从相册选择
  113. success: (res) => {
  114. this.$emit('getImage', res.tempFilePaths[0])
  115. }
  116. })
  117. break;
  118. }
  119. }
  120. }
  121. }
  122. </script>
  123. <style scoped>
  124. .camera-background {
  125. width: 100%;
  126. height: 100%;
  127. background-color: rgba(0, 0, 0, 0.3);
  128. }
  129. .cover-image {
  130. position: fixed;
  131. top: 0;
  132. left: 0;
  133. z-index: 99;
  134. }
  135. .camera-options {
  136. flex-direction: row;
  137. align-items: center;
  138. }
  139. .camera-item {
  140. flex: 1;
  141. flex-direction: row;
  142. justify-content: center;
  143. align-items: center;
  144. height: 100%;
  145. }
  146. .camera-item .camera-item-image {
  147. width: 80rpx;
  148. height: 80rpx;
  149. }
  150. .camera-options-center .camera-item-image {
  151. width: 120rpx;
  152. height: 120rpx;
  153. }
  154. </style>