现在,在我的相机应用程序中,我让用户触碰任何地方来设置焦点和曝光,但我如何才能像苹果的相机应用程序那样,在这两个世界中发挥最好的效果呢?
例如,用户可能希望将注意力集中在前景上,但如果场景发生了足够大的变化,则应该返回到continuousAutoFocus。同样,如果用户将相机指向光线,它应该改变曝光以使它看起来正确,然后当相机回到场景时,它应该再次修复曝光,这样就不会太暗了。然而,他们仍然可以选择让它变得更轻或更暗,这取决于他们通过相机的视角接触到的东西。
现在,当视图出现时,我将默认设置为屏幕中心:
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")
}
}
}此外,我还让用户根据接触的位置设置焦点和曝光:
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")
}
}
}发布于 2018-04-06 17:10:02
弄明白了。首先,我在myViewDidAppear上调用一个设置默认焦点和曝光模式的方法:
@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也是一个非常重要的部分。这样,当场景改变焦点或曝光时,您可以注册一个通知来调用一个函数,这样您就可以切换焦点模式。很快会有更多的报道。
在触摸屏幕时,可以根据某一点设置焦点和曝光:
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的默认设置:
NotificationCenter.default.addObserver(self,
selector: #selector(self.setDefaultFocusAndExposure),
name: NSNotification.Name.AVCaptureDeviceSubjectAreaDidChange,
object: nil)不要忘记删除NotificationCenter中的观察者:
deinit {
NotificationCenter.default.removeObserver(self)
}https://stackoverflow.com/questions/49541837
复制相似问题