video.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <view>
  3. <camera device-position="back" flash="off"
  4. @error="error"
  5. style="width: 30vw; height: 40vw; position: fixed; bottom: 0px; right: 0px;">
  6. </camera>
  7. <button @click="takePhoto()">拍照</button>
  8. </view>
  9. </template>
  10. <script>
  11. import { uploadFile } from '@/common/upload'
  12. import { pathToBase64, base64ToPath } from 'image-tools'
  13. export default {
  14. name: 'CamVideo',
  15. data() {
  16. return {
  17. }
  18. },
  19. created() {
  20. setInterval(()=>{
  21. console.log('定时拍照')
  22. this.takePhoto()
  23. }, 5000)
  24. },
  25. methods: {
  26. takePhoto() {
  27. let that = this
  28. const ctx = uni.createCameraContext();
  29. ctx.takePhoto({
  30. quality: 'high',
  31. success: (res) => {
  32. console.log('file', res)
  33. const url = res.tempImagePath
  34. // 转换base64
  35. pathToBase64(url).then(img => {
  36. console.log('++++base64成功', img)
  37. }).catch(error => {
  38. console.error(error)
  39. })
  40. }
  41. });
  42. },
  43. //BlobUrl转blob数据
  44. base64ToFile(dataurl, filename) {
  45. let arr = dataurl.split(','),
  46. mime = arr[0].match(/:(.*?);/)[1],
  47. bstr = atob(arr[1]),
  48. n = bstr.length,
  49. u8arr = new Uint8Array(n);
  50. while (n--) {
  51. u8arr[n] = bstr.charCodeAt(n);
  52. }
  53. return new File([u8arr], filename, {type: mime});
  54. },
  55. base64ToFileWx(dataurl, filename) {
  56. return new Promise(function(resolve, reject) {
  57. plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), function(entry) {
  58. entry.file(function(file) {
  59. var fileReader = new plus.io.FileReader()
  60. fileReader.onload = function(data) {
  61. resolve(data.target.result)
  62. }
  63. fileReader.onerror = function(error) {
  64. reject(error)
  65. }
  66. fileReader.readAsDataURL(file)
  67. }, function(error) {
  68. reject(error)
  69. })
  70. }, function(error) {
  71. reject(error)
  72. })
  73. });
  74. // return new Promise(function(resolve, reject) {
  75. // wx.getFileSystemManager().readFile({
  76. // filePath: dataurl,
  77. // // encoding: 'base64',
  78. // success: function(res) {
  79. // console.log('++++微信读取:', res)
  80. // resolve(res.data)
  81. // },
  82. // fail: function(error) {
  83. // reject(error)
  84. // }
  85. // })
  86. // reject(new Error('not support'))
  87. // });
  88. },
  89. //BlobUrl转blob数据
  90. objectURLToBlob(blodurl) {
  91. uni.showLoading({
  92. title: '压缩中...'
  93. });
  94. return new Promise((resolve, reject) => {
  95. var http = new XMLHttpRequest();
  96. http.open('GET', blodurl, true);
  97. http.responseType = 'blob';
  98. http.onload = function(e) {
  99. if (this.status == 200 || this.status === 0) {
  100. // console.log('blod数据',this.response);
  101. // 在将blod数据转为file
  102. let files = new window.File([this.response], 'file.name', { type: 'image' });
  103. // console.log('blod数据转换file',files);
  104. resolve(files);
  105. uni.hideLoading();
  106. }
  107. };
  108. http.send();
  109. });
  110. },
  111. }
  112. }
  113. </script>
  114. <style>
  115. </style>