Android测试前置摄像头是否支持闪光灯

问题描述 投票:3回答:2

我知道可以使用如下方法检测相机是否集成了闪光灯:

 /** 
 * @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设备上,使用前置摄像头时,在本机摄像头应用程序上,闪光按钮被禁用,这意味着无法使用。

谢谢。

android android-camera
2个回答
12
投票

保罗的回答对我不起作用。 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,您还需要在循环内调整if-check。


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);
}
© www.soinside.com 2019 - 2024. All rights reserved.