uniapp知识

1、uniapp选择图片并转为base64再上传

//先下载 image-tools 插件
npm i image-tools --save
//引入刚下载的插件
import { pathToBase64 } from 'image-tools'
methods:{
	chooseImage(){
		return new Promise((resolve,reject)=>{
			uni.chooseImage({
				count:5,
				success(e) {
					resolve(e.tempFilePaths)
				}
			})
		})
	},
	async getBase64(){
		let imgs = await this.chooseImage();
		let base64Arr = []
		for(var i = 0; i < imgs.length; i++){
			let base64 = await pathToBase64(imgs[i]);
			//下面三行注释的代码的意思就是 有些后端写的程序,它不支持jpeg的,所以需要把它转为png
			// if(base64.indexOf('jpeg') != -1){
			// 	base64 = base64.replace('jpeg' , 'png');
			// }
			base64Arr.push(base64)
		}
		console.log(base64Arr);
	}
}

2、uniapp 图片上传加水印

<view style="">
	<canvas :style="{ width: canvasWidth, height: canvasHeight }" canvas-id="firstCanvas" id="firstCanvas"></canvas>
	<img  :src="src" alt="" width="100" height="100">
</view>
#firstCanvas{
	width: 1px;
	height: 1px;
	position: fixed;
	left: 9999999999px;
	top: 99999999999px;
}
chooseImage(){
	return new Promise((resolve,reject)=>{
		uni.chooseImage({
			success(e){
				let path = e['tempFilePaths'][0]
				resolve(path)
			}
		})
	})
},

getImageInfo(path){
	return new Promise((resolve,reject)=>{
		uni.getImageInfo({
			src:path,
			success(res) {
				resolve(res)
			}
		})
	})
},

canvas(e){
	console.log(e , 'eee')
	let that = this;
	const context = uni.createCanvasContext('firstCanvas' )
	this.canvasWidth = `${e.width}px`
	this.canvasHeight = `${e.height}px`
	context.fillRect(0, 0 , this.canvasWidth , this.canvasHeight)
	context.drawImage(e.path, 0, 0 , this.canvasWidth , this.canvasHeight)
	//下面两行是插入logo的
	//let imgurl = '../../static/logo.png'
	//context.drawImage(imgurl , 30 , 50 , 30 , 50)
	context.beginPath();
	context.setFontSize(50);
	context.setFillStyle("rgba(255,0,0,0.4)");
	context.fillText("我是水印", 50 , 50);
	//注意下面的延迟执行,因为有些时候,canvas绘制还没加截完就执行了。
	//所以,这儿需要一个延迟,不然,绘制出来的图片有可能会是空白
	//下面的很多个水印的也是一样的道理
	setTimeout(()=>{
		context.draw(false , ()=>{
			uni.canvasToTempFilePath({
				canvasId:'firstCanvas',
				success:(ea)=>{
					console.log(ea , 'ea')
					let tempFilePath = ea.tempFilePath;
					let r = base64ToPath(tempFilePath)
					console.log(r)
					that.src = tempFilePath;
				}
			})
		})
	} , 500)
},


//下面这段是有很多个很多个水印的
canvas(e){
	console.log(e , 'eee')
	let that = this;
	const context = uni.createCanvasContext('firstCanvas' )
	this.canvasWidth = `${e.width}px`
	this.canvasHeight = `${e.height}px`
	context.fillRect(0, 0 , this.canvasWidth , this.canvasHeight)
	context.drawImage(e.path, 0, 0 , this.canvasWidth , this.canvasHeight)
	// let imgurl = '../../static/logo.png'
	// context.drawImage(imgurl , 30 , 50 , 30 , 50)
	context.rotate(45 * Math.PI / 180);
	for (let i = 0; i <= 50; i++) {
		context.setFontSize(50);
		context.setFillStyle("rgba(255,0,0,0.4)");
		context.fillText("我是水印", 0, i * 170);
		for (let j = 0; j <= 50; j++) {
			context.beginPath();
			context.setFontSize(50);
			context.setFillStyle("rgba(255,0,0,0.4)");
			context.fillText("我是水印", j * 340, i * 170);
		}
	}
	for (let i = 0; i < 50; i++) {
		context.beginPath();
		context.setFontSize(50);
		context.setFillStyle("rgba(255,0,0,0.4)");
		context.fillText("我是水印", 0, -170 * i);
		for (let j = 1; j < 50; j++) {
			context.beginPath();
			context.setFontSize(50);
			context.setFillStyle("rgba(255,0,0,0.4)");
			context.fillText("我是水印", 340 * j, -170 * i);
		}
	}
	context.rotate(-45 * Math.PI / 180);
	setTimeout(()=>{
		context.draw(false , ()=>{
			uni.canvasToTempFilePath({
				canvasId:'firstCanvas',
				success:(ea)=>{
					console.log(ea , 'ea')
					let tempFilePath = ea.tempFilePath;
					let r = base64ToPath(tempFilePath)
					console.log(r)
					that.src = tempFilePath;
				}
			})
		})
	} , 500)
},



afterReada(e){
	this.chooseImage()
	.then(res=>{
		return this.getImageInfo(res);
	})
	.then(res=>{
		return this.canvas(res);
		//console.log(res , 'res')
	})
	.catch(err=>{
		console.error(err)
	})
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值