uniapp 照片添加水印

  • 上代码
<canvas 
    id="myCanvas" 
    canvas-id="myCanvas" 
    type="2d"
	:style="{width: canvasSize.width+'px',
        height: canvasSize.height+'px',
        position:'absolute',
        top:'-1000px',
        right:'1000px'}" 
    />    
data() {
	return {
		canvasSize: {
			width: 200,
			height: 200
		},
	}
}
  • 获取图片路径
uni.chooseImage({
	count: 1, //默认9
	sizeType: ['compressed'], // ['original', 'compressed'],可以指定是原图还是压缩图,默认二者都有
	sourceType: ['camera'], // ['camera','album'], //这要注意,camera掉拍照,album是打开手机相册
	success: (res) => {
		//this.watermark(res.tempFilePaths[0])
	}
});
  • 加水印(写在你的方法里)
uni.getImageInfo({
	src: path,//图片路径
	success: (image) => {
		this.canvasSize.width = image.width;
		this.canvasSize.height = image.height;
		//尺寸重置没生效,故做延迟
		setTimeout(() => {
			let ctx = uni.createCanvasContext('myCanvas', this);
			ctx.drawImage(path, 0, 0, image.width, image.height); // image.width, image.height
			// 设置文字水印
			ctx.setFillStyle('white');//颜色
			ctx.setFontSize(30);//大小
			ctx.fillText('哈哈哈', 10, 10, 500);//(text,x轴,y,width)
            // 添加图片 drawImage(图片路径, x, y,图像宽, 图像高)
            ctx.drawImage(path, 550, 1000, 120, 120);
			// ctx.draw()
			ctx.draw(false, () => {
				// 导出图片
				uni.canvasToTempFilePath({
					canvasId: 'myCanvas',
					destWidth: image.width,
					destHeight: image.height,
					fileType: 'png',
					success: (res) => {
						// this.savePhoto(res.tempFilePath, 'name');
                        //你的操作
					    }
					},
					this
				);
			});
		}, 500)
	}
});

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值