2016-06-21 15 views
1

私はAVFoundationを使用して、前面に向いたカメラで写真を撮影しています。デバイスが横向きの場合はすべて正常に動作しますが、横長の場合は画像が上下逆さまになります。LandscapeRightフロントカメラの写真逆さまのキャプチャ

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

答えて

2
は、カメラの設定を開始します

...

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) 

} 

あなたは、いくつかの回避策1つのオプションビーイングを試みることができる:

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

もう一つの仕事は周りに回転させ、および/または撮影後の画像を反転することであろう。

+0

プレビューレイヤーは正確に表示されますが、それは間違ってキャプチャされた写真だけです。 – lunadiviner

+0

私の懸念は、あなたがメソッド呼び出しをしているということでした。あなたが 'session.startRunning()'のように 'output'を追加する前に – Asdrubal

+0

私はそれを変更してsession.startRunning()が最後に呼び出されるようにしました。写真が撮られますが、私はまだ同じ問題に直面しています。 – lunadiviner

関連する問題