横屏右侧前置摄像头拍摄照片上下颠倒。

3

我使用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个回答

4

开始进行相机的配置...

    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")
    }
}

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

预览层显示正常,只是拍摄的照片有问题。 - lunadiviner
我的关注点是您调用方法的顺序。例如在添加输出之前调用 session.startRunning() - Asdrubal
我将代码改变,把session.startRunning()放在最后调用,并且我也尝试在拍摄照片之前的捕获函数中设置输出方向,但是我仍然遇到了相同的问题。 - lunadiviner
我在拍摄后翻转了图像 - 希望我知道为什么一开始会发生这种情况,但这样已经足够好了。 - lunadiviner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接