15

I have been trying to capture frames from front camera and present it on the view. Here is my code

_session = [[AVCaptureSession alloc] init];
_session.sessionPreset = AVCaptureSessionPreset640x480;

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *deviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:&error];

if (deviceInput) {
    [_session addInput:deviceInput];
}
else {
    DDLogInfo(@"Some wierd shit happened");
    return;
}

// Session output
/*
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[_session addOutput:output];
output.metadataObjectTypes = @[AVMetadataObjectTypeFace];
AVCaptureConnection *connection = [output connectionWithMediaType:AVMediaTypeMetadata];
connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
*/

// Session output
AVCaptureMovieFileOutput *videoOutput = [[AVCaptureMovieFileOutput alloc] init];
[_session addOutput:videoOutput];
AVCaptureConnection *connection = [videoOutput connectionWithMediaType:AVMediaTypeVideo];
connection.videoOrientation = AVCaptureVideoOrientationPortrait;

// Preview layer
_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
_previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
_previewLayer.frame = CGRectMake(0, 0, self.middleView.frame.size.width, self.middleView.frame.size.height);
[self.middleView.layer addSublayer:_previewLayer];

[_session startRunning];

Problem with this is, when my iPad is landscape, the images on presentation layer are rotated 90 degrees. How do i fix this? I have been browsing stack overflow, trying to do something with AVCaptureConnection, but alas to no avail.

MegaManX
  • 8,766
  • 12
  • 51
  • 83

2 Answers2

25

Solved it, now it is working in all orientations. We need to set this

_previewLayer.connection.videoOrientation = [self videoOrientationFromCurrentDeviceOrientation];

where the method is:

 - (AVCaptureVideoOrientation) videoOrientationFromCurrentDeviceOrientation {
switch (self.interfaceOrientation) {
    case UIInterfaceOrientationPortrait: {
        return AVCaptureVideoOrientationPortrait;
    }
    case UIInterfaceOrientationLandscapeLeft: {
        return AVCaptureVideoOrientationLandscapeLeft;
    }
    case UIInterfaceOrientationLandscapeRight: {
        return AVCaptureVideoOrientationLandscapeRight;
    }
    case UIInterfaceOrientationPortraitUpsideDown: {
        return AVCaptureVideoOrientationPortraitUpsideDown;
    }
}
}

Also on capture output we need to set:

AVCaptureConnection *output2VideoConnection = [videoOutput connectionWithMediaType:AVMediaTypeVideo];
output2VideoConnection.videoOrientation = [self videoOrientationFromCurrentDeviceOrientation];
MegaManX
  • 8,766
  • 12
  • 51
  • 83
14

In Swift 3:

First we have to set previewLayer.connection.videoOrientation like MegaManX do

previewLayer.connection.videoOrientation = self.videoOrientationFromCurrentDeviceOrientation()

where the method is:

func videoOrientationFromCurrentDeviceOrientation() -> AVCaptureVideoOrientation {
    switch UIApplication.shared.statusBarOrientation {
    case .portrait:
        return AVCaptureVideoOrientation.portrait
    case .landscapeLeft:
        return AVCaptureVideoOrientation.landscapeLeft
    case .landscapeRight:
        return AVCaptureVideoOrientation.landscapeRight
    case .portraitUpsideDown:
        return AVCaptureVideoOrientation.portraitUpsideDown
    default:
        // Can this happen?
        return AVCaptureVideoOrientation.portrait
    }
}

Then we also have to handle the videoOrientation when the user rotates the device (method come from here)

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

    coordinator.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) -> Void in
        self.previewLayer.connection.videoOrientation = self.videoOrientationFromCurrentDeviceOrientation()

        }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
            // Finish Rotation
    })

    super.viewWillTransition(to: size, with: coordinator)
}
He Yifei 何一非
  • 2,592
  • 4
  • 38
  • 69
  • What to do when device orientation is locked? `viewWillTransitionToSize` method will not call. How to manage this. I have to detect picture orientation when device locked. – mahbaleshwar hegde Dec 05 '16 at 13:42
  • @mahbaleshwarhegde you mean despite the device orientation is locked, you still want the picture rotate with the device? – He Yifei 何一非 Dec 05 '16 at 13:43
  • yes. when device locked i am getting statusBarOrientation is portrait. So video connection become portrait even though i took picture in landscape mode. How to do. any idea? – mahbaleshwar hegde Dec 05 '16 at 13:46
  • @mahbaleshwarhegde try the answers in this question http://stackoverflow.com/q/4574693/2603230 :) – He Yifei 何一非 Dec 05 '16 at 13:50
  • My app not have any coremotion functionality. Just for getting orientation in only one screen if i use the coremotion Apple may reject the app. Any other idea is appreciated. – mahbaleshwar hegde Dec 06 '16 at 05:14
  • @mahbaleshwarhegde how about try to force user enter a certain orientation when taking photos? :) http://stackoverflow.com/q/26357162/2603230 – He Yifei 何一非 Dec 06 '16 at 13:14
  • @mahbaleshwarhegde or maybe change the orientation of the photo to the correct one afterwards? (like iOS's "Camera" app does) – He Yifei 何一非 Dec 06 '16 at 13:15