c# - 根据设备方向转动相机图片 (Xamarin.iOS)

标签 c# ios xamarin.ios avfoundation ios-camera

我想制作一个 Xamarin.iOS 应用程序,我可以在其中像相机一样拍摄照片...我的应用程序仅支持纵向。

如果我拍摄照片时相机是风景,我想将拍摄的照片变成肖像。

有人知道我该怎么做吗?

代码

    public async void CapturePhoto()
    {

        var videoConnection = stillImageOutput.ConnectionFromMediaType(AVMediaType.Video);
        var sampleBuffer = await stillImageOutput.CaptureStillImageTaskAsync(videoConnection);

        var jpegImageAsBytes = AVCaptureStillImageOutput.JpegStillToNSData(sampleBuffer).ToArray();

        string base64StringImage = Convert.ToBase64String(jpegImageAsBytes);

        FaceRecognition faceRecognition = new FaceRecognition();
        int result = faceRecognition.SendPhoto(base64StringImage);
     } 

最佳答案

试试这个:

var currentOrientation = UIApplication.SharedApplication.StatusBarOrientation;

if (currentOrientation == UIInterfaceOrientation.Portrait)
{
    videoConnection.VideoOrientation = AVCaptureVideoOrientation.Portrait;
}
else if (currentOrientation == UIInterfaceOrientation.LandscapeRight)
{
    videoConnection.VideoOrientation = AVCaptureVideoOrientation.LandscapeRight;
}
//xxx

更新:

如果应用仅支持方向或者您锁定了屏幕,还有另一种检测设备方向的旧方法。 Core Motion

public void LockOrientation()
{
    CMMotionManager CMManager = new CMMotionManager();
    CMManager.DeviceMotionUpdateInterval = 0.2f;
    CMManager.StartDeviceMotionUpdates(NSOperationQueue.MainQueue, (motion, error) => {
        if (Math.Abs(motion.Gravity.X) > Math.Abs(motion.Gravity.Y))
        {
            Console.WriteLine("Lan");
            if (motion.Gravity.X > 0)
            {
                UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
                Console.WriteLine("Left");
            }
            else
            {
                UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeRight), new NSString("orientation"));
                Console.WriteLine("Right");
            }
        }
        else
        {
            if (motion.Gravity.Y >= 0)
            {
                Console.WriteLine("Down");
            }
            else
            {
                Console.WriteLine("UP");
            }
        }

        CMManager.StopDeviceMotionUpdates();
    });
}

引用here

关于c# - 根据设备方向转动相机图片 (Xamarin.iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46950069/

相关文章:

c# - 在 C# 应用程序之间流式传输数据

c# - T 未知时如何创建 Func<T, bool>

c# - ??的用法在 Nullable<int> 和 string 之间

iphone - 如何在其父 View 的坐标系中获得 subview 的有效边界

android - IOS/Android Phonegap 应用程序可以只链接到网站吗?

c# - 如何在 windows runner 服务器中运行 .drone.yml

ios - MKMapRect 设置后如何缩小

c# - MonoTouch native 资源管理

ios - 如何将 Cocoa Touch 静态库链接到 MonoTouch 项目?

xamarin.ios - 单点触控对话框。屏幕上任何定义位置的部分