Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin forms: How to take picture?

In my xamarin forms application, I have a page in which there is a small camera view and I want to take picture from that camera view.

If I want to navigate to new page completely and take picture, I know it can be done by using a plugin like this

await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
        {
            Directory = "Test",
            SaveToAlbum = true,
            CompressionQuality = 75,
            CustomPhotoSize = 50,
            PhotoSize = PhotoSize.MaxWidthHeight,
            MaxWidthHeight = 2000,
            DefaultCamera = CameraDevice.Front
        });

Can anyone advice how I need to approach if I want to take a picture from the camera on my existing page without navigating to new page?

like image 856
TheDeveloper Avatar asked Dec 23 '22 06:12

TheDeveloper


1 Answers

To capture the image you must return it to a variable like this:

var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
    Directory = "Test",
    SaveToAlbum = true,
    CompressionQuality = 75,
    CustomPhotoSize = 50,
    PhotoSize = PhotoSize.MaxWidthHeight,
    MaxWidthHeight = 2000,
    DefaultCamera = CameraDevice.Front
});

Then you can mount your image on an image controller on your view like this:

image1.Source = file.Path;
like image 191
Jorge L Hernandez Avatar answered Jan 11 '23 20:01

Jorge L Hernandez