index.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <template>
  2. <Layout @instruct="handleInstruct">
  3. <camera class="camera" mode="normal" :device-position="device" @error="error"
  4. style="width: 100%; height: 100%;" resolution="750x1234">
  5. <cover-image v-if="coverImage" :src="coverImage" style="width: 100%;height: 100%;"></cover-image>
  6. </camera>
  7. </Layout>
  8. </template>
  9. <script>
  10. import Layout from './Layout.vue'
  11. import config from '../config.js'
  12. export default {
  13. components: {
  14. Layout
  15. },
  16. props: {
  17. coverImageType: { //遮罩层的类型 (必须在config里面的定义的)
  18. type: String,
  19. default: 'portrait'
  20. }
  21. },
  22. data() {
  23. return {
  24. device: 'back',
  25. cameraContext: null,
  26. shutterShow: false,
  27. coverImage: null,
  28. }
  29. },
  30. mounted() {
  31. this.cameraContext = uni.createCameraContext();
  32. try {
  33. this.coverImage = config[this.coverImageType]
  34. } catch (e) {
  35. uni.showToast({
  36. title: '传入的coverImageType不存在',
  37. icon: 'none'
  38. })
  39. }
  40. },
  41. methods: {
  42. error(err) {
  43. console.log(err)
  44. },
  45. handleInstruct(instruct) {
  46. switch (instruct) {
  47. // 返回
  48. case 'back':
  49. this.$emit('back')
  50. break;
  51. // 快门
  52. case 'shutter':
  53. this.cameraContext.takePhoto({
  54. quality: 'high',
  55. success: (res) => {
  56. // console.log(res)
  57. this.$emit('getImage', res.tempImagePath)
  58. }
  59. })
  60. break;
  61. // 反转
  62. case 'reversal':
  63. this.device = this.device === 'back' ? 'front' : 'back'
  64. break;
  65. // 相册
  66. case 'album':
  67. uni.chooseImage({
  68. count: 1, //默认9
  69. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  70. sourceType: ['album'], //从相册选择
  71. success: (res) => {
  72. this.$emit('getImage', res.tempFilePaths[0])
  73. }
  74. })
  75. break;
  76. }
  77. }
  78. }
  79. }
  80. </script>
  81. <style scoped>
  82. .camera-background {
  83. width: 100%;
  84. height: 100%;
  85. background-color: rgba(0, 0, 0, 0.3);
  86. }
  87. .camera {
  88. position: relative;
  89. }
  90. .shutter-light {
  91. position: absolute;
  92. top: 0;
  93. left: 0;
  94. z-index: 9;
  95. width: 100%;
  96. height: 100%;
  97. background-color: #fff;
  98. opacity: 0;
  99. transition: opacity 0.1s ease-out;
  100. }
  101. .shutter-light-active {
  102. opacity: 1;
  103. }
  104. </style>