Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 771
Tom Avatar asked Jan 19 '11 17:01

Tom


3 Answers

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

like image 90
Kevin Coppock Avatar answered Sep 30 '22 12:09

Kevin Coppock


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

like image 29
Idistic Avatar answered Sep 30 '22 10:09

Idistic


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]

like image 42
KevinDTimm Avatar answered Sep 30 '22 11:09

KevinDTimm