在会话startRunning()之后拍摄静止照片会产生非常暗的照片

时间:2015-10-14 14:39:18

标签: ios iphone camera avfoundation photo

一些必要的背景: 我正在开发一款应用程序,它利用相机跟踪移动,如果是的话 发现拍了一张照片。我用opencv3跟踪移动。因为我正在分析视频流 我必须在跟踪运动后开始拍照。我关闭了运动追踪 视频会话并启动照片会话。

问题很简单:在session.startRunning()之后拍摄静止照片 产生几张非常黑暗(近乎黑色)的照片,然后照片才合适 露出。

我准备了一个非常简单的Xcode项目,其中包含一个视图和一个启动会话的按钮 拍张照片。该应用程序的行为与我描述的完全相同。

就在这里https://github.com/GrigoryPtashko/TakePhotoAfterSessionStart 该项目是SSCCE。您可以克隆它并在iPhone上运行以获得我正在谈论的内容的证据。

我在viewDidLoad()中初始化相机,如下所示:

    // fully initialize the front camera without starting the session
    var frontCamaDev: AVCaptureDevice!
    let availableCameraDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
    for device in availableCameraDevices as! [AVCaptureDevice] {
        if device.position == .Front {
            frontCamaDev = device
        }
    }
    do {
        // connect input to session
        let frontCameraInput = try AVCaptureDeviceInput(device: frontCamaDev)
        if session.canAddInput(frontCameraInput) {
            session.addInput(frontCameraInput)
        }

        // camera auth
        let authorizationStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
        switch authorizationStatus {
        case .NotDetermined:
            AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo,
                completionHandler: { (granted: Bool) -> Void in
                    if granted {
                        NSLog("photo camera permission granted")
                    } else {
                        NSLog("photo camera permission denied. How are we gonna do our awesome photo sesh??!!")
                    }
            })
        case .Authorized:
            NSLog("photo camera authorized")
        case .Denied, .Restricted:
            NSLog("photo camera not authorized")
        }

        // connect output to session
        stillCamOutput = AVCaptureStillImageOutput()
        if session.canAddOutput(stillCamOutput) {
            session.addOutput(stillCamOutput)
        }

        connection = stillCamOutput.connectionWithMediaType(AVMediaTypeVideo)
        connection.videoOrientation =
            AVCaptureVideoOrientation(rawValue: UIDevice.currentDevice().orientation.rawValue)!

        previewLayer = AVCaptureVideoPreviewLayer(session: session)
        cameraView.layer.addSublayer(previewLayer)
    } catch {
        NSLog("error [\(error)]")
    }

然后在按钮处理程序中,我启动会话并拍照:

    session.startRunning()

    stillCamOutput.captureStillImageAsynchronouslyFromConnection(connection) {
        (imageDataSampleBuffer, error) -> Void in

        if error != nil {
            NSLog("error while capturing still image: \(error)")

            return
        }

        if let image = UIImage(data:
            AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer))
        {
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
            NSLog("photo taken!")
        }
    }

所以,我所说的是session.startRunning()之后生成非常暗的照片的代码。 这是为什么? 我已经检查了会话开始的时间,需要130到150毫秒。哪个足够了 在我看来,是时候初始化相机了。

我还尝试手动设置焦距和曝光值,但没有运气..它仍然会产生黑暗 会议开始后的照片。

任何评论都会非常受欢迎。

0 个答案:

没有答案