Xamarin Forms Camera2 Simple Auto Flash mode

vasilytserejvasilytserej USMember ✭✭
edited February 12 in Xamarin.Forms

I managed to have a Camera2 view on my Xamarin Forms Application

it was a mix of this


with this


If you want the code just let me know.

The problem is that I fail to activate the flash in auto mode.
in the examples the camera goes through several states before taking a picture
and depending on the stage It sets up the according configuration and also the flash mode

My question is

is there any way to set up the flash more easy

this is my code

    public void TakePhoto()
        if (_context == null || CameraDevice == null) return;

        _captureBuilder = CameraDevice.CreateCaptureRequest(CameraTemplate.StillCapture);

        _captureBuilder.Set(CaptureRequest.ControlAeMode, (int)ControlAEMode.OnAutoFlash);
        _captureBuilder.Set(CaptureRequest.FlashMode, (int)FlashMode.Off);

        _previewSession.Capture(_captureBuilder.Build(), _cameraCaptureListener, _backgroundHandler);

    public void StartPreview()
        if (CameraDevice == null || !_cameraTexture.IsAvailable || _previewSize == null) return;

        var texture = _cameraTexture.SurfaceTexture;

        texture.SetDefaultBufferSize(_previewSize.Width, _previewSize.Height);

        var surface = new Surface(texture);

        _previewBuilder = CameraDevice.CreateCaptureRequest(CameraTemplate.Preview);

        List<Surface> surfaces = new List<Surface>();

            new CameraCaptureStateListener
                OnConfigureFailedAction = session =>
                OnConfiguredAction = session =>
                    _previewSession = session;

    private void UpdatePreview()
        if (CameraDevice == null || _previewSession == null) return;

        _previewBuilder.Set(CaptureRequest.ControlAeMode, (int)ControlAEMode.OnAutoFlash);
        _previewBuilder.Set(CaptureRequest.FlashMode, (int)FlashMode.Off);

        _previewSession.SetRepeatingRequest(_previewBuilder.Build(), null, _backgroundHandler);


  • MicialMicial USMember ✭✭

    The problem is that I fail to activate the flash in auto mode.

    Could you please elaborate a bit more about this?

  • vasilytserejvasilytserej USMember ✭✭

    @Micial Sorry I dint asked correctly
    I found out the issue. I created a camera session and I simply requested a picture to the session.

    in camera2 namespace it is necesary to put the camera through various stages before taking a picture and if you dont go through thos stages the flash is never activated.

    In the code above I just called TakePhoto() from the running preview session

Sign In or Register to comment.