Xamarin Forms Camera2 Simple Auto Flash mode

vasilytserejvasilytserej USMember ✭✭
edited February 12 in Xamarin.Forms

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

it was a mix of this

https://github.com/UNIT-23/Xam-Android-Camera2-Sample

with this

https://developer.xamarin.com/samples/monodroid/android5.0/Camera2Basic/

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.AddTarget(_imageReader.Surface);

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

        _previewSession.StopRepeating();
        _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);
        _previewBuilder.AddTarget(surface);

        List<Surface> surfaces = new List<Surface>();
        surfaces.Add(surface);
        surfaces.Add(_imageReader.Surface);


        CameraDevice.CreateCaptureSession(surfaces,
            new CameraCaptureStateListener
            {
                OnConfigureFailedAction = session =>
                {
                },
                OnConfiguredAction = session =>
                {
                    _previewSession = session;
                    UpdatePreview();
                }
            },
            _backgroundHandler);
    }

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

Answers

  • 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.