lunadiviner
lunadiviner

Reputation: 509

LandscapeRight front camera photos capturing upside down

I'm using AVFoundation to capture photos with the front facing camera. When the device is oriented landscape left everything works fine, but when it's landscape right the pictures are captured upside down.

func setOutputOrientation() {  
    var outputOrientation: AVCaptureVideoOrientation!  
    let connection = self.previewLayer.connection  

    if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft {  
       outputOrientation = AVCaptureVideoOrientation.LandscapeRight  
    }  

    else if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight {  
       outputOrientation = AVCaptureVideoOrientation.LandscapeLeft  
    }  

    else if UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait {  
       outputOrientation = AVCaptureVideoOrientation.Portrait  
    }  

    else if UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown {  
       outputOrientation = AVCaptureVideoOrientation.PortraitUpsideDown  
    }  

    connection.videoOrientation = outputOrientation  
    }

and the following is in viewDidLoad:

let device = getFrontCamera(  
let error   : NSError? = nil  

let input: AVCaptureDeviceInput?  
do {  
  input = try AVCaptureDeviceInput(device: device)  
} catch _ {  
  input = nil  
}  
if input != nil {  
  session.addInput(input)  
}  

else {  
  print(error)  
}  

session.sessionPreset = AVCaptureSessionPresetPhoto  

previewLayer = AVCaptureVideoPreviewLayer(session: session) as AVCaptureVideoPreviewLayer  
previewLayer.frame = CGRectMake(0, 0, 300, 300)  
previewLayer.setAffineTransform( CGAffineTransformTranslate(CGAffineTransformMakeScale(0.33, 0.33), -375, -480) )  
previewLayer.position = upperRightPosition  
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;  
self.view.layer.addSublayer(previewLayer)  

let photoImage = UIImage(named:"PhotoTwo")  

session.startRunning()  

output.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]  

if session.canAddOutput(output)  
{  
  session.addOutput(output)  
}  

self.setOutputOrientation()

Upvotes: 3

Views: 1560

Answers (1)

Asdrubal
Asdrubal

Reputation: 2481

Start the configuration of the camera ...

    if captureSession.canAddOutput(videoOutput) {
        captureSession.addOutput(videoOutput)
        let connection = videoOutput.connectionWithMediaType(AVMediaTypeVideo)
        if connection.supportsVideoOrientation {
            connection.videoOrientation = isFrontCamera ? AVCaptureVideoOrientation.LandscapeLeft : AVCaptureVideoOrientation.LandscapeRight
        }
        print("adding output")

    } else {
        print("Could not add front video output")
        return
    }

    captureSession.commitConfiguration()
    captureSession.startRunning()

... set up the preview layer for the camera.

func setupPreviewLayer() {        
    var previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    var bounds = cameraView.bounds
    previewLayer.backgroundColor = UIColor.blackColor().CGColor
    previewLayer.bounds = bounds
    previewLayer.videoGravity = AVLayerVideoGravityResize
    previewLayer.position = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds))
    previewLayer.connection.videoOrientation = recordingOrientation;
    self.cameraView.layer.addSublayer(previewLayer)

}

You could try a few work arounds one option being:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.currentDevice().orientation.isLandscape.boolValue {
        print("Landscape")
    } else {
        print("Portrait")
    }
}

Another work around would be to rotate and/or flip the image after the picture is taken.

Upvotes: 4

Related Questions