uni-app获取图片背景色

//获取图片信息
		getImageInfo(path, index) {

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

						var ctx = uni.createCanvasContext('myCanvas', this)



						// 填充背景色,白色
						ctx.drawImage(res.path, 0, 0, 1, 1) // drawImage(图片路径,x,y,绘制图像的宽度,绘制图像的高度)



						ctx.draw(false, (ret) => {

							uni.canvasGetImageData({
								canvasId: 'myCanvas',
								x: 0,
								y: 0,
								width: 0.5,
								height: 0.5,
								success: (res) => {

									let pxArr = res.data
									pxArr = Array.from(pxArr);
									const colorList = {}
									let i = 0;

									while (i < pxArr.length) {
										const r = pxArr[i];
										const g = pxArr[i + 1];
										const b = pxArr[i + 2];
										const a = pxArr[i + 3];
										i = i + 4; // 最后 +4 比每次 i++ 快 10ms 左右性能
										const key = [r, g, b, a].join(',')
										key in colorList ? ++colorList[key] : (
											colorList[key] = 1)
										let info = this.cardList[index]
										info['bgColor'] = key

										this.cardList.splice(index, 1, info)
									}
									resolve()
								},
								fail: (fail) => {
									console.log('未绘制')
									reject(fail)
								}
							})
						})

					}
				})
			})

		}

你可能感兴趣的:(vue,uni-app,前端,javascript)