(Xamarin.iOS)如果设备方向是横向的,请转动相机图片

时间:2017-10-26 09:04:09

标签: 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);
     } 

1 个答案:

答案 0 :(得分:2)

试试这个:

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