百富门答题小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
3.5 KiB

8 months ago
  1. <template>
  2. <view>
  3. <block v-if="authCamera">
  4. <camera :device-position="devPosition" @initdone="init" flash="off" style="width: 100%;" :style="{height:viewH-btmH+'px'}"></camera>
  5. </block>
  6. <block v-else>
  7. <view class="defaultBgm" :style="{height:viewH-btmH+'px'}"></view>
  8. </block>
  9. <!-- 摄像头区域 -->
  10. <!-- 底部区域 -->
  11. <view class="view_cont_btm flex_row">
  12. <view class="cont_box" @click="openAlbum">相册</view>
  13. <view class="photobtn">
  14. <button @click="takePhoto"></button>
  15. </view>
  16. <view class="cont_box" @click="switchPosition">切换</view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. data() {
  23. return {
  24. devPosition: 'back', //打开的摄像头位置
  25. viewH: 0, //页面整体高度
  26. btmH: 0, //底部高度
  27. authCamera: false, //检测摄像头是否授权,如果没有则不显示camera
  28. }
  29. },
  30. onLoad(option) {
  31. },
  32. onShow() {
  33. var that = this;
  34. // 检测用户是否授权打开摄像头
  35. uni.getSetting({
  36. success(e) {
  37. console.log(e)
  38. if(!e.authSetting['scope.camera']){
  39. uni.showModal({
  40. title: '提示',
  41. content: '需要您授权打开摄像头',
  42. success(e) {
  43. if (e.confirm) {
  44. uni.openSetting({
  45. success(res) {
  46. console.log(res)
  47. if(res.authSetting['scope.camera']){
  48. that.authCamera = true
  49. }else{
  50. uni.showToast({
  51. title: '未授权',
  52. icon:'none'
  53. })
  54. }
  55. }
  56. })
  57. }else{
  58. uni.showToast({
  59. title: '未授权',
  60. icon:'none'
  61. })
  62. }
  63. }
  64. })
  65. }else{
  66. that.authCamera = true
  67. }
  68. }
  69. })
  70. },
  71. created() {
  72. var that = this;
  73. var h = uni.getSystemInfoSync().windowHeight;
  74. this.viewH = h;
  75. let info = uni.createSelectorQuery().select(".view_cont_btm");
  76.     info.boundingClientRect(function(data) { //data - 各种参数
  77. that.btmH = data.height
  78.    }).exec()
  79. },
  80. methods: {
  81. // 点击相册
  82. openAlbum() {
  83. uni.chooseImage({
  84. count: 1,
  85. sourceType: ['album'],
  86. success(res) {
  87. console.log(JSON.stringify(res.tempFilePaths));
  88. uni.previewImage({
  89. urls: res.tempFilePaths,
  90. })
  91. }
  92. })
  93. },
  94. // 点击切换摄像头
  95. switchPosition() {
  96. if(this.devPosition == 'back'){
  97. this.devPosition = 'front'
  98. }else{
  99. this.devPosition = 'back'
  100. }
  101. },
  102. // 点击拍照
  103. takePhoto() {
  104. uni.showLoading({
  105. title: '识别中..'
  106. })
  107. const ctx = uni.createCameraContext();
  108. ctx.takePhoto({
  109. quality: 'high',
  110. success: (res) => {
  111. var imgs = [res.tempImagePath];
  112. uni.previewImage({
  113. urls: imgs,
  114. success() {
  115. uni.hideLoading();
  116. }
  117. })
  118. this.src = res.tempImagePath
  119. }
  120. });
  121. },
  122. // 相机初始化完成时触发
  123. init(e){
  124. console.log('初始化')
  125. }
  126. }
  127. }
  128. </script>
  129. <style>
  130. .defaultBgm{
  131. background: #4AA352;
  132. }
  133. .view_cont_btm{
  134. padding: 120rpx 60rpx;
  135. display: flex;
  136. justify-content: space-around;
  137. align-items: center;
  138. }
  139. .cont_box{
  140. border: 1px solid #4AA352;
  141. color: #4AA352;
  142. padding: 20rpx;
  143. font-size: 28rpx;
  144. }
  145. .photobtn{
  146. width: 160rpx;
  147. height: 160rpx;
  148. border-radius: 50%;
  149. border: 1px solid #4AA352;
  150. padding: 10rpx;
  151. box-sizing: border-box;
  152. text-align: center;
  153. }
  154. .photobtn button{
  155. width: 100%;
  156. height: 100%;
  157. margin: 0;
  158. background: linear-gradient(to right, #5eab52, #9ac454);
  159. border-radius: 50%;
  160. }
  161. </style>