TheDeveloper
TheDeveloper

Reputation: 1217

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?

Upvotes: 5

Views: 10635

Answers (2)

Jorge L Hernandez
Jorge L Hernandez

Reputation: 939

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;

Upvotes: 6

GiampaoloGabba
GiampaoloGabba

Reputation: 1458

There is a full example of camera view (with custom renderers) available in the microsoft docs: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/view

In the article, you will learn 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.

However, you will need to add the code needed to take the picture. For that, you can get inspiration from the custom renderers of this app: https://github.com/pierceboggan/Moments

Upvotes: 3

Related Questions