直接上代码
<template>
<view class="inspection">
<view class="inspection_details">
<view class="upload public" @click="uploadFile">
<text>上传图片</text>
<text class="upload_icon">+</text>
</view>
<canvas canvas-id="waterMarkCanvas" style="width: 500rpx; height: 500rpx;" />
</view>
<view class="submit">
<text class="submit_btn" @click="submit">提交</text>
</view>
</view>
</template>
<script>
export default {
data() {
return {
cvHeight: 0,
cvWidth: 0,
canvasImg: undefined,
canvasWidth: 0,
canvasHeight: 0
};
},
methods: {
uploadFile() {
let that = this;
uni.chooseImage({
count: 1,
sourceType: ['album'],
success: function(res) {
uni.getImageInfo({
src: res.tempFilePaths[0],
success: function(image) {
const ctx = uni.createCanvasContext('waterMarkCanvas')
console.log(image.width);
console.log(image.height);
console.log('详情', image);
const canvasWidth = uni.upx2px(300); // 500rpx转px
const canvasHeight = uni.upx2px(500);
ctx.drawImage(image.path, 0, 0, canvasWidth, canvasHeight);
ctx.setFontSize(16);
ctx.setFillStyle('red');
const watermarkText = '测试';
ctx.fillText(watermarkText, 10, canvasHeight - 10);
ctx.draw(true)
setTimeout(() => {
uni.canvasToTempFilePath({
canvasId: 'waterMarkCanvas',
fileType: "jpg",
width: canvasWidth,
height: canvasHeight,
destWidth: canvasWidth,
destHeight: canvasHeight,
success: (res) => {
console.log('水印生成:', res);
console.log(res.tempFilePath);
that.canvasImg = res.tempFilePath;
},
fail: (err) => {
console.error('水印生成失败:', JSON.stringify(err)); // 输出完整错误信息
}
});
}, 100)
}
});
}
});
},
submit() {
uni.showToast({
title: this.canvasImg
});
},
}
};
</script>
<style scoped lang="scss">
.inspection {
background-color: #f1f1f1;
}
.inspection_details {
padding: 20rpx;
box-sizing: border-box;
}
.textarea {
border: 1rpx solid #dcdcdc;
width: 710rpx;
background-color: #fff;
}
.public {
padding: 20rpx;
box-sizing: border-box;
font-size: 34rpx;
background-color: #fff;
margin-top: 30rpx;
}
.upload {
display: flex;
justify-content: space-between;
align-items: center;
flex-direction: row;
height: 110rpx;
}
.upload_icon {
font-size: 60rpx;
color: #838383;
}
.content {
display: flex;
flex-direction: column;
}
.active {
background-color: #009944;
color: #fff;
}
.activeNo {
background-color: #ff4244;
color: #fff;
}
.submit {
box-sizing: border-box;
margin-top: 30rpx;
padding: 20rpx;
position: fixed;
left: 0;
bottom: 0rpx;
background-color: #fff;
width: 750rpx;
}
.submit_btn {
height: 90rpx;
background-color: #009944;
color: #ffffff;
font-size: 34rpx;
border-radius: 20rpx;
text-align: center;
line-height: 90rpx;
// margin-bottom: 30rpx;
width: 100%;
display: inline-block;
}
.margin {
margin-top: 0;
margin-bottom: 20rpx;
display: flex;
align-items: center;
flex-direction: row;
}
.uni-input {
flex: 1;
height: 60rpx;
}
.garden_item {
display: flex;
align-items: center;
flex-direction: row;
padding: 20rpx 0;
}
.normal {
border-bottom: 1rpx solid #b7b7b7;
}
.garden {
width: 36rpx;
height: 36rpx;
border-width: 1rpx;
border-color: #b7b7b7;
border-style: solid;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
margin-right: 20rpx;
}
.child_garden {
width: 27rpx;
height: 27rpx;
border-radius: 50%;
}
.activeGarden {
background-color: #009944;
}
</style>
代码自己抽离 拿到最zhong的地址进行保存就成