makeCanvas: function(imgPath) {
console.log("start draw");
var ctx = wx.createCanvasContext('canvas')
// 设置背景
ctx.setFillStyle('#ffffff')
ctx.fillRect(0, 0, this.data.canvasWidth, this.data.canvasWidth)
// 描绘图片
/**
* @function drawImage Canvas描绘图片
* @param {String} imgPath 图片路径
* @param {Number} imgWidth 图片宽度
* @param {Number} imgHeight 图片高度
* @author mossbaoo
*/
let cWidth = this.data.canvasWidth*0.5;
let cHeight = this.data.canvasWidth*0.5;
let imgWidth = this.data.imgInfo.width;
let imgHeight = this.data.imgInfo.height
console.log(cWidth+"_"+cHeight+"_"+imgWidth+"_"+imgHeight);
let dWidth = cWidth/imgWidth; // canvas与图片的宽度比例
let dHeight = cHeight/imgHeight; // canvas与图片的高度比例
if (imgWidth > cWidth && imgHeight > cHeight || imgWidth < cWidth && imgHeight < cHeight) {
if (dWidth > dHeight) {
ctx.drawImage(imgPath, 0, (imgHeight - cHeight/dWidth)/2, imgWidth, cHeight/dWidth, 0, 0, cWidth, cHeight)
} else {
ctx.drawImage(imgPath, (imgWidth - cWidth/dHeight)/2, 0, cWidth/dHeight, imgHeight, 0, 0, cWidth, cHeight)
}
} else {
if (imgWidth < cWidth) {
ctx.drawImage(imgPath, 0, (imgHeight - cHeight/dWidth)/2, imgWidth, cHeight/dWidth, 0, 0, cWidth, cHeight)
} else {
ctx.drawImage(imgPath, (imgWidth - cWidth/dHeight)/2, 0, cWidth/dHeight, imgHeight, 0, 0, cWidth, cHeight)
}
}
ctx.draw();
console.log("end draw");
debugger;
var that = this;
wx.canvasToTempFilePath({
canvasId: 'canvas',
success: function success(res) {
that.setData({
canvasImgUrl: res.tempFilePath
});
that.canvasShow=false;
console.log(res.tempFilePath);
// 人脸验证
let base64 = wx.getFileSystemManager().readFileSync(res.tempFilePath, 'base64');
console.log("base64two:"+base64.length);
if (!that.isLoading) {
that.setData({
isLoading: !0
});
//先活体face/faceVerify
that.vertifyImg(base64);
}
},
complete: function complete(e) {
}
});
}
微信小程序开发-canves 图片压缩 获取base64
猜你喜欢
转载自blog.csdn.net/wxb880114/article/details/109232714
今日推荐
周排行