java Android:使用相机检测手机周围的亮度(光量)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4738294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: detect brightness (amount of light) in phone's surroundings using the camera?
提问by Tom
The following applies to the Android operating system.
以下内容适用于 Android 操作系统。
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")?
我的问题很简单:我如何使用相机(后置相机的前部)来获得这种亮度(“光量”)?
Thanks in advance.
提前致谢。
回答by Kevin Coppock
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);
}
EDIT:Thanks to @AntiMatter for the suggested updates.
编辑:感谢@AntiMatter 提供的更新建议。
回答by Idistic
If you wanted to use the camera, for instance if the other sensors were not available then I would
如果您想使用相机,例如如果其他传感器不可用,那么我会
- Set the camera's auto exposure to off, and set exposure compensation to 0
- Set capture to lowest quality / size (why bother with more)
- Make the preview window as small as possible (maybe you could even set it to invisible and still get a frame to analyze, not sure)
- Get an image preview frame, but those are in yuv format so 2.1 and earlier would need an a translator.
- 将相机的自动曝光设置为关闭,并将曝光补偿设置为0
- 将捕获设置为最低质量/大小(为什么要麻烦更多)
- 使预览窗口尽可能小(也许您甚至可以将其设置为不可见,但仍然可以得到一个框架进行分析,不确定)
- 获取图像预览帧,但这些帧采用 yuv 格式,因此 2.1 及更早版本需要翻译器。
Then I would compute the histogram for the images luminosity and work right (brightest) to left (dimmest) until you found the first significant value higher than N, which would determine your scene brightness.
然后我将计算图像亮度的直方图并从右(最亮)到左(最暗)工作,直到您找到高于 N 的第一个显着值,这将确定您的场景亮度。
Having said all of that I would not do it since you would need to test for a lot of outlier situations.
说了这么多,我不会这样做,因为您需要测试很多异常情况。
Course not sure what you use case is, if it's a situation where you are using it to adjust "brightness" in a car, then forward facing camera would be effected by headlights etc.
当然不确定你的用例是什么,如果你用它来调整汽车的“亮度”,那么前向摄像头会受到大灯等的影响。
Anyway interesting problem but seems like a lot of work for such little gain, especially with most decent pieces of hardware having those sensors
无论如何有趣的问题,但似乎需要做很多工作才能获得如此微薄的收益,尤其是在大多数体面的硬件都具有这些传感器的情况下
回答by KevinDTimm
My response may be too incomplete for an answer, but I like the formatting here.
我的回答可能太不完整了,但我喜欢这里的格式。
Additionally, my answer is not facetious, so don't stop reading right away.
另外,我的回答不是开玩笑的,所以不要马上停止阅读。
Of course, first you'll have to take a picture. Then you'll have to define 'brightness'. I don't have the algorithm to do so, but I'm guessing something like that has already been done. Maybe something like :
当然,首先你得拍照。然后你必须定义“亮度”。我没有这样做的算法,但我猜已经完成了这样的事情。也许是这样的:
Determine which pixel values are bright, which are dark and which are in between (and each of them can have a variable amount of brightness). You'll then have to apply that algorithm to your photograph to get a brightness value.
确定哪些像素值是亮的,哪些是暗的,哪些介于两者之间(并且它们中的每一个都可以具有可变的亮度)。然后,您必须将该算法应用于您的照片以获得亮度值。
Yes, this is a very short (35,000 foot) view of the problem, but it should give you some ideas about where to start.
是的,这是对问题的一个非常短的(35,000 英尺)视图,但它应该可以让您了解从哪里开始。
[edit]
Hereis a place to start (the android documentation for sensors)
[/edit]
[编辑]
这是一个开始的地方(传感器的 android 文档)
[/edit]
回答by jarnbjo
Using the camera is pointless, since the camera will adjust the exposure time and aperture to the surrounding light, so that the recorded images will have very similar overall brightness independent of the actual available light (at least in an ideal world).
使用相机是没有意义的,因为相机会根据周围的光线调整曝光时间和光圈,因此记录的图像将具有非常相似的整体亮度,而不受实际可用光线的影响(至少在理想世界中)。
The Android API offer functionality in the SensorManager to access the hardware sensors (you are interested in the sensor of type TYPE_LIGHT). I am not sure however, if you can rely on access to these sensors through the API, although they may (or may not) be present.
Android API 在 SensorManager 中提供访问硬件传感器的功能(您对 TYPE_LIGHT 类型的传感器感兴趣)。但是,我不确定您是否可以依赖通过 API 访问这些传感器,尽管它们可能(也可能不)存在。
回答by Nick
The big deal at wanting to nail the brightness down is to figure out how HTC is doing it in their camera.apk . Because the stock Android Camera app is horrible for overexposure especially for the DINC.
想要降低亮度最重要的是弄清楚 HTC 在他们的 camera.apk 中是如何做到的。因为股票的 Android 相机应用程序对于过度曝光来说是可怕的,尤其是对于 DINC。