<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>
