【小程序图片水印】微信小程序图片加水印功能 canvas绘图

news/2024/7/20 1:30:17 标签: 小程序, 微信小程序, canvas, 水印

看看效果

在这里插入图片描述

实现步骤:

1.选择图片

/* 选择背景图片 对图片的大小以及类型做一下限制 */
chooseImage(e) {
	uni.chooseImage({
		count: 1,
		success: (res) => {
			let index = res.tempFilePaths[0].lastIndexOf(".");
			let imgUrl = res.tempFilePaths[0].substr(index + 1);

			if (imgUrl != "png" && imgUrl != "jpg" && imgUrl != "jpeg") {
				uni.showToast({
					title: '请上传jpg、jpeg、png类型的图片',
					icon: 'none'
				});
				return
			}
			if (res.tempFiles[0].size / 1024 < 1024 * 1024 * 20) {
				this.originImg = res.tempFilePaths[0]
				this.imageSrc = res.tempFilePaths[0]
				this.loadImage();
			} else {
				uni.showToast({
					title: '图片大小不能超过20M,当前大小' + (res.tempFiles[0].size / 1024).toFixed(
						2) + 'KB',
					icon: 'none'
				})
			}
		},
	});
},

图片展示区域计算

/* 将图片加载到画布  */
loadImage() {
	uni.showLoading({
		title: "图片加载中...",
	});

	/* 获取图片信息  */
	uni.getImageInfo({
		src: this.imageSrc,
		success:(res) => {
			let imgH = res.height;
			let imgW = res.width;

			/* 图片的宽高比  */
			IMG_RATIO = imgW / imgH;

			/**
			 * 如果图片更高一些,为确保图片能够完整在视窗内显示需如下处理
			 * 1. 缩放图片的高为 视窗高度减去底部菜单按钮高度(120)
			 * 2. 根据图片缩放后的高度,根据图片宽高比计算图片的宽度
			 * 3. 如果步骤2计算的图片宽度大于屏幕宽度,则需要再次调整图片宽度为视窗宽度-margin(10)
			 * 4. 根据步骤3的宽度,结合图片宽高比重新计算图片的高度
			 */
			if (IMG_RATIO < 1 && (SCREEN_HEIGHT - 133) * IMG_RATIO < SCREEN_WIDTH - 10) {
				IMG_REAL_W = (SCREEN_HEIGHT - 133) * IMG_RATIO;
				IMG_REAL_H = SCREEN_HEIGHT - 133;
			} else {
				IMG_REAL_W = SCREEN_WIDTH - 10;
				IMG_REAL_H = IMG_REAL_W / IMG_RATIO;
			}
				/* 裁剪区域的宽高同图片尺寸  */
				this.cropperW=IMG_REAL_W,
				this.cropperH=IMG_REAL_H,
				/* 上下左右各留一定的margin已便更好的拖动裁剪区域  */
				this.cropperL=Math.ceil((SCREEN_WIDTH - IMG_REAL_W) / 2),
				/* 留出底部操作按钮位置 70  */
				this.cropperT=uni.getStorageSync("navHeight"),
				// 图片缩放值
				this.imageW=IMG_REAL_W,
				this.imageH=IMG_REAL_H,
				this.isShowImg=true,

				uni.hideLoading();
				this.finish()
		},
	});
},

canvas_85">图片水印canvas绘制

/* 完成裁剪,输出裁剪后的图片路径  */
finish() {
	uni.showLoading({
		title: "图片生成中...",
	});
	// 水印加载
	const ctx = uni.createCanvasContext("myCanvas", this);
	ctx.clearRect(0, 0, IMG_REAL_W, IMG_REAL_H);
	// 将图片写入画布
	ctx.drawImage(this.imageSrc, 0, 0, IMG_REAL_W, IMG_REAL_H);
	ctx.save();
	ctx.beginPath();

	if (['alone'].includes(this.changeMode)) {
		ctx.beginPath()
		ctx.setFontSize(this.changeSize)
		ctx.setFillStyle(this.changeColor)
		ctx.fillText(this.changeText, this.imageW - this.changeSize * this.changeText.length - 10, this.imageH - this.changeSize)
	}

	if (['level'].includes(this.changeMode)) {
		for (let j = 1; j < 12; j++) {
			ctx.beginPath()
			ctx.setFontSize(this.changeSize)
			ctx.setFillStyle(this.changeColor)
			ctx.fillText(this.changeText, 0, 50 * j)
			for (let i = 1; i < 12; i++) {
				ctx.beginPath()
				ctx.setFontSize(this.changeSize)
				ctx.setFillStyle(this.changeColor)
				ctx.fillText(this.changeText, (15 + (this.changeSize - 1) * this.changeText.length) * i, 50 * j)
			}
		}
	}

	if (["incline"].includes(this.changeMode)) {
		ctx.font = this.changeSize + "px serif";
		ctx.fillText(this.changeText, -1000, -1000)
      const textWidth = ctx.measureText(this.changeText + "").width
		const _textWidth = textWidth * 1.5
		const _textHeight = 40
		const xSize = Math.floor(this.imageW / _textWidth + 1)
     const ySize = Math.floor(this.imageH / _textHeight + 1)
		// 开始绘制水印
      // 左右各多出一半图片宽度的水印,是用来处理旋转后盖住空白部分的;上下同理:
		for (var x = 0; x < xSize * 2; x++) {
        // x控制横向多少个水印
        for (var y = 0; y < ySize * 2; y++) {
          // y控制纵向多少个水印
          // 绘制文字  注意::绘制斜体文字 旋转以后会发生位移,所以必须在旋转之后进行位置的调整;
          this.drawText(ctx, (this.imageW), (this.imageH), -this.imageW / 2 + ((x * _textWidth)), -this.imageH / 2 + ((y * _textHeight)))
        }
      }
	}

	ctx.draw(true, () => {
		// 获取画布要裁剪的位置和宽度   均为百分比 * 画布中图片的宽度    保证了在微信小程序中裁剪的图片模糊  位置不对的问题 canvasT = (this.cutT / this.cropperH) * (this.imageH / pixelRatio)
		var canvasW = IMG_REAL_W;
		var canvasH = IMG_REAL_H;
		var canvasL = 0;
		var canvasT = 0;
		uni.canvasToTempFilePath({
				x: canvasL,
				y: canvasT,
				width: canvasW,
				height: canvasH,
				// destWidth: canvasW,
				// destHeight: canvasH,
				quality: +this.quality,
				fileType: this.fileType,
				canvasId: "myCanvas",
				success: (res) => {
					uni.hideLoading();
					// this.saveImg()
					this.waterImgSrc = res.tempFilePath
				},
				fail: (err) => {
					uni.hideLoading();
					uni.showToast({
						title: "图片截取失败!",
						icon: "none",
					});
				},
			},
			this
		);
	});
},
drawText(ctx, imgWidth, imgHeight, x, y) {
	var text = this.changeText;
	ctx.save(); //保存原来的状态  绘制字体都是需要旋转倾斜  那么之前绘制的图片就要进行状态的保存
	// ctx.globalAlpha = 0.6
	// 移动到中心点,再旋转相当于按照之前的原点旋转了
	ctx.translate(imgWidth / 2, imgHeight / 2)
	ctx.rotate(-Math.PI / 6); //绘制倾斜字体
	// 移动回原来的位置:
	ctx.translate(-imgWidth / 2, -imgHeight / 2)
	//ctx.translate(tsx, tsy); //发生位移进行位移的恢复
	ctx.font = this.changeSize + "px serif";
	ctx.fillStyle = this.changeColor;
	ctx.fillText(text, x, y);
	ctx.restore(); //状态的恢复
},

