uni根据图片获取其主题颜色

<template>
	<view class="content" :style="{background:Bg}">
		<image class="logo" :src="img" id="loadImg"></image>
		<canvas canvas-id="myCanvas" class="canvas"></canvas>
	</view>
</template>

<script>
	var _this;
	export default {
		data() {
			return {
				img: 'https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3880341262,3308316348&fm=26&gp=0.jpg',
				Bg: "", //背景颜色
			}
		},
		onLoad() {
			_this = this;
			_this.$nextTick(function() {
				_this.getImageColor("myCanvas", "loadImg", this.img).then(res => {
					if(res!=''){
						_this.Bg = 'rgba('+res.r+','+res.g+','+res.b+',1)';
					}
				})
			});
		},
		methods: {
			/**
			 * 求图片主题颜色
			 * @param {String} canvasID 画布ID
			 * @param {String} imgID 图片ID
			 * @param {String} imgSrc 图片资源路径
			 * */
			getImageColor:function(canvasID, imgID, imgSrc){
				let imgWidth,
					imgHeight;
				let canvasWidth,
					canvasHeight;
				var canvas = uni.createCanvasContext(canvasID);
				// 获取img的宽度/高度
				var pro = function() {
					return new Promise((resolve, reject) => {
						uni.createSelectorQuery().select("#" + imgID).boundingClientRect(ret => {
							imgWidth = ret.width;
							imgHeight = ret.height;
								
							// 赋值canvas宽度/高度
							canvasWidth = imgWidth;
							canvasHeight = imgHeight;
							canvas.drawImage(imgSrc, 0, 0, canvasWidth, canvasHeight);
							canvas.draw(true, () => {
								// 获取像素数据
								uni.canvasGetImageData({
									// #ifdef MP-WEIXIN
									canvasId: canvas.canvasId,
									// #endif
									// #ifndef MP-WEIXIN
									canvasId: canvas.id,
									// #endif
									x: 0,
									y: 0,
									width: imgWidth,
									height: imgHeight,
									success: (res) => {
										let data = res.data;
										var r = 1,
											g = 1,
											b = 1;
										// 取所有像素的平均值
										for (var row = 0; row < imgHeight; row++) {
											for (var col = 0; col < imgWidth; col++) {
												// console.log(data[((img.width * row) + col) * 4])
												if (row == 0) {
													r += data[((imgWidth * row) + col)];
													g += data[((imgWidth * row) + col) + 1];
													b += data[((imgWidth * row) + col) + 2];
												} else {
													r += data[((imgWidth * row) + col) * 4];
													g += data[((imgWidth * row) + col) * 4 + 1];
													b += data[((imgWidth * row) + col) * 4 + 2];
												}
											}
										}
										// 求取平均值
										r /= (imgWidth * imgHeight);
										g /= (imgWidth * imgHeight);
										b /= (imgWidth * imgHeight);
										// 将最终的值取整
										r = Math.round(r);
										g = Math.round(g);
										b = Math.round(b);
										let obj = {
											r,
											g,
											b
										}
										resolve(obj);
									},
									fail: (fail) => {
										reject(fail);
									}
								});
							});
								
						}).exec();
					}).catch(e => {
						console.log(e)
					})
				}
				return pro();
			}
		}
	}
</script>

<style>
	.content {
		width: 100%;
		height: 500rpx;
		box-sizing: border-box;
		position: relative;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
	}
	.canvas{
		position: absolute;
		height: 200rpx;
		width: 200rpx;
		opacity: 0;
	}
</style>

 

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
uni-app中,可以使用`uni.getImageInfo`方法来获取图片的宽高。可以按照以下步骤来实现: 1. 引入uni-app的API:在页面的js文件中,使用`import`语句引入`uni`对象,以便使用其中的方法。 2. 调用`uni.getImageInfo`方法:在需要获取图片宽高的地方,使用`uni.getImageInfo`方法,并传入图片的路径或临时文件路径作为参数。 3. 处理返回结果:`uni.getImageInfo`方法会返回一个Promise对象,可以使用`then`方法来处理获取到的图片信息。在`then`方法中,可以通过`res.width`和`res.height`来获取图片的宽高。 下面是一个示例代码,展示了如何在uni-app获取图片的宽高: ```javascript import uni from 'uni-app'; uni.getImageInfo({ src: '/static/image.jpg', }).then(res => { const width = res.width; const height = res.height; console.log('图片宽度:', width); console.log('图片高度:', height); }); ``` 通过以上步骤,你可以在uni-app获取图片的宽高信息。<span class="em">1</span> #### 引用[.reference_title] - *1* [uni-app 组件里面获取元素宽高的实现](https://download.csdn.net/download/weixin_38564718/12929837)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值