0

I want to make a Xamarin.iOS app where I can capture pictures like the camera... My app supports only portrait.

I want to turn the captured picture to portrait if the camera was landscape when i capture the picture.

Does someone know how I can do this?

Code

    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);
     } 
DerStarkeBaer
  • 669
  • 8
  • 28

1 Answers1

2

Try this :

var currentOrientation = UIApplication.SharedApplication.StatusBarOrientation;

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

Update:

If the app only supports an orientation or you lock the screen , there is another old way to detect device orientation. 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();
    });
}

Refer to here

ColeX
  • 14,062
  • 5
  • 43
  • 240
  • 1
    I solved the problem... I had to replace stillImageOutput with videoConnection. Thank you for your help – DerStarkeBaer Oct 27 '17 at 07:17
  • But there is another problem when I only allow the Portrait layout the status bar is always on top and it wont work... Do you have another idea instead of the orientation of the status bar? – DerStarkeBaer Oct 27 '17 at 08:09
  • https://forums.developer.apple.com/thread/62004 , If it helps please let me know.@Dominic – ColeX Oct 27 '17 at 08:22
  • this looks nice but it telsl me the orientation to late... (after everything is already done) – DerStarkeBaer Oct 30 '17 at 10:31
  • So it work now... I had to take only some parts of your code not the full... Thank you for your help – DerStarkeBaer Oct 30 '17 at 11:58