简体   繁体   English

Android:使用相机检测手机周围的亮度(光线量)?

[英]Android: detect brightness (amount of light) in phone's surroundings using the camera?

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. 提前致谢。

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提供的建议更新。

Docs: SensorEventListener SensorManager SensorEvent Sensor 文档: SensorEventListener SensorManager SensorEvent Sensor

If you wanted to use the camera, for instance if the other sensors were not available then I would 如果您想使用相机,例如,如果其他传感器不可用,那么我会

  1. Set the camera's auto exposure to off, and set exposure compensation to 0 将相机的自动曝光设定为关闭,并将曝光补偿设定为0
  2. Set capture to lowest quality / size (why bother with more) 将捕获设置为最低质量/尺寸(为什么还需要更多)
  3. 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) 使预览窗口尽可能小(也许你甚至可以将它设置为不可见,仍然得到一个框架来分析,不确定)
  4. Get an image preview frame, but those are in yuv format so 2.1 and earlier would need an a translator. 获取图像预览框架,但这些是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 无论如何有趣的问题,但似乎很多工作,以获得这么小的收益,特别是有大多数体面的硬件有这些传感器

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] [编辑]
Here is a place to start (the android documentation for sensors) 是一个开始的地方(传感器的android文档)
[/edit] [/编辑]

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). Android API提供SensorManager中的功能以访问硬件传感器(您对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. 但是,我不确定您是否可以通过API访问这些传感器,尽管它们可能(或可能不存在)。

The big deal at wanting to nail the brightness down is to figure out how HTC is doing it in their camera.apk . 想要降低亮度的重要一点是弄明白HTC是如何在他们的相机中做到的。 Because the stock Android Camera app is horrible for overexposure especially for the DINC. 因为Android相机应用程序对过度曝光很可怕,特别是对于DINC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Android手机的相机和OpenCV最好地检测不同的LED颜色? - How can I best detect different LED colours using an Android phone's camera and OpenCV? Android Studio:手机预览,只显示屏幕,而不是屏幕周围 - Android Studio: Preview of the phone, only screen is showing, not the surroundings of the screen Android-使用按钮更改屏幕的亮度 - Android - using a button to change the screen's brightness 在opencv中使用Android手机摄像头? - Using android phone camera in opencv? 如何检测android手机是否具有双后置摄像头 - How to detect if android phone has dual back camera 在Android上通过摄像头确定暗/亮 - Determine dark/light by camera on Android Android代码无法访问手机摄像头......出了什么问题? - Android code can't access phone camera…what's wrong? 测量Android相机的光强度 - Measuring Light Intensities from Android Camera 函数隔离照相机的LED灯和java.lang.NullPointerException:尝试调用虚拟方法'void android.hardware.Camera $ Parameters - Function Isolating Camera's LED Light and java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera$Parameters 使用实时图像处理在Android相机中检测激光点的代码 - Code to detect laser dot in android camera using realtime image processing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM