uniApp实战二:仿今日相机水印功能

文章目录

1.最终效果预览

2.页面实现

页面布局

复制代码
<view @click="handleTakePhotoNew">拍照</view>
			<view v-if="!snapSrc">
				<canvas canvas-id="canvas" :style="{'width':cvWidth+'px','height':cvHeight+'px'}">
				</canvas>
			</view>
			<view v-else>
				<image :style="{'width':cvWidth+'px','height':cvHeight+'px'}" mode="aspectFit" :src="snapSrc">
				</image>
			</view>

data定义

复制代码
data() {
			return {
				snapSrc: "",
				cvHeight: "",
				cvWidth: "",
				tKey: "时 间:",
				addKey: "地 点:",
			};
		},

点击事件及方法

复制代码
handleTakePhotoNew() {
				this.snapSrc = ""
				uni.chooseImage({
					count: 1,
					success: (res) => {
						const imgPath = res.tempFilePaths[0]
						this.getWaterImageNew(imgPath)
					}
				});

			},


			async getWaterImageNew(imgPath) {
				const {
					width,
					height
				} = await this.getImageSizeByUrl(imgPath);
				this.cvWidth = width
				this.cvHeight = height
				if (this.cvWidth > 350) {
					let rad = this.cvWidth / 350
					this.cvWidth = this.cvWidth / rad
					this.cvHeight = this.cvHeight / rad
				}

				const obj = {
					url: imgPath,
					title: '日常巡检',
					time: '2025-03-03 22:22',
					address: "详细地址信息XXX",
					width: this.cvWidth,
					height: this.cvHeight
				}
				this.snapSrc = await this.imageAddWatermarksNew(obj)
			},


			async imageAddWatermarksNew(obj) {
				try {
					const waterUrl = await this.imgToCanvasNew(obj);
					return waterUrl
				} catch (error) {
					console.error(error);
				}
			},

			getImageSizeByUrl(url) {
				return new Promise((resolve, reject) => {
					uni.getImageInfo({
						src: url,
						success: function(res) {
							resolve({
								width: res.width,
								height: res.height,
							});
						},
						fail: function(err) {
							reject(new Error("getImageSizeByUrl load fail"));
						}
					})
				})
			},


			imgToCanvasNew(obj) {
				return new Promise((resolve, reject) => {
					const cWidth = obj.width > 250 ? 200 : obj.width
					const cHeight = obj.width > 250 ? 100 : obj.height
					const sNum = 5
					const fbottom = obj.height - sNum * 6;
					const halfH = sNum * 4;
					const ctx = uni.createCanvasContext("canvas");
					ctx.clearRect(0, 0, obj.width, obj.height);
					ctx.drawImage(obj.url, 0, 0, obj.width, obj.height);
					const grd = ctx.createLinearGradient(0, 0, cWidth, 0);
					grd.addColorStop(0, "#4169E1");
					grd.addColorStop(1, "#87CEFA");
					ctx.fillStyle = grd;
					ctx.fillRect(sNum * 2, fbottom - halfH * 4, cWidth, halfH * 2);
					ctx.font = "italic normal bold 25px arial";
					ctx.setFillStyle("#FFFFFF");
					ctx.setTextAlign('center')
					ctx.fillText(obj.title, cWidth / 2+sNum*2, fbottom - halfH * 2 - sNum * 2);
					ctx.fillStyle = "rgba(245,245,245,0.5)";
					ctx.fillRect(sNum * 2, fbottom - halfH * 2, cWidth, halfH * 2 + sNum);
					ctx.setTextAlign('start')
					ctx.setFontSize(sNum * 3);
					ctx.setFillStyle("#000000");
					ctx.fillText(this.tKey + obj.time, sNum * 3, fbottom - halfH);
					ctx.fillText(this.addKey + obj.address, sNum * 3, fbottom);
					setTimeout(() => {
						ctx.draw(false, async () => {
							uni.canvasToTempFilePath({
									canvasId: "canvas",
									fileType: "jpg",
									success: (res) => {
										resolve(res.tempFilePath)
									},
									fail: (err) => {
										reject(err);
									},
								},
								true
							);
						});
					}, 1000)
				});
			},
相关推荐
泯泷1 分钟前
JavaScript随机数生成技术实践 | 为什么Math.random不是安全的随机算法?
前端·javascript·安全
八了个戒32 分钟前
「数据可视化 D3系列」入门第八章:动画效果详解(让图表动起来)
开发语言·前端·javascript·数据可视化
八了个戒35 分钟前
「数据可视化 D3系列」入门第九章:交互式操作详解
javascript·信息可视化·数据可视化·d3
拉不动的猪2 小时前
无缝适配 PC 和移动端‌我们要注意哪些点呢
前端·javascript·面试
子燕若水2 小时前
关于UE5的抗锯齿和TAA
数码相机·计算机视觉·ue5
Benson叔2 小时前
uniapp APP端 DOM生成图片保存到相册
uni-app
爱看书的小沐3 小时前
【小沐杂货铺】基于Three.JS绘制卫星轨迹Satellite(GIS 、WebGL、vue、react,提供全部源代码)
javascript·vue.js·webgl·three.js·卫星轨道·地球earth·satellite
王富贵的记录4 小时前
React 函数组件和类组件的区别
前端·javascript·react.js
巴巴_羊4 小时前
React Article模块
javascript·react.js·ecmascript
z_mazin4 小时前
正则表达式在爬虫中的应用:匹配 HTML 和 JSON 的技巧
javascript·爬虫·正则表达式