Android: detect brightness (amount of light) in phone#39;s surroundings using the camera?(Android:使用相机检测手机周围环境的亮度(光量)?)
问题描述
以下适用于 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:使用相机检测手机周围环境的亮度(光量)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android:使用相机检测手机周围环境的亮度(光量)


基础教程推荐
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01