ios 9 AVCaptureDevice设置焦点

时间:2016-04-18 14:19:27

标签: ios avfoundation autofocus ios-camera front-camera

我试图用前置摄像头设置焦点。

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    let screenSize = cameraView.bounds.size

    let frameSize:CGSize = view.frame.size

    if let touchPoint = touches.first {

        var location:CGPoint = touchPoint.locationInView(cameraView)

        print("Tap Location : X: \(location.x), Y: \(location.y)")

        if cameraManager.cameraDevice == .Front {
            print("Front camera is used")

            location.x = frameSize.width - location.x;
        }
        else {
            print("Back camera is used")
        }

        let x = location.x / frameSize.width
        let y = 1.0 - (location.x / frameSize.width)

        let focusPoint = CGPoint(x: x, y: y)

        print("POINT : X: \(x), Y: \(y)")


        let captureDevice = (AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as! [AVCaptureDevice]).filter{$0.position == .Front}.first

        if let device = captureDevice {
            do {
                try device.lockForConfiguration()

                let support:Bool = device.focusPointOfInterestSupported

                if support  {

                    print("focusPointOfInterestSupported: \(support)")

                    device.focusPointOfInterest = focusPoint

                    // device.focusMode = .ContinuousAutoFocus
                    device.focusMode = .AutoFocus
                    // device.focusMode = .Locked

                    device.unlockForConfiguration()

                    print("Focus point was set successfully")
                }
                else{
                    print("focusPointOfInterestSupported is not supported: \(support)")
                }
            }
            catch {
                // just ignore
                print("Focus point error")
            }
        }
    }
}

适用于iphone6用于后置摄像头。但是,当我激活前置摄像头时,它会脱落。 focusPointOfInterestSupported始终返回false。

我该如何解决这个问题。我可以通过编程方式设置焦点吗?

谢谢!

1 个答案:

答案 0 :(得分:5)

focusPointOfInterestSupported布尔值表示此设备的前置摄像头不支持以这种方式设置焦点。事实上,它似乎是&#34; FaceTime&#34; iPhone 6的相机具有固定焦点。没有提及&#34;焦点&#34;在iPhone 6的技术规格部分&#34; FaceTime&#34;相机,但面向后方&#34; iSight&#34;相机部分。此外,尝试将前置摄像头重新聚焦在靠近镜头的模糊物体上(通过点击相机应用程序)似乎只调整曝光,而不是焦点。

来源:https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVCaptureDevice_Class/index.html#//apple_ref/occ/instp/AVCaptureDevice/focusPointOfInterestSupported

来源:http://www.apple.com/iphone-6/specs/