my-camera.vue 3.8 KB

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