Enabling Camera Flash While Recording Video

前端 未结 4 506
遇见更好的自我
遇见更好的自我 2021-02-05 02:46

I need a way to control the camera flash on an Android device while it is recording video. I\'m making a strobe light app, and taking videos with a flashing strobe light would r

4条回答
  •  灰色年华
    2021-02-05 03:17

    To access the device camera, you must declare the CAMERA permission in your Android Manifest. Also be sure to include the manifest element to declare camera features used by your application. For example, if you use the camera and auto-focus feature, your Manifest should include the following:

     
     
     
    

    A sample that checks for torch support might look something like this:

    //Create camera and parameter objects
    private Camera mCamera;
    private Camera.Parameters mParameters;
    private boolean mbTorchEnabled = false;
    
    //... later in a click handler or other location, assuming that the mCamera object has already been instantiated with Camera.open()
    mParameters = mCamera.getParameters();
    
    //Get supported flash modes
    List flashModes = mParameters.getSupportedFlashModes ();
    
    //Make sure that torch mode is supported
    //EDIT - wrong and dangerous to check for torch support this way
    //if(flashModes != null && flashModes.contains("torch")){
    if(flashModes != null && flashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)){
        if(mbTorchEnabled){
            //Set the flash parameter to off
            mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        }
        else{
            //Set the flash parameter to use the torch
            mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        }
    
        //Commit the camera parameters
        mCamera.setParameters(mParameters);
    
        mbTorchEnabled = !mbTorchEnabled;
    }
    

    To turn the torch on, you simply set the camera parameter Camera.Parameters.FLASH_MODE_TORCH

    Camera mCamera;
    Camera.Parameters mParameters;
    
    //Get a reference to the camera/parameters
    mCamera = Camera.open();
    mParameters = mCamera.getParameters();
    
    //Set the torch parameter
    mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    
    //Comit camera parameters
    mCamera.setParameters(mParameters);
    

    To turn the torch off, set Camera.Parameters.FLASH_MODE_OFF

提交回复
热议问题