yf-photo-take.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <view>
  3. <button type="primary" @tap="takePhoto">拍照人脸识别</button>
  4. </view>
  5. </template>
  6. <script>
  7. import { pathToBase64, base64ToPath } from 'image-tools'
  8. export default {
  9. name: 'yf-photo-take',
  10. props:{
  11. value: String,
  12. base64: String
  13. },
  14. data() {
  15. return {
  16. photo: '',
  17. photoBase64: ''
  18. }
  19. },
  20. watch:{
  21. value: {
  22. handler(val){
  23. if(!val){
  24. this.photo = ''
  25. this.photoBase64 = ''
  26. }
  27. }
  28. },
  29. photo: {
  30. handler(val){
  31. this.$emit('input', val)
  32. this.$emit('update:base64', this.photoBase64)
  33. }
  34. }
  35. },
  36. methods: {
  37. // 拍照选图
  38. takePhoto(){
  39. // #ifdef MP-WEIXIN
  40. this.mediaChoose()
  41. // #endif
  42. // #ifndef MP-WEIXIN
  43. this.imageChoose()
  44. // #endif
  45. },
  46. // 压缩和处理图片
  47. compressImage(url){
  48. let that = this
  49. uni.showLoading({
  50. title: '正在处理图片..'
  51. })
  52. // #ifndef H5
  53. uni.compressImage({
  54. src: url,
  55. width: '500px',
  56. height: 'auto',
  57. quality: 90,
  58. success: res => {
  59. console.log('uni-压缩:', res.tempFilePath)
  60. url = res.tempFilePath
  61. // 转换base64
  62. pathToBase64(url).then(img => {
  63. // 去除png文件头
  64. that.photoBase64 = img.replace(/^data:image\/\w+;base64,/, "");
  65. that.photo = url
  66. uni.hideLoading()
  67. }).catch(error => {
  68. console.error(error)
  69. uni.hideLoading()
  70. })
  71. }
  72. })
  73. // #endif
  74. // #ifdef H5
  75. pathToBase64(url).then(img => {
  76. // H5压缩base64
  77. that.$utils.compressImage(img, 500).then(psd=>{
  78. // 去除png文件头
  79. that.photoBase64 = psd.replace(/^data:image\/\w+;base64,/, "");
  80. that.photo = url
  81. uni.hideLoading()
  82. })
  83. }).catch(error => {
  84. console.error(error)
  85. uni.hideLoading()
  86. })
  87. // #endif
  88. },
  89. // 小程序平台选择图片及压缩
  90. mediaChoose(){
  91. let that = this
  92. uni.chooseMedia({
  93. count: 1,
  94. mediaType: ['image'],
  95. sourceType: ['camera'],
  96. maxDuration: 30,
  97. camera: 'front',
  98. success(res) {
  99. let url = ''
  100. if(res.tempFilePaths){
  101. url = res.tempFilePaths[0]
  102. }else{
  103. url = res.tempFiles[0].tempFilePath
  104. }
  105. that.compressImage(url);
  106. }
  107. })
  108. },
  109. // H5平台选择图片和压缩
  110. imageChoose(){
  111. let that = this
  112. uni.chooseImage({
  113. count: 1,
  114. sizeType: ['original', 'compressed'],
  115. sourceType: ['camera'], //这要注意,camera掉拍照,album是打开手机相册
  116. success: (res)=> {
  117. let url = res.tempFilePaths[0]
  118. that.compressImage(url);
  119. }
  120. });
  121. }
  122. }
  123. }
  124. </script>