2013-05-06 142 views
2

我知道是可能的,检测是否相机闪光灯集成使用这样的方法:的Android测试,如果前置摄像头支持闪光灯

/** 
* @return true if a flash is available, false if not 
*/ 
public static boolean isFlashAvailable(Context context) { 
    return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); 
} 

但如果该装置具有2个相机如何测试为它们中的每如果有闪光灯可用?

例如在Samsung S2设备上,在使用前置摄像头时的本机相机应用程序中,闪光灯按钮被禁用,意味着不可用。

谢谢。

回答

11

保罗的回答没有为我工作。 Galaxy Nexus上的前置摄像头的有效闪光模式为FLASH_MODE_OFF,但它是唯一支持的选项。此方法将适用于所有情况:

private boolean hasFlash(){ 
    Parameters params = mCamera.getParameters(); 
    List<String> flashModes = params.getSupportedFlashModes(); 
    if(flashModes == null) { 
     return false; 
    } 

    for(String flashMode : flashModes) { 
     if(Parameters.FLASH_MODE_ON.equals(flashMode)) { 
      return true; 
     } 
    } 

    return false; 
} 

如果您的应用支持不仅仅是FLASH_MODE_OFFFLASH_MODE_ON更多,你需要调整内循环,如果检查。

+1

这非常有趣。不知道可以只有FLASH_MODE_OFF作为选项......的确,这个解决方案似乎比我提出的解决方案更好。感谢您指出了这一点。 – Paul 2013-11-06 23:03:39

7

我自己想通这一点,我张贴在这里的解决方案,这其实很简单:

/** 
* Check if Hardware Device Camera can use Flash 
* @return true if can use flash, false otherwise 
*/ 
public static boolean hasCameraFlash(Camera camera) { 
    Camera.Parameters p = camera.getParameters(); 
    return p.getFlashMode() == null ? false : true; 
} 

上述方法是不同的这一个:

/** 
* Checking availability of flash in device. 
* Obs.: If device has 2 cameras, this method doesn't ensure both cameras can use flash. 
* @return true if a flash is available in device, false if not 
*/ 
public static boolean isFlashAvailable(Context context) { 
    return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); 
}