微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

相机拍摄的图像旋转为人像-Xamarin android

如何解决相机拍摄的图像旋转为人像-Xamarin android

我有一个xamarin.android项目,该项目具有自定义摄像头预览。每当我初始化相机时,它将认打开为风景。所以我像这样从SurfaceChanged方法改变了方向。

private int setCameradisplayOrientation(Activity mContext,int v)
        {
            var degrees = 0;
            var orientation =0;

            display display = mContext.GetSystemService(Context.WindowService).JavaCast<IWindowManager>().Defaultdisplay;

            Camera.CameraInfo info = new Camera.CameraInfo();
            Camera.GetCameraInfo(v,info);
            var rotation = windowManager.Defaultdisplay.Rotation;

            displayMetrics dm = new displayMetrics();
            display.GetMetrics(dm);
            int width = dm.WidthPixels;
            int height = dm.HeightPixels;

            //Natural orientation is portrait
            if ((rotation == SurfaceOrientation.Rotation0 || rotation == SurfaceOrientation.Rotation180) && height > width ||
                (rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270) && width > height)
            {
                switch (rotation)
                {
                    case SurfaceOrientation.Rotation0:
                        degrees = 90;
                        break;
                    case SurfaceOrientation.Rotation90:
                        degrees = 0;
                        break;
                    case SurfaceOrientation.Rotation180:
                        degrees = 270;
                        break;
                    case SurfaceOrientation.Rotation270:
                        degrees = 180;
                        break;
                }
            }
            
            return (degrees);
        }

工作正常。它将以纵向方式布置前后摄像头。

问题

当我拍摄照片时,在某些设备中,它将以横向模式存储在某些设备中,它将存储肖像。无论如何,我都希望图像处于纵向模式。为此,我尝试获取图像的Exif数据并将其相应地旋转为纵向模式。但是在某些设备中,如三星,VIVO,方向值变为“ 0”。我不知道该怎么做。如果我PreRotate 90,那么某些设备可以解决此问题,而其他设备可以将照片向上保存。

**Managing Rotation**

 Android.Graphics.Bitmap loadAndResizeBitmap(string filePath)
    {

        Android.Graphics.Bitmap resizedBitmap = Android.Graphics.BitmapFactory.DecodeFile(filePath);
        ExifInterface exif = null;
        try
        {
            exif = new ExifInterface(filePath);
            string orientation = exif.GetAttribute(ExifInterface.TagOrientation);

            Android.Graphics.Matrix matrix = new Android.Graphics.Matrix();
            switch (orientation)
            {
                case "1":
                    matrix.PreRotate(-90);
                    resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap,resizedBitmap.Width,resizedBitmap.Height,matrix,false);
                    matrix.dispose();
                    matrix = null;
                    break;
                case "3":
                    matrix.PreRotate(180);
                    resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap,false);
                    matrix.dispose();
                    matrix = null;
                    break;
                case "4":
                    matrix.PreRotate(180);
                    resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap,false);
                    matrix.dispose();
                    matrix = null;
                    break;
                case "5":
                    matrix.PreRotate(90);
                    resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap,false);
                    matrix.dispose();
                    matrix = null;
                    break;
                case "6": // portrait
                    matrix.PreRotate(90);
                    resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap,false);
                    matrix.dispose();
                    matrix = null;
                    break;
                case "7":
                    matrix.PreRotate(-90);
                    resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap,false);
                    matrix.dispose();
                    matrix = null;
                    break;
                case "8":
                    matrix.PreRotate(-90);
                    resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap,false);
                    matrix.dispose();
                    matrix = null;
                    break;
                case "0":
                    matrix.PreRotate(-90);
                    resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap,false);
                    matrix.dispose();
                    matrix = null;
                    break;
            }

            return resizedBitmap;

        }

        catch (IOException ex)
        {
            return null;
        }

    }

我从Xamarin.Andoid Image rotation得到了这个想法。但是我不知何故不能继续。有什么问题吗?我应该通过Stream而不是文件路径吗?是因为曲面旋转吗?无论在任何设备上如何拍摄拍摄的肖像肖像?任何帮助表示赞赏。

解决方法

仅对于Xamarin.Android,使用依赖项服务在共享项目中调用

由此获得图像的旋转角度。

public int GetImageRotation(string filePath)
        {
            try
            {
                ExifInterface ei = new ExifInterface(filePath);
                Orientation orientation = (Orientation)ei.GetAttributeInt(ExifInterface.TagOrientation,(int)Orientation.Undefined);
                switch (orientation)
                {
                    case Orientation.Rotate90:
                        return 90;
                    case Orientation.Rotate180:
                        return 180;
                    case Orientation.Rotate270:
                        return 270;
                    default:
                        return 0;
                }
            }
            catch(Exception ex)
            {
                return 0;
            }
        }

现在根据获得的角度值旋转图像。

我对流而不是实际文件执行操作并以字节数组的形式返回。

public byte[] RotateImage(System.IO.Stream imageStream,string filePath)
        {
            int rotationDegrees = GetImageRotation(filePath)
            byte[] byteArray = new byte[imageStream.Length];
            try
            {
                imageStream.Read(byteArray,(int)imageStream.Length);

                Bitmap originalImage = BitmapFactory.DecodeByteArray(byteArray,byteArray.Length);
                Matrix matrix = new Matrix();
                matrix.PostRotate((float)rotationDegrees);

                Bitmap rotatedBitmap = Bitmap.CreateBitmap(originalImage,originalImage.Width,originalImage.Height,matrix,true);

                using (MemoryStream ms = new MemoryStream())
                {
                    rotatedBitmap.Compress(Bitmap.CompressFormat.Jpeg,100,ms);
                    return ms.ToArray();
                }
            }
            catch(Exception ex)
            {
                return byteArray;
            }
        }

让我知道它是否有效。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。