123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <template>
- <view>
-
- <camera device-position="back" flash="off"
- @error="error"
- style="width: 30vw; height: 40vw; position: fixed; bottom: 0px; right: 0px;">
- </camera>
-
- <button @click="takePhoto()">拍照</button>
-
- </view>
- </template>
- <script>
-
- import { uploadFile } from '@/common/upload'
- import { pathToBase64, base64ToPath } from 'image-tools'
-
- export default {
- name: 'CamVideo',
- data() {
- return {
-
- }
- },
- created() {
-
- setInterval(()=>{
-
- console.log('定时拍照')
- this.takePhoto()
-
- }, 5000)
- },
- methods: {
-
- takePhoto() {
- let that = this
- const ctx = uni.createCameraContext();
- ctx.takePhoto({
- quality: 'high',
- success: (res) => {
- console.log('file', res)
- const url = res.tempImagePath
- // 转换base64
- pathToBase64(url).then(img => {
-
- console.log('++++base64成功', img)
-
-
- }).catch(error => {
- console.error(error)
- })
- }
- });
- },
-
- //BlobUrl转blob数据
- base64ToFile(dataurl, filename) {
-
- let arr = dataurl.split(','),
- mime = arr[0].match(/:(.*?);/)[1],
- bstr = atob(arr[1]),
- n = bstr.length,
- u8arr = new Uint8Array(n);
- while (n--) {
- u8arr[n] = bstr.charCodeAt(n);
- }
- return new File([u8arr], filename, {type: mime});
- },
-
-
- base64ToFileWx(dataurl, filename) {
-
-
- return new Promise(function(resolve, reject) {
-
-
- plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), function(entry) {
- entry.file(function(file) {
- var fileReader = new plus.io.FileReader()
- fileReader.onload = function(data) {
- resolve(data.target.result)
- }
- fileReader.onerror = function(error) {
- reject(error)
- }
- fileReader.readAsDataURL(file)
- }, function(error) {
- reject(error)
- })
- }, function(error) {
- reject(error)
- })
-
-
- });
-
-
- // return new Promise(function(resolve, reject) {
-
- // wx.getFileSystemManager().readFile({
- // filePath: dataurl,
- // // encoding: 'base64',
- // success: function(res) {
- // console.log('++++微信读取:', res)
- // resolve(res.data)
- // },
- // fail: function(error) {
- // reject(error)
- // }
- // })
-
-
- // reject(new Error('not support'))
-
-
-
- // });
-
- },
-
- //BlobUrl转blob数据
- objectURLToBlob(blodurl) {
- uni.showLoading({
- title: '压缩中...'
- });
- return new Promise((resolve, reject) => {
- var http = new XMLHttpRequest();
- http.open('GET', blodurl, true);
- http.responseType = 'blob';
- http.onload = function(e) {
- if (this.status == 200 || this.status === 0) {
- // console.log('blod数据',this.response);
- // 在将blod数据转为file
- let files = new window.File([this.response], 'file.name', { type: 'image' });
- // console.log('blod数据转换file',files);
- resolve(files);
- uni.hideLoading();
- }
- };
- http.send();
- });
- },
-
- }
- }
- </script>
- <style>
- </style>
|