Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android test if front camera supports flash

I know is possible to detect if camera has flash integrated, using a method like this:

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

but if the device has 2 cameras how can I test for each of them if has flash available?

For example on a Samsung S2 device, on native camera application when using the front camera the flash button is disabled, meaning is not available.

Thanks.

like image 791
Paul Avatar asked May 06 '13 14:05

Paul


2 Answers

Paul's answer didn't work for me.

The front camera on a Galaxy Nexus has a valid flash-mode of FLASH_MODE_OFF, but it is the only supported option. This method will work in all situations:

    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;
    }

If your app supports more than just FLASH_MODE_OFF and FLASH_MODE_ON, you'll need to tweak the if-check inside the loop.

-- Update --

Also you can add torch for flash in if condition if you really need to use torch.

    if (Camera.Parameters.FLASH_MODE_ON.equals(flashMode) || Camera.Parameters.FLASH_MODE_TORCH.equals(flashMode)) {
like image 191
Jason Robinson Avatar answered Oct 17 '22 07:10

Jason Robinson


I figured this by myself and I post here the solution, which is actually very simple:

/**
 * 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;
}

The above method is different by this one:

/**
 * 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);
}
like image 8
Paul Avatar answered Oct 17 '22 06:10

Paul