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:使用相机检测手机周围环境的亮度(光量)
基础教程推荐
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01