Commit 3e6c8074 authored by 和慭琛's avatar 和慭琛

拍照处理方法梳理

parent 772e5c8b
......@@ -16,7 +16,7 @@
<cover-image class="backIcon" :src="icon_arrow_down" mode="" @click="backClick"></cover-image>
<!--cover-image class='flashIcon' :src='icon_light_on_white' @click="flashClick()"> </cover-image-->
</cover-view>
<!--canvas class="showCanvas" v-show='showImage' canvas-id="canvas"></canvas-->
<canvas v-show='showImage' canvas-id="canvas" :style="{'width':width,'height': height}"></canvas>
</view>
......@@ -46,6 +46,8 @@
liveWaterMarkImage:'/static/live_mark.png',
icon_arrow_down:'https://qn-static.banshouhui.com/arrow_down.png',
icon_live_mark:'https://qn-static.banshouhui.com/live_mark.png',
width:'',
height:'',
}
},
onLoad(e) {
......@@ -88,33 +90,110 @@
});
},
//拍照
//第一步: 拍照
takePhoto(){
let that = this
this.ctx.takePhoto({
quality: 'high',
success: (res) => {
console.log('拍照成功')
console.log(res)
this.imageChange(res)
//this.getImageInfo(res.tempImagePath)
//this.imageChange(res)
this.getImageInfo(res.tempImagePath)
//this.saveToLocal(res.tempImagePath)
}
});
},
//获取图片信息
//第二步: 获取图片信息
getImageInfo(tempImagePath) {
let that = this
uni.getImageInfo({
src:tempImagePath,
success(res) {
console.log('获取图片信息成功')
//that.saveToLocal(res.)
that.width = res.width/3+'px';
that.height = res.height/3+'px';
that.drawCanvas(tempImagePath,res)
}
})
},
//保存到本地相册
//第三步: 绘制画布
drawCanvas: function (tempImagePath,res){
console.log('绘制画布')
this.showImage = true
//将原图画到canvas上面
this.canvas = uni.createCanvasContext("canvas", this)
this.canvas.drawImage(tempImagePath, 0, 0, res.width / 3 , res.height / 3)
this.canvas.draw()
uni.showLoading({
title: '数据处理中...',
icon: 'loading',
duration: 10000
})
//that.canvas.draw()
this.exportCanvasTempFilePath(res.width,res.height)
},
//第四步: 导出画布
exportCanvasTempFilePath : function (width,height){
console.log('导出画布')
console.log('width:' + width)
console.log('height:' + height)
let that = this
//导出自己需要的图片
uni.canvasToTempFilePath({
canvasId: "canvas",
success: function(ress) {
that.image = ress.tempFilePath //处理好的图片
uni.hideLoading()
console.log('处理图片 成功:')
console.log(ress.tempFilePath)
uni.showToast({
icon:'none',
title:'处理成功'
})
setTimeout(function() {
that.saveToLocal(ress.tempFilePath)
uni.previewImage({
urls: [ress.tempFilePath],
current:0
});
}, 1000);
//TODO........
},
fail: function(e) {
uni.hideLoading()
uni.showToast({
title: '出错啦...',
icon: 'loading'
})
console.log('处理图片 失败:')
console.log(e)
}
});
},
//第五步: 保存到本地相册
saveToLocal(tempImagePath) {
uni.saveImageToPhotosAlbum({
filePath:tempImagePath,
......@@ -127,130 +206,25 @@
})
},
//照片处理
imageChange(options) {
var that = this
//原图片信息
var _width = options.width
var _height = options.height
that.path = options.tempImagePath
console.log('原照片信息')
console.log('宽:'+options.width)
console.log('高:'+options.height)
console.log('路径:'+options.tempImagePath)
//需要的图片信息
var image_x = parseInt(_width * 0.1);
var image_y = parseInt(_height * 0.09);
var image_width = parseInt(_width * 0.8);
var image_height = parseInt(_height * 0.87);
console.log('需要的图片信息')
console.log('x:'+image_x)
console.log('y:'+image_y)
console.log('宽:'+image_width)
console.log('高:'+image_height)
uni.getImageInfo({
src: that.path,
success: function(res) {
that.showImage = true
//将原图画到canvas上面
that.canvas = uni.createCanvasContext("canvas", that)
//that.canvas.drawImage(that.path, 0, 0, _width, _height)
that.canvas.drawImage(that.path, 0, 0, res.width * 3 , res.height * 3)
/*
//-----------------------------
// let ctx = uni.createCanvasContext('firstCanvas'); // 创建画布
// //将图片src放到cancas内,宽高为图片大小
// ctx.drawImage(res.tempFilePaths[0], 0, 0, ress.width / 3, ress.height / 3)
that.canvas.setFontSize(18)
// that.canvas.setFillStyle('#8a2929')
// that.canvas.rotate(-Math.PI / 2);
let textToWidth = res.width / 3 * 0.5;
let textToHeight = res.height / 3 * 0.3;
that.canvas.fillText('我是水印', 10, 90)
//. 除了上面的文字水印,这里也可以加入图片水印
// ctx.drawImage('/static/watermark.png', 0, 0, res.width / 3, ress.height / 3)
that.canvas.drawImage('/static/live_mark.png', 0, 0, 200, 200)
//-----------------------------
*/
that.canvas.draw()
uni.showLoading({
title: '数据处理中...',
icon: 'loading',
duration: 10000
})
that.canvas.draw()
setTimeout(function() {
//导出自己需要的图片
uni.canvasToTempFilePath({
canvasId: "canvas",
x: image_x,
y: image_y,
width: image_width,
height: image_height,
destWidth: image_width,
destHeight: image_height,
success: function(ress) {
that.image = ress.tempFilePath //处理好的图片
uni.hideLoading()
console.log('处理图片 成功:')
console.log(ress.tempFilePath)
uni.showToast({
icon:'none',
title:'处理成功'
})
setTimeout(function() {
that.saveToLocal(ress.tempFilePath)
uni.previewImage({
urls: [ress.tempFilePath],
current:0
});
// uni.navigateTo({
// url:'/pages/photo/preview'+'?path=' + res.tempFilePath
// })
}, 1000);
//TODO........
},
fail: function(e) {
uni.hideLoading()
uni.showToast({
title: '出错啦...',
icon: 'loading'
})
console.log('处理图片 失败:')
console.log(e)
}
});
}, 1000);
}
})
},
//获取当前时间
getTime:function(){
var date = new Date(),
year = date.getFullYear(),
month = date.getMonth() + 1,
day = date.getDate(),
hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
month >= 1 && month <= 9 ? (month = "0" + month) : "";
day >= 0 && day <= 9 ? (day = "0" + day) : "";
var timer = year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second;
return timer;
},
//获取当前时间
getTime:function(){
var date = new Date(),
year = date.getFullYear(),
month = date.getMonth() + 1,
day = date.getDate(),
hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
month >= 1 && month <= 9 ? (month = "0" + month) : "";
day >= 0 && day <= 9 ? (day = "0" + day) : "";
var timer = year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second;
return timer;
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment