问题描述
以下适用于 Android 操作系统.
The following applies to the Android operating system.
我正在尝试使用摄像头估计手机所在房间的黑暗(或光亮).
I am trying to estimate how dark (or light) it is in the room where the phone is located using the camera.
这个想法是相机可以返回一定的亮度水平,我可以用它来确定手机周围的光量.
The idea is that the camera can return a certain brightness level, which I can use to determine the amount of light in the surroundings of the phone.
我的问题很简单:如何使用摄像头(无论是前置摄像头还是后置摄像头)来获得这样的亮度(光量")?
My question is simple: how do I use the camera (either the front of back camera) to get this amount of brightness (the "amount of light")?
提前致谢.
推荐答案
以下是在光传感器上注册监听器的方法:
Here is how you register a listener on the light sensor:
private final SensorManager mSensorManager;
private final Sensor mLightSensor;
private float mLightQuantity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Obtain references to the SensorManager and the Light Sensor
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
// Implement a listener to receive updates
SensorEventListener listener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
mLightQuantity = event.values[0];
}
}
// Register the listener with the light sensor -- choosing
// one of the SensorManager.SENSOR_DELAY_* constants.
mSensorManager.registerListener(
listener, lightSensor, SensorManager.SENSOR_DELAY_UI);
}
感谢@AntiMatter 提出的更新建议.
Thanks to @AntiMatter for the suggested updates.
文档:SensorEventListener
SensorManager
SensorEvent
传感器
这篇关于Android:使用相机检测手机周围环境的亮度(光量)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!