【问题标题】:Xamarin.Forms Take photo with camera shows wrong orientation and crashes on back buttonXamarin.Forms 用相机拍照显示方向错误并在后退按钮上崩溃
【发布时间】:2014-12-05 14:51:16
【问题描述】:

我正在使用此处的 Xamarin.Forms 相机示例 - https://github.com/XForms/Xamarin-Forms-Labs-Samples/tree/master/XF.Labs.CameraSample 我可以选择或拍照。

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;

}

图片的代码很简单

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

有几个问题:

第一个。您可以拍照或选择存储的照片,然后它将显示在页面上。如果您选择一张照片,它会正确显示它,无论是纵向还是横向。当您拍照时,它仅以横向模式显示,因此如果图像是纵向拍摄的,则图像显示在侧面。这不是灾难性的,但最好显示图像是如何拍摄的。

第二个问题有点严重,如果您在相机或图片库中按下设备的后退按钮,那么屏幕会变为空白,然后您会收到一条消息,指出应用程序已停止响应。

到目前为止,我只在 Android 上尝试过这个。有人对我如何解决上述问题有任何想法吗?

编辑:我已经设法修复了后退按钮崩溃的问题,但是对于 Android,图像仍然显示在其一侧,但对于 iOS 则正确显示

【问题讨论】:

  • 有人解决了这个问题吗?
  • 我没有机会尝试这些解决方案,但这似乎只是三星设备上的问题。在我测试过的其他设备上运行良好

标签: android camera xamarin.forms


【解决方案1】:

我敢猜测这里有几个问题。 Android 上的 Xamarin.Forms.Labs 依赖注入处理程序之一是 1) 不检查需要的旋转,2) 不检查外部存储或不处理 onActivityCancelled。

解决您的问题的简单方法是使用 Xamarin.Mobile Xamarin.Mobile 我不能 100% 确认它会处理所有事情,但如果这样做,这将是一个快速且简单的解决方案。

让您获得更多控制权的更困难的选择是推出您自己的平台特定实现。我不打算讨论 DI 是如何工作的,你知道或可以看到 Accessing Native Features

这是一个带有代码的 Android 拍照示例,用于确定存储是否在外部以及是否需要旋转。

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {


        base.OnActivityResult(requestCode, resultCode, data);
        //FinishActivity(requestCode);
        try
        {

        if (resultCode == Result.Ok)
        {
            switch (requestCode)
            {
                case TAKE_PHOTO:
                    {
                        Java.IO.File photo = null;

                        if (isMounted)
                        {

                        photo = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.ToString(), SharedLibrary.strPhotoLocation);

                        }
                        else
                        {
                        photo = new Java.IO.File(CacheDir, SharedLibrary.strPhotoLocation);
                        }


                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.InJustDecodeBounds = true;
                        options.InSampleSize = 4;
                        options.InPurgeable = true;
                        options.InInputShareable = true;                          

                        try
                        {
                            //Cleanup code... removed this in favor of using options.InJustDecodeBounds to get info about the bitmap
                            //instead of creating it twice in memory

                            //Bitmap imageBitmap = BitmapFactory.DecodeFile(photo.AbsolutePath, options);


                            //int w = imageBitmap.Width;
                            //int h = imageBitmap.Height;

                            BitmapFactory.DecodeFile(photo.AbsolutePath, options);
                            int w = options.OutWidth;
                            int h = options.OutHeight;

                            Matrix matrix = new Matrix();
                            matrix.SetRotate(getNeededRotation(photo.AbsolutePath));

                            options.InJustDecodeBounds = false;

                            //Bitmap imageBitmap = Bitmap.CreateBitmap(BitmapFactory.DecodeFile(photo.AbsolutePath, options), 0, 0, w, h, matrix, false);                                                       
                            Bitmap imageBitmap = Bitmap.CreateScaledBitmap(BitmapFactory.DecodeFile(photo.AbsolutePath, options), w, h, false);
                            //...

上车

     private System.Boolean isMounted
    {
        get
        {
            return       (System.Boolean)Android.OS.Environment.ExternalStorageState.Equals(Android.OS.Environment.MediaMounted);
        }
    }                               

GetRotationNeeded

    private int getNeededRotation(string filepath)
    {
        ExifInterface exif = new ExifInterface(filepath);
        int orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, -1);
        int rotate = 0;
        switch (orientation)
        {
            case 6:
                {
                    rotate = 90;
                    break;
                }
            case 3:
                {
                    rotate = 180;
                    break;
                }
            case 8:
                {
                    rotate = 270;
                    break;
                }
            default:
                {
                    rotate = 0;
                    break;
                }


        }
        exif.Dispose();
        return rotate;
    }

【讨论】:

    【解决方案2】:

    你可以使用'mediaFile.Exif.Orientation'来检查它是纵向还是横向。('TopLeft'-->纵向。)然后在保存之前设置照片方向,

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

    【讨论】:

      猜你喜欢
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      相关资源
      最近更新 更多