AVCaptureDevice videoZoomFactor Pinch-to-Zoom Rate



我正在尝试在AVFoundation中使用AVCaptureDevice实现">捏合缩放"功能:

 @IBAction func pinchGestureDetected(_ gestureRecognizer: UIPinchGestureRecognizer)  {
    switch gestureRecognizer.state {
    case .began:
        print ("began")
        self.currenZoomFactor = self.videoDevice!.videoZoomFactor
        do {
            try self.videoDevice!.lockForConfiguration()
        } catch let error as NSError {
            NSLog("Could not lock device for configuration: %@", error)
        }
    case .changed:
        print ("changed")
            var zoomValue : CGFloat = ((gestureRecognizer.scale) - 1) + self.currenZoomFactor
            if zoomValue > min(10.00, self.videoDevice!.activeFormat.videoMaxZoomFactor) {
                zoomValue = min(10.00, self.videoDevice!.activeFormat.videoMaxZoomFactor)
            } else if zoomValue < 1.00 {
                zoomValue = 1.00
            }
            self.videoDevice!.videoZoomFactor = sentZoomValue
    case .ended, .cancelled:
        print ("ended/canceld")
        self.videoDevice!.unlockForConfiguration()
    default:
        break
    }
}

以上工作正常。 但是,如上所述,缩放速率与捏合比例呈线性关系。 这使得在较高的变焦系数下缩放速度要慢得多。

如何在更高的变焦系数下获得更快的变焦率?

为了获得更快的缩放速率,我们需要以下一些计算。

您可以从pinchGestureDetected调用此实用程序方法

func zoomto(scale: CGFloat, hasBegunToZoom: Bool) {
    if hasBegunToZoom {
        initialPinchZoom = captureDevice.videoZoomFactor
    }
    do {
        try captureDevice.lockForConfiguration()
        if scale < 1.0 {
            zoomFactor = initialPinchZoom - pow(captureDevice.activeFormat.videoMaxZoomFactor, 1.0 - scale)
        }
        else {
            zoomFactor = initialPinchZoom + pow(captureDevice.activeFormat.videoMaxZoomFactor, (scale - 1.0f) / 2.0f)
        }
        zoomFactor = min(10.0, zoomFactor)
        zoomFactor = max(1.0, zoomFactor)
        captureDevice.videoZoomFactor = zoomFactor
        captureDevice.unlockForConfiguration()
    } catch let error as NSError {
        NSLog("Could not lock device for configuration: %@", error)
    }
}

你可以像下面这样打电话

@IBAction func pinchGestureDetected(_ gestureRecognizer: UIPinchGestureRecognizer)  {
    zoomto(scale: gestureRecognizer.scale, hasBegunToZoom:(gestureRecognizer.state == .began))
}

相关内容

  • 没有找到相关文章

最新更新