摄像头捕获图像旋转到纵向-Xamarin android

摄像头捕获图像旋转到纵向-Xamarin android,xamarin,xamarin.forms,xamarin.android,Xamarin,Xamarin.forms,Xamarin.android,我有一个xamarin.android项目,它有一个自定义的相机预览。每当我初始化相机时,它将作为默认打开。所以我改变了SurfaceChanged方法的方向,如下所示 private int setCameraDisplayOrientation(Activity mContext, int v) { var degrees = 0; var orientation =0; Display display

我有一个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);
        }
这个主意是我从你那里得到的。但不知怎的,我不能相信它。有什么问题吗?我应该传递流而不是文件路径吗?这是由于surfaceview旋转造成的吗?我如何在任何设备上制作拍摄的图像肖像?感谢您的帮助

仅对于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;
            }
        }
现在根据获得的角度值旋转图像

我在流上执行操作,而不是实际的文件&以字节数组的形式返回


让我知道它是否有效。

就我个人而言,我从未见过一个解决方案涵盖所有Android设备的所有摄像头定位问题。最大的问题(IHO)是,有些手机有旋转传感器,有些手机使用“自动定向”,为了避免这些问题,您必须在拍摄前检查定向,并使用相机参数旋转定向。@Sushingover Hi感谢您的回复。你能解释一下吗?我是否应该使用摄像头方向保存图像exif数据?有很多基于Java/Kotlin的解决方案可以说明如何正确地执行此操作。我个人使用react native extensions作为我的goto源代码,因为他们的开发者社区非常庞大,并且知道他们的
react native camera
插件正确地处理了这个问题,因为我借用了很多Java片段来转换为C#,如果您在该插件的问题中搜索,您将找到正确处理方向的代码更正参考(当然,仍然有手机,如果您需要100%的合规性,您必须针对每个型号进行特殊处理)@sushinkover好的,先生。谢谢你的指导,你用的是哪块金块?好的,当然,希望它对你也有用。(我们在三星Galaxy s7、s8、s9上遇到了问题……上面的代码修复了它)嗨,我有一个疑问。我应该在“ExiFinInterface ei=new ExiFinInterface(fileName);”中传递文件路径或文件名吗?是文件名吗?文件路径。。。因为最近我离线了,你试过了吗?它是否有效?嗨,我测试了你的代码。但现在图像被保存在左旋转90度的位置:(这是否与我的曲面视图旋转有关?
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, 0, (int)imageStream.Length);

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

                Bitmap rotatedBitmap = Bitmap.CreateBitmap(originalImage, 0, 0, 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;
            }
        }