wx.getImageInfo({
src: result,
success: function (res) {
let originWidth, originHeight;
originHeight = res.height;
originWidth = res.width;
console.log("the origin width:" + originWidth + " the origin height:" + originHeight);
//压缩比例
// 最大尺寸限制
let maxWidth = 300,
maxHeight = 100;
// 目标尺寸
let targetWidth = originWidth,
targetHeight = originHeight;
//等比例压缩,如果宽度大于高度,则宽度优先,否则高度优先
if (originWidth > maxWidth || originHeight > maxHeight) {
if (originWidth / originHeight > maxWidth / maxHeight) {
// 要求宽度*(原生图片比例)=新图片尺寸
targetWidth = maxWidth;
targetHeight = Math.round(maxWidth * (originHeight / originWidth));
} else {
targetHeight = maxHeight;
targetWidth = Math.round(maxHeight * (originWidth / originHeight));
}
}
//更新canvas大小
that.setData({
cw: targetWidth,
ch: targetHeight
});
const query = wx.createSelectorQuery()
query.select('#zipCanvas')
.fields({
node: true,
size: true
})
.exec(async res => {
// 获取 canvas 实例
const canvas = res[0].node
// 获取 canvas 绘图上下文
const ctx = canvas.getContext('2d')
// 根据设备dpr处理尺寸
const dpr = wx.getSystemInfoSync().pixelRatio
canvas.width = targetWidth * dpr
canvas.height = targetHeight * dpr
console.log('canvas.width, canvas.height', canvas.width, canvas.height)
ctx.scale(dpr, dpr)
// 将图片绘制到 canvas
ctx.drawImage(result, 0, 0, targetWidth, targetHeight)
setTimeout(function () {
// 将canvas图片上传到微信临时文件
wx.canvasToTempFilePath({
canvas,
x: 0,
y: 0,
destWidth: targetWidth,
destHeight: targetHeight,
complete(res) {
if (res.errMsg === 'canvasToTempFilePath:ok') {
// 返回临时文件路径
that.uploadImg(res.tempFilePath)
}
},
fail(err) {
wx.hideLoading();
wx.showToast({
title: '电子签名压缩失败',
icon: 'none',
duration: 2000
});
}
})
}, 1000);
})
}
})
拿到的图片为空白,请大佬帮帮解决一下,多谢!!!
给代码片段
你这代码是给人看的吗