图片保存逻辑

saveImg() {
	const path = this.waterImgSrc
	uni.showShareImageMenu({ 
		path,
		success: (res)=>{
			console.log(res)
			wx.showToast({
				title: '生成成功!',
				icon: 'success',
				duration: 2000//持续的时间
			})
		},
		fail: (err)=> {
			console.log(err)
			wx.showToast({
				title: '生成失败!',
				icon: 'none',
				duration: 2000//持续的时间
			})
		}
	})
},

感觉有用的话,可以打赏一把么?一毛不嫌少,十块不嫌多
在这里插入图片描述
更多详细代码请关注公众号索取(备注:公众号):
在这里插入图片描述


http://www.niftyadmin.cn/n/5123565.html

相关文章

Android加载SO包

一、前言 这几天用Android整合开源的RTMP推拉流都没成功&#xff0c;好几年没玩Android了碰到好多坑&#xff0c;在Android中为了效率难免需要调用C语言编写生成的SO文件&#xff0c;比如图片渲染加速&#xff0c;视频编解码等插件&#xff0c;今天我们就先聊一下在Android中如…

苹果将于10月31日举行今秋的第二场发布会

在今日凌晨&#xff0c;苹果宣布&#xff0c;将于北京时间10月31日早上8点举行今秋的第二场发布会&#xff0c;主题为“来势迅猛”。据多方猜测苹果本次活动的核心产品大概率是搭载全新M3芯片的Mac系列产品。 据了解&#xff0c;在苹果的产品线中&#xff0c;搭载M3芯片的Mac系…

uniapp编译微信小程序富文本rich-text的图片样式不生效原因

this.detail.contents this.detail.contents.replace(/\<img/gi, <img style"display:block;max-width:90%;height:auto;border:2px solid #eee;box-shadow:5px 5px 5px rgba(100,100,100,0.8);margin-bottom:10px;text-align:center;" );开始采用这个replace…

Windows server部署filebeat到kafka

需求&#xff1a;Windows dhcp日志需要实时传输到elk或者其他告警平台。 1、filebeat下载地址&#xff1a;https://www.elastic.co/cn/downloads/beats/filebeat 2、下载后解压后配置filebeat.yml文件&#xff0c; 3、README.md文件中有运行的操作方法&#xff1a;cmd上进入f…

使用flask_login出错:AttributeError: type object ‘User‘ has no attribute ‘is_active‘

if not force and not user.is_active: AttributeError: type object User has no attribute is_active 原来是User类没有继承UserMixin from flask_login import UserMixin class User(UserMixin,db.Model):

有六家机器视觉公司今年11月份初放假到明年春节后,除夕不放假看住企业不跑路,不倒闭,明年大家日子会越来越甜

不幸的消息一个接着一个&#xff0c;请大家注意下面的消息 我已经收到已经有6家机器视觉公司今年11月份初放假到明年春节后&#xff0c;他们真的没有订单了&#xff0c;其中4家宣布员工可以自行寻找工作&#xff0c;今年除夕不放假是经济下行经济考量吗&#xff1f;看住企业不…

Redis桌面管理工具:Redis Desktop Manager for Mac

Redis Desktop Manager是一款非常实用的Redis管理工具&#xff0c;它不仅提供了方便易用的图形用户界面&#xff0c;还支持多种Redis数据结构&#xff0c;可以帮助用户轻松地完成Redis数据库的管理工作。 以下是一些推荐Redis Desktop Manager的理由&#xff1a; 多平台支持&a…