James Combs James Combs - 4 months ago 41
iOS Question

Need to Mirror Video Orientation and Handle Rotation When Using Front Camera

I can't quite figure out how to handle front facing camera video capture orientations. I have all rotations handled for the back camera when capturing video and pictures and all rotations handled for the front camera when taking pictures, as well as saving the captured videos and pictures with the correct orientations as well, except for the front camera video capture.

The first problem is when in either landscape mode, the video is not saved correctly with the correct orientation. The second problem is that the saved video is mirrored. Although i know how to handle this mirroring effect for picture using the front camera, I am not sure what to call to handle it for videos.

I had a lot of trouble trying to find anything specifically for this one issue and failed to do so. If anybody could point me to a thread that addresses this specific issue, that would be great.

Either way, here is the method that is called that handles video orientation when device orientation changes. I am not sure exactly what to add to my code if the front camera is being used.

/**************************************************************************
DEVICE ORIENTATION DID CHANGE
**************************************************************************/
func deviceOrientationDidChange() {

println("DEVICE ORIENTATION DID CHANGE CALLED")

let orientation: UIDeviceOrientation = UIDevice.currentDevice().orientation

//------ IGNORE THESE ORIENTATIONS ------
if orientation == UIDeviceOrientation.FaceUp || orientation == UIDeviceOrientation.FaceDown || orientation == UIDeviceOrientation.Unknown || orientation == UIDeviceOrientation.PortraitUpsideDown || self.currentOrientation == orientation {

println("device orientation does not need to change --- returning...")

return
}


self.currentOrientation = orientation


//------ APPLY A ROTATION USING THE STANDARD ROTATION TRANSFORMATION MATRIX in R3 ------

/*

x y z
--- ---
x | cosø sinø 0 |
y | -sinø consø 0 |
z | 0 0 1 |
--- ---

*/


//----- PERFORM BUTTON AND VIDEO DATA BUFFER ROTATIONS ------
switch orientation {

case UIDeviceOrientation.Portrait:

rotateButtons(self.degrees0)

if self.usingFrontCamera == true {


}
else {

}

println("Device Orientation Portrait")

break

case UIDeviceOrientation.LandscapeLeft:

println("Device Orientation LandScapeLeft")

rotateButtons(self.degrees90)

if self.usingFrontCamera == true {

println("Using front camera, rotation in landscape left")

// if let connection = self.captureConnection {
//
// connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight
//
// println("Capture connection Orientation is LandScape Right")
// }
// else {
//
// println("Capture connection is nil, could not change video orientation")
// }
}
else {

if let connection = self.captureConnection {

connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight

println("Capture connection Orientation is LandScape Right")
}
else {

println("Capture connection is nil, could not change video orientation")
}
}

break

case UIDeviceOrientation.LandscapeRight:

println("Device Orientation LandscapeRight")

rotateButtons(-self.degrees90)

if self.usingFrontCamera == true {

println("Using front camera, rotation in landscape right")

// if let connection = self.captureConnection {
//
// connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight
//
// println("Capture connection Orientation is LandScape Left")
// }
// else {
//
// println("Capture connection is nil, could not change video orientation")
// }
}
else {

if let connection = self.captureConnection {

connection.videoOrientation = AVCaptureVideoOrientation.LandscapeLeft

println("Capture connection Orientation is LandScape Left")
}
else {

println("Capture connection is nil, could not change video orientation")
}
}

break

default:

break
}
}

Sam Sam
Answer

Based on this answer: Video Saving in the wrong orientation AVCaptureSession

I faced the same issue and was able to fix it following this post.

    var videoConnection:AVCaptureConnection?

for connection in self.fileOutput.connections { for port in connection.inputPorts! { if port.mediaType == AVMediaTypeVideo { videoConnection = connection as? AVCaptureConnection if videoConnection!.supportsVideoMirroring { videoConnection!.videoMirrored = true } } } }

Please let me know if it helps you James