[Android Camera] The basic process of Android camera development

[Android Camera] Light sensor recognizes ambient light intensity

For the development process of Android camera, you can see the previous article, 

 [Android Camera] The basic process of Android camera development

https://blog.csdn.net/bluewindtalker/article/details/54563910

As we all know, if an Android phone wants to identify the current ambient brightness, most of it can be identified by its own light sensor. Of course, the environmental sensor includes a number of data including ambient temperature, brightness, ambient pressure, ambient humidity, and device temperature.


Below we only explain and analyze the light, the following code is the tool class of the light sensor

[java]  view plain copy  
  1. package com.bluewindtalker.camera.demo;  
  2.   
  3. import android.content.Context;  
  4. import android.hardware.Sensor;  
  5. import android.hardware.SensorEvent;  
  6. import android.hardware.SensorEventListener;  
  7. import android.hardware.SensorManager;  
  8.   
  9. /** 
  10.  * @author bluewindtalker 
  11.  * @description light sensor tool 
  12.  * @date 4/15/2018 - 12:08pm 
  13.  */  
  14. publicfinalclass LightSensorUtil {    
  15.   
  16.     private LightSensorUtil() {  
  17.     }  
  18.   
  19.     publicstatic SensorManager getSenosrManager(Context context){   
  20.         return  (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);  
  21.   
  22.     }  
  23.   
  24.     /** 
  25.      * Register light sensor listener 
  26.      * @param sensorManager 
  27.      * @param listener 
  28.      */  
  29.     publicstaticvoid registerLightSensor(SensorManager sensorManager,SensorEventListener listener) {    
  30.         if(sensorManager == null || listener == null){  
  31.             return;  
  32.         }  
  33.         Sensor lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);  // Get the light sensor  
  34.         if (lightSensor != null) { // 光线传感器存在时  
  35.             sensorManager.registerListener(listener, lightSensor, SensorManager.SENSOR_DELAY_NORMAL); // 注册事件监听  
  36.         }  
  37.     }  
  38.     /** 
  39.      * 反注册光线传感器监听器 
  40.      * @param sensorManager 
  41.      * @param listener 
  42.      */  
  43.     public static void unregisterLightSensor(SensorManager sensorManager,SensorEventListener listener) {  
  44.         if(sensorManager == null || listener == null){  
  45.             return;  
  46.         }  
  47.         sensorManager.unregisterListener(listener);  
  48.     }  
  49. }  

 然后我们看下监听器的实现细节。

[java]  view plain  copy
  1. private SensorEventListener lightSensorListener = new SensorEventListener() {  
  2.         @Override  
  3.         public void onSensorChanged(SensorEvent event) {  
  4.             if (event.sensor.getType() == Sensor.TYPE_LIGHT) {  
  5.                 //光线强度  
  6.                 float lux = event.values[0];  
  7.                 Log.e(TAG, "光线传感器得到的光线强度-->" + lux);  
  8.             }  
  9.         }  
  10.   
  11.         @Override  
  12.         public void onAccuracyChanged(Sensor sensor, int accuracy) {  
  13.         }  
  14.     };  

这里我们要注意下,谷歌官方提到

最好在onResume注册和onPause里面反注册传感器,为了防止高耗能。

 这里我们直接在onSensorChanged方法里进行触发回调即可,但是这个回调是当光线亮度发生变化的时候才会触发回调,不会定时回调的,同时大部分光线传感器都设置安装在手机的正面,导致无法使用手机背面的摄像头捕捉亮度,于是乎我们不得不再次通过技术的方式来从摄像头来识别周围环境光强度。请看下篇文章。

[Android相机]通过手机摄像头识别环境亮度

本文demo在 https://github.com/bluewindtalker/camerademo

The official Google demo involved is https://developer.android.com/guide/topics/sensors/sensors_environment.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325541883&siteId=291194637