H5Camera.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <template>
  2. <div class="h5Camera">
  3. <van-icon class="photo photos" name="photograph" size="22px" color="#969696" @click="camera" />
  4. <input type="file" accept="image/*" :capture="capture" id="h5Camera" />
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'H5Camera',
  10. props: {
  11. capture: {
  12. // 摄像头类型 camera:照相机;camcorder:摄像机;microphone:录音
  13. type: String,
  14. default: '',
  15. },
  16. objectType: {
  17. type: String,
  18. default: '',
  19. },
  20. },
  21. data() {
  22. return {
  23. imgUrl: '',
  24. input: null,
  25. };
  26. },
  27. mounted() {
  28. this.getH5Cameradome();
  29. },
  30. methods: {
  31. getH5Cameradome() {
  32. let h5Camera = document.getElementById('h5Camera');
  33. if (h5Camera) {
  34. this.input = h5Camera;
  35. this.input.addEventListener('change', (e) => {
  36. let file = e.target.files[0];
  37. let reader = new FileReader();
  38. reader.onload = (e) => {
  39. let dataURL = e.target.result;
  40. this.$emit('getImg', dataURL);
  41. // console.log(dataURL);
  42. // 在此处对 dataURL 进行操作,例如显示图片
  43. };
  44. reader.readAsDataURL(file);
  45. });
  46. } else {
  47. this.getH5Cameradome();
  48. }
  49. },
  50. camera() {
  51. if (this.objectType == '' || this.objectType == null) {
  52. this.$toast('请选择类型!');
  53. return;
  54. }
  55. if (this.input) this.input.click();
  56. },
  57. },
  58. };
  59. </script>
  60. <style lang="scss" scoped>
  61. .h5Camera {
  62. #h5Camera {
  63. display: none;
  64. }
  65. }
  66. </style>