简体   繁体   English

如何使用 plugin.media for xamarin.forms 正确调整照片大小?

[英]How to correctly resize photo using plugin.media for xamarin.forms?

I'm trying to use xamarin.forms plugin.media plugin to take a photo and save it at a specific size.我正在尝试使用 xamarin.forms plugin.media 插件拍摄照片并将其保存为特定尺寸。 Here's my code:这是我的代码:

private async void ButtonPhoto_OnClicked(object sender, EventArgs e)
{
    if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported)
    {
        var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions
        {
            PhotoSize = PhotoSize.Custom,
            MaxWidthHeight = 250,
            Directory = "MyFolder",
            Name = _item.Id + ".jpg",
            SaveToAlbum = true
        };

        var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions);
    }
}

The actual saving of the photo to the desired folder works, however, the resizing options are being ignored.将照片实际保存到所需文件夹是有效的,但是,调整大小选项被忽略。 I want the picture width to be set at no more than 250 pixels.我希望将图片宽度设置为不超过 250 像素。 Can anyone spot where I'm going wrong?谁能发现我哪里出错了?

edit 1:编辑1:

I also tried the following, but still no joy:我也尝试了以下方法,但仍然没有快乐:

var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
    PhotoSize = PhotoSize.MaxWidthHeight,
    MaxWidthHeight = 250,
    Directory = "MyFolder",
    Name = _item.Id + ".jpg",
    SaveToAlbum = true
};

edit 2:编辑2:

Tried the following using RotateImage as suggested, but still no joy:按照建议使用 RotateImage 尝试了以下操作,但仍然没有乐趣:

var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
    PhotoSize = PhotoSize.Custom,
    RotateImage = true,
    CustomPhotoSize = 5,
    Directory = "MyFolder",
    Name = _item.Id + ".jpg",
    SaveToAlbum = true
};

edit 3:编辑3:

Tried setting RotateImage with MaxWidthHeight, but still not working:尝试使用 MaxWidthHeight 设置 RotateImage,但仍然无法正常工作:

var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
    PhotoSize = PhotoSize.MaxWidthHeight,
    MaxWidthHeight = 150,
    RotateImage = true,
    Directory = "MyFolder",
    Name = _item.Id + ".jpg",
    SaveToAlbum = true,
    CompressionQuality = 75
};

The SaveToAlbum option always stores the original photo in full resolution locally. SaveToAlbum 选项始终在本地以全分辨率存储原始照片。

The image resize is only applied in the returned MediaFile.GetStream() data.图像大小调整仅适用于返回的 MediaFile.GetStream() 数据。

So you could deactivate SaveToAlbum and store it manually using the provided data stream.因此,您可以停用 SaveToAlbum 并使用提供的数据流手动存储它。

In my case I had to send the image over a network connection.就我而言,我必须通过网络连接发送图像。 The behaviour full size on local storage and resized in app stream was handy in my case.在我的情况下,本地存储上的完整大小和在应用程序流中调整大小的行为很方便。

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

相关问题 使用Xamarin Forms nuget Xam.Plugin.Media用相机拍照 - Taking a photo with camera using Xamarin Forms nuget Xam.Plugin.Media 如何使用Xamarin Forms将照片上传到Web服务 - How to upload a photo to a webservice using Xamarin Forms VS Xamarin.Forms (Android):相机和存储访问被 Xam.Plugin.Media 拒绝 - VS Xamarin.Forms (Android): Camera and Storage access denied with Xam.Plugin.Media Xamarin.forms-Xam.Plugin.Media投掷错误此组件的可移植版本中未实现此功能 - Xamarin.forms - Xam.Plugin.Media throwing error This functionality is not implemented in the portable version of this assembly 在 xamarin.forms 中拍照后手机挂起 - Phone Hangs after taking Photo in xamarin.forms Xamarin.Forms:从图库中选择照片后导航错误 - Xamarin.Forms: Error Navigation after picking photo from gallery 使用Xamarin.Forms上的AppCompat在API 22中调整调整大小不起作用 - Adjust resize does not work in API 22 with AppCompat on Xamarin.Forms Xamarin Forms - 媒体插件 - 手机上的空缩略图 - Xamarin Forms - Media Plugin - Empty thumbnails on phone 工具栏在使用 Xamarin.Forms 的 Android 上不起作用 - Toolbar not working on Android using Xamarin.Forms 如何在Android的Xamarin.forms上正确使用活动指示器? - How can I use correctly the activity indicator on Xamarin.forms for Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM