Xamarin.Forms拍照带摄像头显示方向错误和崩溃的后退按钮 [英] Xamarin.Forms Take photo with camera shows wrong orientation and crashes on back button

查看:1977
本文介绍了Xamarin.Forms拍照带摄像头显示方向错误和崩溃的后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用从这里Xamarin.Forms相机样品 - <一个href=\"https://github.com/XForms/Xamarin-Forms-Labs-Samples/tree/master/XF.Labs.CameraSample\">https://github.com/XForms/Xamarin-Forms-Labs-Samples/tree/master/XF.Labs.CameraSample我能够选择或拍照。

 专用异步任务SelectPicture()
{
    mediaPicker = DependencyService.Get&LT; IMediaPicker&GT;();
    ImageSource的= NULL;
        VAR媒体文件=等待mediaPicker.SelectPhotoAsync(新CameraMediaStorageOptions
            {
                DefaultCamera = CameraDevice.Front,
                MaxPixelDimension = 400
            });
        的ImageSource = ImageSource.FromStream(()=&GT; mediaFile.Source);
        img.Source = ImageSource的;}
私人异步任务TakePicture()
{
    mediaPicker = DependencyService.Get&LT; IMediaPicker&GT;();
    ImageSource的= NULL;
        VAR媒体文件=等待mediaPicker.TakePhotoAsync(新CameraMediaStorageOptions
            {
                DefaultCamera = CameraDevice.Front,
                MaxPixelDimension = 400
            });
        的ImageSource = ImageSource.FromStream(()=&GT; mediaFile.Source);
        img.Source = ImageSource的;}

在code的形象简直是

  IMG =新形象
    {
        BackgroundColor中= Color.White,
        看点= Aspect.AspectFit
    };

有几个问题:

第一个。您可以拍摄照片,或者选择一个存储之一,然后它会显示在页面上。如果您选择一张照片正确显示的它,无论是纵向或横向。当你拍照,它只是在横向模式下显示,因此,如果图像的拍摄人像时,图像显示在旁边。这不是灾难性的,但它会更好地显示图像是如何拍摄。

第二个问题是更激烈一点,如果你preSS装置的,当你无论是在相机或图片库,然后变为黑屏,然后你会得到一个消息,说明该应用程序已停止响应后退按钮

我只在Android试过这种为止。有没有人对如何解决上述问题的任何想法?

编辑:我已成功地固定在后退按钮的崩溃,但图像仍显示在其一侧为Android,但正确显示适用于iOS


解决方案

您可以通过使用mediaFile.Exif.Orientation'来检查它是否是纵向还是横向检查方向(左上 - >人像)。然后使用保存前设置照片的方向,

\r
\r

mediaFile.Exif.Orientation = ExifLib.ExifOrientation.TopLeft;

\r

\r
\r

I am using the Xamarin.Forms Camera sample from here - https://github.com/XForms/Xamarin-Forms-Labs-Samples/tree/master/XF.Labs.CameraSample I am able to select or take a photo.

private async Task SelectPicture()
{
    mediaPicker = DependencyService.Get<IMediaPicker>();
    imageSource = null;
        var mediaFile = await mediaPicker.SelectPhotoAsync(new CameraMediaStorageOptions
            {
                DefaultCamera = CameraDevice.Front,
                MaxPixelDimension = 400
            });
        imageSource = ImageSource.FromStream(() => mediaFile.Source);
        img.Source  = imageSource;

}


private async Task TakePicture()
{
    mediaPicker = DependencyService.Get<IMediaPicker>();
    imageSource = null;
        var mediaFile = await mediaPicker.TakePhotoAsync(new CameraMediaStorageOptions
            {
                DefaultCamera = CameraDevice.Front,
                MaxPixelDimension = 400
            });
        imageSource = ImageSource.FromStream(() => mediaFile.Source);
        img.Source  = imageSource;

}

the code for the image is simply

    img = new Image
    {
        BackgroundColor = Color.White,
        Aspect = Aspect.AspectFit
    };    

There are a couple of issues:

First one. You can take a photo or select a stored one and it will then show it on the page. If you select a photo it displays it correctly, either portrait or landscape. When you take a photo, it only displays in landscape mode, so if the image was taken in portrait, the image shows on the side. This isn't catastrophic, but it would be better to show the image how it was taken.

The second issue is a bit more drastic, if you press the device's back button when you are either in the camera or the image gallery then the screen goes blank and then you get a message stating the app has stopped responding.

I have only tried this on Android so far. Does anyone have any ideas on how I can solve the above problems?

EDIT: I have managed to fix the crashing on back button, but the image still displays on its side for Android, but displays correctly for iOS

解决方案

you can check orientation by using 'mediaFile.Exif.Orientation' to check whether it's portrait or Landscape.(' TopLeft'--> Portrait.) And then set photo orientation before saving using,

 mediaFile.Exif.Orientation = ExifLib.ExifOrientation.TopLeft;

这篇关于Xamarin.Forms拍照带摄像头显示方向错误和崩溃的后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