my-camera.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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':'http://139.9.103.171:1888/miniofile/xlyq/face2.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/confirm1.png" ></image>
  14. <!-- 确认拍摄 -->
  15. <image class="confirm" v-else @click="takePhoto" src="../../static/camera/takephoto.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. console.log(this.src)
  56. /* 返回调用页面并把图片URL传递过去 */
  57. let that=this
  58. let pages = getCurrentPages();
  59. let prevPage = pages[pages.length - 2];
  60. prevPage.setData({
  61. "image":that.src
  62. })
  63. uni.navigateBack();
  64. },
  65. //闪光灯切换
  66. tapLight(){
  67. if (this.src==null) {
  68. this.flash=this.flash=='on'?'off':'on'
  69. }
  70. },
  71. //切换镜头
  72. change(){
  73. if (this.src==null) {
  74. this.position=this.position=='back'?'front':'back'
  75. }
  76. },
  77. //重新拍摄
  78. rephoto(){
  79. let that=this
  80. uni.showModal({
  81. title:"提示",
  82. content:"确定重新拍摄?",
  83. success: (res) => {
  84. if (res.confirm) {
  85. that.src=null
  86. }
  87. }
  88. })
  89. },
  90. //取消拍照
  91. cancel(){
  92. uni.showModal({
  93. title:"提示",
  94. content:"确定取消拍摄?",
  95. success: (res) => {
  96. if (res.confirm) {
  97. uni.navigateBack()
  98. }
  99. }
  100. })
  101. },
  102. //拍照
  103. takePhoto() {
  104. let that=this
  105. const ctx = uni.createCameraContext();
  106. ctx.takePhoto({
  107. quality: 'high',
  108. success: (res) => {
  109. that.src = res.tempImagePath
  110. },
  111. fail: (err) => {
  112. console.log(err)
  113. }
  114. });
  115. },
  116. //打印错误日志
  117. error(e) {
  118. console.log(e.detail);
  119. }
  120. }
  121. }
  122. </script>
  123. <style lang="scss">
  124. .warp{
  125. height: 100vh;
  126. background-color: #fff;
  127. }
  128. .top{
  129. display: flex;
  130. justify-content: space-between;
  131. box-sizing: border-box;
  132. image{
  133. width: 50rpx;
  134. }
  135. //闪光灯
  136. .light{
  137. margin-left: 50rpx;
  138. padding: 10rpx 0;
  139. }
  140. .rephoto{
  141. margin-right: 30rpx;
  142. padding: 6rpx 0;
  143. }
  144. }
  145. .center{
  146. .content{
  147. height: 80vh;
  148. width: 100vw;
  149. .coverImg-type0{
  150. height: 75vh;
  151. margin-top: -20rpx;
  152. margin-bottom: 20rpx;
  153. opacity: 0.3;
  154. }
  155. .coverImg-type1{
  156. height: 80vh;
  157. opacity: 0.6;
  158. }
  159. }
  160. }
  161. // 底部功能按钮
  162. .bottom{
  163. height: 19vh;
  164. display: flex;
  165. justify-content:space-around;
  166. align-items: center;
  167. //切换镜头
  168. .change{
  169. height: 88rpx;
  170. width: 88rpx;
  171. margin-left: 40rpx;
  172. opacity: 0.7;
  173. }
  174. //确认拍摄
  175. .confirm{
  176. height: 150rpx;
  177. width: 150rpx
  178. }
  179. //取消上传
  180. .cancel{
  181. height: 88rpx;
  182. width: 88rpx;
  183. margin-right: 40rpx;
  184. opacity: .7;
  185. }
  186. }
  187. //重新拍摄按钮
  188. .button {
  189. font-size: 20rpx;
  190. background-color: #59a5f0; /* Green */
  191. border: none;
  192. color: white;
  193. padding: 2rpx 32rpx;
  194. text-align: center;
  195. text-decoration: none;
  196. display: inline-block;
  197. cursor: pointer;
  198. }
  199. </style>