Hani Gerges
Hani Gerges

Reputation: 1

Camera Width and Size in Xamarin.forms

I am using this code to open the camera and take a photo. What I want to do is set a fixed width and height for the camera and photos.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Image x:Name="PhotoImage" />
    <Button x:Name="CameraButton" Text="Take Photo" Grid.Row="1" />
</Grid>

public MainPage()
{
    InitializeComponent();

    CameraButton.Clicked += CameraButton_Clicked;
}

private async void CameraButton_Clicked(object sender, EventArgs e)
{
    var photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions() { });

    if (photo != null)
        PhotoImage.Source = ImageSource.FromStream(() => { return photo.GetStream(); });
}

Upvotes: 0

Views: 1637

Answers (1)

Bruno Caceiro
Bruno Caceiro

Reputation: 7199

Using Plugin.Media, you can set the image photo size, by changing the PhotoSize param. The available values are Small, Medium, or Large which correspond to 25%, 50%, or 75% or the original.

var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
    PhotoSize = PhotoSize.Medium,
});

You can also set a custom percentage:

var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
    PhotoSize = PhotoSize.Custom,
    CustomPhotoSize = 90 //Resize to 90% of original
});

This will allow you to resize the original image.

In order to display it, you can also change the Image HeightRequest and WidthRequest to adapt to the view you want

 <Image Source="yourImage.png" WidthRequest="72" Aspect="Fill"  />   

Upvotes: 2

Related Questions