安卓:检测亮度(光量)在使用相机手机的周围环境?亮度、周围环境、相机、手机

2023-09-05 08:32:11 作者:青春就应该放肆

以下内容适用于Android操作系统。

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

在此先感谢。

推荐答案

下面是你如何注册一个监听器上的光线感应器:

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
    sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    lightSensor = sensorManager.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.
    sensorManager.registerListener(
            listener, lightSensor, SensorManager.SENSOR_DELAY_UI);
}

编辑:感谢@AntiMatter的建议更新

Thanks to @AntiMatter for the suggested updates.

文档: SensorEventListener 的SensorManager SensorEvent 传感器

Docs: SensorEventListener SensorManager SensorEvent Sensor