Forum Xamarin.Forms

How to open Front Camera?

Steve1000Steve1000 Member ✭✭✭

Hi - how can i open the front camera using Xamarin Forms?

I've got the following code which seems to default to the rear camera. I'd need to restrict it to the front camera only

var requestPermission = await Permissions.RequestAsync<Permissions.Camera>();

var status = await Xamarin.Essentials.Permissions.CheckStatusAsync<Permissions.Camera>();



if (status != Xamarin.Essentials.PermissionStatus.Granted)
{
    await DisplayAlert("Can't access camera", "Please grant access to Camera to upload a profile picture", "OK");
    return;
}

var photo = await MediaPicker.CapturePhotoAsync(new MediaPickerOptions
{
    Title ="Please take a selfie"
});
Tagged:

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    It seems that Xamarin.Essentials does not yet reveal any interface for setting up front and rear camera.
    For this, you can post a new feature request here: https://github.com/xamarin/Essentials/issues

    Thanks for your support for xamarin.


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • Steve1000Steve1000 Member ✭✭✭

    Is there any other way to open the front camera only through Xamarin forms?

  • jezhjezh Member, Xamarin Team Xamurai

    For this, you can check the official document Implementing a View of Custom Renderers .
    This article demonstrates how to create a custom renderer for a Xamarin.Forms custom control that's used to display a preview video stream from the device's camera.

    And you can check the sample here: https://github.com/xamarin/xamarin-forms-samples/tree/master/CustomRenderers/View

    Here we can change to the front camera in class CameraPreview:

    public class CameraPreview : View
    {
        public static readonly BindableProperty CameraProperty = BindableProperty.Create (
            propertyName: "Camera",
            returnType: typeof(CameraOptions),
            declaringType: typeof(CameraPreview),
            defaultValue: CameraOptions.Rear);
    
        public CameraOptions Camera {
            get { return (CameraOptions)GetValue (CameraProperty); }
            set { SetValue (CameraProperty, value); }
        }
    }
    

    We can change the CameraOptions.Rear to CameraOptions.Front.


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Sign In or Register to comment.