首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >斯威夫特:同时自动对焦/曝光和连续自动聚焦/曝光?

斯威夫特:同时自动对焦/曝光和连续自动聚焦/曝光?
EN

Stack Overflow用户
提问于 2018-03-28 18:37:37
回答 1查看 3.8K关注 0票数 6

现在,在我的相机应用程序中,我让用户触碰任何地方来设置焦点和曝光,但我如何才能像苹果的相机应用程序那样,在这两个世界中发挥最好的效果呢?

例如,用户可能希望将注意力集中在前景上,但如果场景发生了足够大的变化,则应该返回到continuousAutoFocus。同样,如果用户将相机指向光线,它应该改变曝光以使它看起来正确,然后当相机回到场景时,它应该再次修复曝光,这样就不会太暗了。然而,他们仍然可以选择让它变得更轻或更暗,这取决于他们通过相机的视角接触到的东西。

现在,当视图出现时,我将默认设置为屏幕中心:

代码语言:javascript
复制
func setDefaultFocusAndExposure() {

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

    if let device = AVCaptureDevice.default(for:AVMediaType.video) {
        do {
            try device.lockForConfiguration()
            if device.isFocusPointOfInterestSupported {
                print(focusPoint)
                device.focusPointOfInterest = focusPoint
                device.focusMode = AVCaptureDevice.FocusMode.autoFocus
            }
            if device.isExposurePointOfInterestSupported {
                device.exposurePointOfInterest = focusPoint
                device.exposureMode = AVCaptureDevice.ExposureMode.autoExpose
            }
            device.unlockForConfiguration()

        } catch {
            // Handle errors here
            print("There was an error focusing the device's camera")
        }
    }

}

此外,我还让用户根据接触的位置设置焦点和曝光:

代码语言:javascript
复制
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    let bounds = UIScreen.main.bounds

    let touchPoint = touches.first! as UITouch
    let screenSize = bounds.size
    let focusPoint = CGPoint(x: touchPoint.location(in: view).y / screenSize.height, y: 1.0 - touchPoint.location(in: view).x / screenSize.width)

    if let device = AVCaptureDevice.default(for:AVMediaType.video) {
        do {
            try device.lockForConfiguration()
            if device.isFocusPointOfInterestSupported {
                device.focusPointOfInterest = focusPoint
                device.focusMode = AVCaptureDevice.FocusMode.autoFocus
            }
            if device.isExposurePointOfInterestSupported {
                device.exposurePointOfInterest = focusPoint
                device.exposureMode = AVCaptureDevice.ExposureMode.autoExpose
            }
            device.unlockForConfiguration()

        } catch {
            // Handle errors here
            print("There was an error focusing the device's camera")
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-06 17:10:02

弄明白了。首先,我在myViewDidAppear上调用一个设置默认焦点和曝光模式的方法:

代码语言:javascript
复制
@objc func setDefaultFocusAndExposure() {

    if let device = AVCaptureDevice.default(for:AVMediaType.video) {
        do {
            try device.lockForConfiguration()
                device.isSubjectAreaChangeMonitoringEnabled = true
                device.focusMode = AVCaptureDevice.FocusMode.continuousAutoFocus
                device.exposureMode = AVCaptureDevice.ExposureMode.continuousAutoExposure
            device.unlockForConfiguration()

        } catch {
            // Handle errors here
            print("There was an error focusing the device's camera")
        }
    }
}

这里的诀窍不是把autoFocus和continuousAutoFocus混为一谈。这样,在默认情况下,它会不断地观察场景,并设置焦点和曝光。同时,isSubjectAreaChangeMonitorEnabled也是一个非常重要的部分。这样,当场景改变焦点或曝光时,您可以注册一个通知来调用一个函数,这样您就可以切换焦点模式。很快会有更多的报道。

在触摸屏幕时,可以根据某一点设置焦点和曝光:

代码语言:javascript
复制
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    let bounds = UIScreen.main.bounds

    let touchPoint = touches.first! as UITouch
    let screenSize = bounds.size
    let focusPoint = CGPoint(x: touchPoint.location(in: view).y / screenSize.height, y: 1.0 - touchPoint.location(in: view).x / screenSize.width)

    if let device = AVCaptureDevice.default(for:AVMediaType.video) {
        do {
            try device.lockForConfiguration()
            if device.isFocusPointOfInterestSupported {
                device.focusPointOfInterest = focusPoint
                device.focusMode = AVCaptureDevice.FocusMode.autoFocus
            }
            if device.isExposurePointOfInterestSupported {
                device.exposurePointOfInterest = focusPoint
                device.exposureMode = AVCaptureDevice.ExposureMode.autoExpose
            }
            device.unlockForConfiguration()

        } catch {
            // Handle errors here
            print("There was an error focusing the device's camera")
        }
    }
}

在viewDidLoad中注册通知,以便将焦点和曝光模式设置为连续模式。在这里,我只是调用了一个函数,该函数设置了calling的默认设置:

代码语言:javascript
复制
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(self.setDefaultFocusAndExposure),
                                           name: NSNotification.Name.AVCaptureDeviceSubjectAreaDidChange,
                                           object: nil)

不要忘记删除NotificationCenter中的观察者:

代码语言:javascript
复制
deinit {
    NotificationCenter.default.removeObserver(self)
}
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49541837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档