gpt4 book ai didi

ios - Xamarin.Forms Xam.Plugin.Media 在 iOS 上拍照

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:24:56 24 4
gpt4 key购买 nike

我在我的表单应用程序中使用 Xam.Plugin.Media 来拍照。我获取图像流 (GetStream) 并转换为 byte[] 并存储在我的数据库中。然后我将其用作图像源。在 Android 和 UWP 上,它工作正常。在 iOS 上,如果照片是在纵向模式下拍摄的,一旦选择图像,图像将始终旋转 90 度。稍后我会将其上传到服务器,该图像可以在不同的设备上使用。为此,我还尝试了 GetStreamWithImageRotatedForExternalStorage 但在这种情况下,我根本看不到图像。流中有字节(我 DisplayAlert 的长度)但图像不显示。

知道我可能做错了什么吗?

我的代码:-

private async Task TakePicture(WineDetails details)
{
await CrossMedia.Current.Initialize();

if (CrossMedia.Current.IsCameraAvailable && CrossMedia.Current.IsTakePhotoSupported)
{
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
AllowCropping = true,
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
SaveToAlbum = false,
RotateImage = true
});

if (file == null)
return;

using (var ms = new MemoryStream())
{
var stream = file.GetStreamWithImageRotatedForExternalStorage();

stream.CopyTo(ms);
details.LabelImage = ms.ToArray();

details.NotifyChange("ImageSource");
}
}
}

图像通过 NotifyChange 在页面中更新,看起来像这样:-ImageSource.FromStream(() => new MemoryStream(this.LabelImage))

这在 Android 和 UWP 上的所有情况下都工作正常,在 iOS 上使用 GetStream 工作(除了图像旋转不正确)但在 iOS 上使用 GetStreamWithImageRotatedForExternalStorage 时不起作用。

还有其他人在使用这个插件吗?知道为什么 GetStream 返回旋转的图像吗?知道为什么 GetStreamWithImageRotatedForExternalStorage 不起作用吗?

谢谢

更新:-

更改了 SaveToAlbum = true,当我打开画廊时,图像旋转了 90 度。有 RotateImage = true 这可能会导致问题吗?我会尝试将其设置为 false。我仍然无法使用 GetStreamWithImageRotatedForExternalStorage 将图像源设置为图像的字节数组。

using (var ms = new MemoryStream())
{
file.GetStreamWithImageRotatedForExternalStorage().CopyTo(ms);
details.LabelImage = ms.ToArray();
}

使用图像的字节数组

return ImageSource.FromStream(() => new MemoryStream(this.LabelImage));

这对我不起作用,GetStream 工作正常。

更新:-

好的,RotateImage = false + GetStreamWithImageRotatedForExternalStorage 允许我显示图像,但它在我的应用程序和图库中仍然错误地旋转。

最佳答案

我正在使用 this插件,它是相似的(如果不是同一件事——我知道 James Montemagno 最近将他的作品与 Xamarin 打包/捆绑在一起)。

如果您查看那里的问题板,您会发现有相当多的人有类似的问题(iOS 上的图像旋转)。几乎每个“解决方案”都提到使用 GetStreamWithImageRotatedForExternalStorage

我的问题类似 - 我无法在没有其他(非 iOS 设备)旋转图像的情况下以纵向模式在 iOS 上拍照。我已经尝试了数周来解决这个问题,但对该插件的支持似乎非常有限。


最终我不得不通过一个巨大的解决方法来解决这个问题——使用从FFImageLoading扩展的自定义渲染器。显示我们的图像和MetadataExtractor .然后,我们能够从流中提取 EXIF 数据,并对 FFImageLoding 图像控件应用旋转变换。

旋转信息以一种奇怪的方式存储为字符串。这是我用来提取旋转信息的方法,并将需要旋转的量作为 int 返回。请注意,对我来说,iOS 仍然能够正确显示图像,因此它只返回了 Android 设备的旋转变化。

    public static int GetImageRotationCorrection(byte[] image)
{
try
{
var directories = ImageMetadataReader.ReadMetadata(new MemoryStream(image));
if (Device.Android == "Android")
{
foreach (var directory in directories)
{
foreach (var tag in directory.Tags)
{
if (tag.Name == "Orientation")
{
if (tag.Description == "Top, left side(Horizontal / normal)")
return 0;
else if (tag.Description == "Left side, bottom (Rotate 270 CW)")
return 270;
else if (tag.Description == "Right side, top (Rotate 90 CW")
return 90;
}
}
}
}

return 0;
}
catch (Exception ex)
{
return 0;
}
}

请注意,还有一个用于 FFImage 加载的图像的自定义渲染器。

public class RotatedImage : CachedImage
{
public static BindableProperty MyRotationProperty = BindableProperty.Create(nameof(MyRotation), typeof(int), typeof(RotatedImage), 0, propertyChanged: UpdateRotation);

public int MyRotation
{
get { return (int)GetValue(MyRotationProperty); }
set { SetValue(MyRotationProperty, value); }
}

private static void UpdateRotation(BindableObject bindable, object oldRotation, object newRotation)
{
var _oldRotation = (int)oldRotation;
var _newRotation = (int)newRotation;

if (!_oldRotation.Equals(_newRotation))
{
var view = (RotatedImage)bindable;
var transformations = new System.Collections.Generic.List<ITransformation>() {
new RotateTransformation(_newRotation)
};
view.Transformations = transformations;
}
}
}

因此,在我的 XAML 中 - 我声明了一个 RotatedImage 而不是标准图像。使用自定义渲染器,我能够做到这一点并使图像显示旋转正确的量。

image.MyRotation = GetImageRotationCorrection(imageAsBytes)

这是一个完全不必要的解决方法 - 但这些是我为解决此问题所必须付出的努力。

我肯定会继续回答这个问题,社区中可能有人可以帮助我们!

关于ios - Xamarin.Forms Xam.Plugin.Media 在 iOS 上拍照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51200013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com