繁体   English   中英

Xamarin.forms中的相机宽度和大小

[英]Camera Width and Size in Xamarin.forms

我正在使用此代码打开相机并拍照。 我要为相机和照片设置固定的宽度和高度。

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

关于什么:

<Image
   WidthRequest="200"
   HeightRequest="200"
   x:Name="PhotoImage" />

使用Plugin.Media,可以通过更改PhotoSize参数来设置图像的照片大小。 可用值是“ Small, Medium, or Large ,分别对应于原始值的25%,50%或75%。

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

您还可以设置自定义百分比:

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

这将允许您调整原始图像的大小。

为了显示它,您还可以更改Image HeightRequest和WidthRequest以适应所需的视图

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM