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.
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
If you wanted to use the camera, for instance if the other sensors were not available then I would
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
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With