問題描述
以下適用于 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:使用相機檢測手機周圍環境的亮度(光量)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!