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

家住魔仙堡 提交于 2020-01-09 03:21:46

问题


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")?

Thanks in advance.


回答1:


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.

Docs: SensorEventListener SensorManager SensorEvent Sensor




回答2:


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
  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.

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.

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




回答3:


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.

[edit]
Here is a place to start (the android documentation for sensors)
[/edit]




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/4738294/android-detect-brightness-amount-of-light-in-phones-surroundings-using-the-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!