Android 通過反射 打開閃光燈

Android如何打開閃光燈

在android中打開閃光燈的方法有兩種,一種是獲取硬件服務,通過反射的方式來操作閃光燈。另外一種是獲得Camera對象,通過設置Camera的參數來操作閃光燈。一下是一個操作閃光燈的工具類:實現了兩種方式操作閃光燈。通過switchFlashlight方法是通過反射的方式操作,通過turnLightOn,turnLightOff方法操作是通過設置Camera來操作閃關燈的。通過反射的方法貌似在4.0以上的版本中都不好用了,建議使用設置攝像頭參數的方式來操作。


[java] view plaincopy
  1. public class FlashlightManager {  
  2.     private static final String TAG = FlashlightManager.class.getSimpleName();  
  3.     private static final Object iHardwareService;  
  4.     private static final Method setFlashEnabledMethod;  
  5.     static {  
  6.         iHardwareService = getHardwareService();  
  7.         setFlashEnabledMethod = getSetFlashEnabledMethod(iHardwareService);  
  8.         if (iHardwareService == null) {  
  9.             Log.v(TAG, "This device does supports control of a flashlight");  
  10.         } else {  
  11.             Log.v(TAG, "This device does not support control of a flashlight");  
  12.         }  
  13.     }  
  14.     private FlashlightManager() {  
  15.     }  
  16.     private static Object getHardwareService() {  
  17.         Class<?> serviceManagerClass = maybeForName("android.os.ServiceManager");  
  18.         if (serviceManagerClass == null) {  
  19.             return null;  
  20.         }  
  21.         Method getServiceMethod = maybeGetMethod(serviceManagerClass,  
  22.                 "getService", String.class);  
  23.         if (getServiceMethod == null) {  
  24.             return null;  
  25.         }  
  26.         Object hardwareService = invoke(getServiceMethod, null"hardware");  
  27.         if (hardwareService == null) {  
  28.             return null;  
  29.         }  
  30.         Class<?> iHardwareServiceStubClass = maybeForName("android.os.IHardwareService$Stub");  
  31.         if (iHardwareServiceStubClass == null) {  
  32.             return null;  
  33.         }  
  34.         Method asInterfaceMethod = maybeGetMethod(iHardwareServiceStubClass,  
  35.                 "asInterface", IBinder.class);  
  36.         if (asInterfaceMethod == null) {  
  37.             return null;  
  38.         }  
  39.         return invoke(asInterfaceMethod, null, hardwareService);  
  40.     }  
  41.     private static Method getSetFlashEnabledMethod(Object iHardwareService) {  
  42.         if (iHardwareService == null) {  
  43.             return null;  
  44.         }  
  45.         Class<?> proxyClass = iHardwareService.getClass();  
  46.         return maybeGetMethod(proxyClass, "setFlashlightEnabled"boolean.class);  
  47.     }  
  48.     private static Class<?> maybeForName(String name) {  
  49.         try {  
  50.             return Class.forName(name);  
  51.         } catch (ClassNotFoundException cnfe) {  
  52.             // OK  
  53.             return null;  
  54.         } catch (Exception re) {  
  55.             re.printStackTrace();  
  56.             Log.w(TAG, "Unexpected error while finding class " + name, re);  
  57.             return null;  
  58.         }  
  59.     }  
  60.     /** 
  61.      * 通過設置Camera打開閃光燈 
  62.      * @param mCamera 
  63.      */  
  64.     public static void turnLightOn(Camera mCamera) {  
  65.         if (mCamera == null) {  
  66.             return;  
  67.         }  
  68.         Parameters parameters = mCamera.getParameters();  
  69.         if (parameters == null) {  
  70.             return;  
  71.         }  
  72.     List<String> flashModes = parameters.getSupportedFlashModes();  
  73.         // Check if camera flash exists  
  74.         if (flashModes == null) {  
  75.             // Use the screen as a flashlight (next best thing)  
  76.             return;  
  77.         }  
  78.         String flashMode = parameters.getFlashMode();  
  79.         Log.i(TAG, "Flash mode: " + flashMode);  
  80.         Log.i(TAG, "Flash modes: " + flashModes);  
  81.         if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) {  
  82.             // Turn on the flash  
  83.             if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {  
  84.                 parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);  
  85.                 mCamera.setParameters(parameters);  
  86.             } else {  
  87.             }  
  88.         }  
  89.     }  
  90.     /** 
  91.      * 通過設置Camera關閉閃光燈 
  92.      * @param mCamera 
  93.      */  
  94.     public static void turnLightOff(Camera mCamera) {  
  95.         if (mCamera == null) {  
  96.             return;  
  97.         }  
  98.         Parameters parameters = mCamera.getParameters();  
  99.         if (parameters == null) {  
  100.             return;  
  101.         }  
  102.         List<String> flashModes = parameters.getSupportedFlashModes();  
  103.         String flashMode = parameters.getFlashMode();  
  104.         // Check if camera flash exists  
  105.         if (flashModes == null) {  
  106.             return;  
  107.         }  
  108.         Log.i(TAG, "Flash mode: " + flashMode);  
  109.         Log.i(TAG, "Flash modes: " + flashModes);  
  110.         if (!Parameters.FLASH_MODE_OFF.equals(flashMode)) {  
  111.             // Turn off the flash  
  112.             if (flashModes.contains(Parameters.FLASH_MODE_OFF)) {  
  113.                 parameters.setFlashMode(Parameters.FLASH_MODE_OFF);  
  114.                 mCamera.setParameters(parameters);  
  115.             } else {  
  116.                 Log.e(TAG, "FLASH_MODE_OFF not supported");  
  117.             }  
  118.         }  
  119.     }  
  120.     private static Method maybeGetMethod(Class<?> clazz, String name,  
  121.             Class<?>... argClasses) {  
  122.         try {  
  123.             return clazz.getMethod(name, argClasses);  
  124.         } catch (Exception nsme) {  
  125.             nsme.printStackTrace();  
  126.             // OK  
  127.             return null;  
  128.         }  
  129.     }  
  130.     private static Object invoke(Method method, Object instance, Object... args) {  
  131.         try {  
  132.             return method.invoke(instance, args);  
  133.         } catch (Exception e) {  
  134.             Log.w(TAG, "Unexpected error while invoking " + method, e);  
  135.             return null;  
  136.         }  
  137.     }  
  138.     /** 
  139.      * 通過反射來操作閃光燈 
  140.      * @param active 
  141.      */  
  142.     public static void switchFlashlight(boolean active) {  
  143.         setFlashlight(active);  
  144.     }  
  145.     static void disableFlashlight() {  
  146.         setFlashlight(false);  
  147.     }  
  148.     private static void setFlashlight(boolean active) {  
  149.         if (iHardwareService != null) {  
  150.             invoke(setFlashEnabledMethod, iHardwareService, active);  
  151.         }  
  152.     }  
  153. }  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章