LandscapeRight前置摄像头拍摄的照片颠倒了

时间:2016-06-21 17:57:03

标签: ios swift avfoundation

我正在使用AVFoundation使用前置摄像头拍摄照片。当设备朝向风景时,一切正常,但是当风景正确时,图片会被颠倒捕获。

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  
    }

以下是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()

1 个答案:

答案 0 :(得分:3)

开始配置相机...

    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()

...设置相机的预览图层。

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)

}

您可以尝试一些选项:

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

另一种解决方法是在拍摄照片后旋转和/或翻转图像。