#开发微信小程序中遇到的问题:打开摄像头将图像的亮度等信息显示在界面上,但是不知道为什么亮度的值始终是初值,球求老哥们帮忙解答。
这是JS页面的代码
// index.js
Page({
data: {
brightness: 0 // 用于显示亮度值
},
handleCameraFrame: function (e) {
const frameData = e.detail.data;
const brightness = this.calculateBrightness(frameData);
this.setData({
brightness: brightness.toFixed(2) // 保留两位小数显示亮度值
});
},
calculateBrightness: function (frameData) {
let sum = 0;
for (let i = 0; i < frameData.length; i += 4) {
const r = frameData[i];
const g = frameData[i + 1];
const b = frameData[i + 2];
const brightness = (0.299 * r + 0.587 * g + 0.114 * b) / 255; // 简化后的灰度值
sum += brightness;
}
const averageBrightness = sum / (frameData.length / 4);
return averageBrightness;
}
});
这是wxml页面的代码
<!-- index.wxml -->
<view class="container">
<camera device-position="back" flash="torch" bindframe="handleCameraFrame" style="width: 100%; height: 100%;"></camera>
<view>亮度:{{brightness}}</view>
</view>