Android test if front camera supports flash

前端 未结 2 1595
感动是毒
感动是毒 2020-12-06 08:29

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         


        
2条回答
  •  一生所求
    2020-12-06 08:44

    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 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)) {
    

提交回复
热议问题