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

拍照处理方法梳理

parent 772e5c8b
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
<cover-image class="backIcon" :src="icon_arrow_down" mode="" @click="backClick"></cover-image> <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-image class='flashIcon' :src='icon_light_on_white' @click="flashClick()"> </cover-image-->
</cover-view> </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> </view>
...@@ -46,6 +46,8 @@ ...@@ -46,6 +46,8 @@
liveWaterMarkImage:'/static/live_mark.png', liveWaterMarkImage:'/static/live_mark.png',
icon_arrow_down:'https://qn-static.banshouhui.com/arrow_down.png', icon_arrow_down:'https://qn-static.banshouhui.com/arrow_down.png',
icon_live_mark:'https://qn-static.banshouhui.com/live_mark.png', icon_live_mark:'https://qn-static.banshouhui.com/live_mark.png',
width:'',
height:'',
} }
}, },
onLoad(e) { onLoad(e) {
...@@ -88,117 +90,76 @@ ...@@ -88,117 +90,76 @@
}); });
}, },
//拍照 //第一步: 拍照
takePhoto(){ takePhoto(){
let that = this
this.ctx.takePhoto({ this.ctx.takePhoto({
quality: 'high', quality: 'high',
success: (res) => { success: (res) => {
console.log('拍照成功') console.log('拍照成功')
console.log(res) console.log(res)
this.imageChange(res)
//this.getImageInfo(res.tempImagePath) //this.imageChange(res)
this.getImageInfo(res.tempImagePath)
//this.saveToLocal(res.tempImagePath) //this.saveToLocal(res.tempImagePath)
} }
}); });
}, },
//获取图片信息
//第二步: 获取图片信息
getImageInfo(tempImagePath) { getImageInfo(tempImagePath) {
let that = this let that = this
uni.getImageInfo({ uni.getImageInfo({
src:tempImagePath, src:tempImagePath,
success(res) { success(res) {
console.log('获取图片信息成功')
//that.saveToLocal(res.) //that.saveToLocal(res.)
} that.width = res.width/3+'px';
}) that.height = res.height/3+'px';
},
//保存到本地相册 that.drawCanvas(tempImagePath,res)
saveToLocal(tempImagePath) {
uni.saveImageToPhotosAlbum({
filePath:tempImagePath,
success() {
console.log('保存本地相册 成功')
},
fail() {
console.log('保存本地相册 失败')
} }
}) })
}, },
//照片处理 //第三步: 绘制画布
imageChange(options) { drawCanvas: function (tempImagePath,res){
var that = this console.log('绘制画布')
//原图片信息 this.showImage = true
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上面 //将原图画到canvas上面
that.canvas = uni.createCanvasContext("canvas", that) this.canvas = uni.createCanvasContext("canvas", this)
//that.canvas.drawImage(that.path, 0, 0, _width, _height) this.canvas.drawImage(tempImagePath, 0, 0, res.width / 3 , res.height / 3)
that.canvas.drawImage(that.path, 0, 0, res.width * 3 , res.height * 3) this.canvas.draw()
/*
//-----------------------------
// 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({ uni.showLoading({
title: '数据处理中...', title: '数据处理中...',
icon: 'loading', icon: 'loading',
duration: 10000 duration: 10000
}) })
that.canvas.draw() //that.canvas.draw()
setTimeout(function() { 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({ uni.canvasToTempFilePath({
canvasId: "canvas", canvasId: "canvas",
x: image_x,
y: image_y,
width: image_width,
height: image_height,
destWidth: image_width,
destHeight: image_height,
success: function(ress) { success: function(ress) {
that.image = ress.tempFilePath //处理好的图片 that.image = ress.tempFilePath //处理好的图片
uni.hideLoading() uni.hideLoading()
console.log('处理图片 成功:') console.log('处理图片 成功:')
console.log(ress.tempFilePath) console.log(ress.tempFilePath)
uni.showToast({ uni.showToast({
icon:'none', icon:'none',
title:'处理成功' title:'处理成功'
...@@ -211,12 +172,10 @@ ...@@ -211,12 +172,10 @@
urls: [ress.tempFilePath], urls: [ress.tempFilePath],
current:0 current:0
}); });
// uni.navigateTo({
// url:'/pages/photo/preview'+'?path=' + res.tempFilePath
// })
}, 1000);
}, 1000);
//TODO........ //TODO........
}, },
...@@ -230,12 +189,23 @@ ...@@ -230,12 +189,23 @@
console.log(e) console.log(e)
} }
}); });
}, 1000);
},
//第五步: 保存到本地相册
saveToLocal(tempImagePath) {
uni.saveImageToPhotosAlbum({
filePath:tempImagePath,
success() {
console.log('保存本地相册 成功')
},
fail() {
console.log('保存本地相册 失败')
} }
}) })
}, },
//获取当前时间 //获取当前时间
getTime:function(){ getTime:function(){
...@@ -259,6 +229,10 @@ ...@@ -259,6 +229,10 @@
} }
} }
</script> </script>
......
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