my-camera.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <template>
  2. <view class="warp" v-if="appletStatus==1">
  3. <view class="center">
  4. <image class="content" v-if="src!=null" :src="src" ></image>
  5. <camera class="content" v-else :device-position="position" flash="auto" @error="error" >
  6. <cover-image :class="type==1?'coverImg-type1':'coverImg-type0'" :src="type==1?'../../static/camera/card.png':'../../static/camera/face.png'"></cover-image>
  7. </camera>
  8. </view>
  9. <view class="bottom">
  10. <!-- 取消拍摄 -->
  11. <image class="cancel" @click="cancel" src="../../static/camera/back.png" ></image>
  12. <!-- 确认选择照片 -->
  13. <image class="confirm" @click="confirm" v-if="src!=null" src="../../static/camera/confirm.png" ></image>
  14. <!-- 确认拍摄 -->
  15. <image class="confirm" v-else @click="takePhoto" src="../../static/camera/takephoto1.png" ></image>
  16. <!-- 切换摄像头 -->
  17. <image class="change" v-if="src==null" @click="change" src="../../static/camera/change.png" ></image>
  18. <!-- 重新拍摄 -->
  19. <image class="change" v-else @click="rephoto" src="../../static/camera/rephoto.png" ></image>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. export default {
  25. data() {
  26. return {
  27. //0 体验版 1正式版
  28. appletStatus:0,
  29. //0 人面 1 证件
  30. type:0,
  31. flash:'off',
  32. position:'front',
  33. src: null
  34. }
  35. },
  36. onLoad(options) {
  37. this.type=options.type || 0
  38. this.handelAppletAudit()
  39. },
  40. methods: {
  41. handelAppletAudit(){
  42. this.appletStatus=uni.getStorageSync("appletStatus")
  43. if (this.appletStatus==1) {
  44. uni.setNavigationBarTitle({
  45. title:' '
  46. })
  47. }else{
  48. uni.setNavigationBarTitle({
  49. title:'待开发'
  50. })
  51. }
  52. },
  53. //确认
  54. confirm(){
  55. /* 返回调用页面并把图片URL传递过去 */
  56. let that=this
  57. let pages = getCurrentPages();
  58. let prevPage = pages[pages.length - 2];
  59. prevPage.setData({
  60. "image":that.src
  61. })
  62. uni.navigateBack();
  63. },
  64. //闪光灯切换
  65. tapLight(){
  66. if (this.src==null) {
  67. this.flash=this.flash=='on'?'off':'on'
  68. }
  69. },
  70. //切换镜头
  71. change(){
  72. if (this.src==null) {
  73. this.position=this.position=='back'?'front':'back'
  74. }
  75. },
  76. //重新拍摄
  77. rephoto(){
  78. let that=this
  79. uni.showModal({
  80. title:"提示",
  81. content:"确定重新拍摄?",
  82. success: (res) => {
  83. if (res.confirm) {
  84. that.src=null
  85. }
  86. }
  87. })
  88. },
  89. //取消拍照
  90. cancel(){
  91. uni.showModal({
  92. title:"提示",
  93. content:"确定取消拍摄?",
  94. success: (res) => {
  95. if (res.confirm) {
  96. uni.navigateBack()
  97. }
  98. }
  99. })
  100. },
  101. //拍照
  102. takePhoto() {
  103. let that=this
  104. const ctx = uni.createCameraContext();
  105. ctx.takePhoto({
  106. quality: 'high',
  107. success: (res) => {
  108. that.src = res.tempImagePath
  109. },
  110. fail: (err) => {
  111. console.log(err)
  112. }
  113. });
  114. },
  115. //打印错误日志
  116. error(e) {
  117. console.log(e.detail);
  118. }
  119. }
  120. }
  121. </script>
  122. <style lang="scss">
  123. .warp{
  124. height: 100vh;
  125. background-color: #000;
  126. }
  127. .top{
  128. display: flex;
  129. justify-content: space-between;
  130. box-sizing: border-box;
  131. image{
  132. width: 50rpx;
  133. }
  134. //闪光灯
  135. .light{
  136. margin-left: 50rpx;
  137. padding: 10rpx 0;
  138. }
  139. .rephoto{
  140. margin-right: 30rpx;
  141. padding: 6rpx 0;
  142. }
  143. }
  144. .center{
  145. .content{
  146. height: 80vh;
  147. width: 100vw;
  148. .coverImg-type0{
  149. height: 80vh;
  150. opacity: 0.3;
  151. }
  152. .coverImg-type1{
  153. height: 80vh;
  154. opacity: 0.6;
  155. }
  156. }
  157. }
  158. // 底部功能按钮
  159. .bottom{
  160. height: 19vh;
  161. display: flex;
  162. justify-content:space-around;
  163. align-items: center;
  164. //切换镜头
  165. .change{
  166. height: 88rpx;
  167. width: 88rpx;
  168. margin-left: 40rpx;
  169. opacity: 0.7;
  170. }
  171. //确认拍摄
  172. .confirm{
  173. height: 150rpx;
  174. width: 150rpx
  175. }
  176. //取消上传
  177. .cancel{
  178. height: 88rpx;
  179. width: 88rpx;
  180. margin-right: 40rpx;
  181. opacity: .7;
  182. }
  183. }
  184. //重新拍摄按钮
  185. .button {
  186. font-size: 20rpx;
  187. background-color: #59a5f0; /* Green */
  188. border: none;
  189. color: white;
  190. padding: 2rpx 32rpx;
  191. text-align: center;
  192. text-decoration: none;
  193. display: inline-block;
  194. cursor: pointer;
  195. }
  196. </style>