Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

Commit

Permalink
Added ability to control zoom factor when available and added ability…
Browse files Browse the repository at this point in the history
… to example.

Note: this only works on Photo preset.
  • Loading branch information
eyaldar committed Jul 11, 2016
1 parent 9ad7204 commit de8acb7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
17 changes: 17 additions & 0 deletions CameraEngine/CameraEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ public class CameraEngine: NSObject {
}
}

private var _cameraZoomFactor: CGFloat = 1.0
public var cameraZoomFactor: CGFloat! {
get {
if let `captureDevice` = captureDevice {
_cameraZoomFactor = captureDevice.videoZoomFactor
}

return self._cameraZoomFactor
}
set {
let newZoomFactor = self.cameraDevice.changeCurrentZoomFactor(newValue)
if newZoomFactor > 0 {
self._cameraZoomFactor = newZoomFactor
}
}
}

public var blockCompletionBuffer: blockCompletionOutputBuffer? {
didSet {
self.cameraOutput.blockCompletionBuffer = self.blockCompletionBuffer
Expand Down
18 changes: 18 additions & 0 deletions CameraEngine/CameraEngineDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ class CameraEngineDevice {
}
}

func changeCurrentZoomFactor(newFactor: CGFloat) -> CGFloat {
var zoom: CGFloat = 1.0
if let currentDevice = self.currentDevice {
do {
try currentDevice.lockForConfiguration()
zoom = max(1.0, min(newFactor, currentDevice.activeFormat.videoMaxZoomFactor))
currentDevice.videoZoomFactor = zoom
currentDevice.unlockForConfiguration()
}
catch {
zoom = -1.0
fatalError("[CameraEngine] error, impossible to lock configuration device")
}
}

return zoom
}

func changeCurrentDevice(position: AVCaptureDevicePosition) {
self.currentPosition = position
switch position {
Expand Down
13 changes: 13 additions & 0 deletions ExampleProject/CameraEngineExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,18 @@ class ViewController: UIViewController {
self.cameraEngine.blockCompletionCodeDetection = { codeObject in
print("code object value : \(codeObject.stringValue)")
}

let twoFingerPinch = UIPinchGestureRecognizer(target: self, action: #selector(ViewController.onTwoFingerPinch(_:)))
self.view.addGestureRecognizer(twoFingerPinch)
self.view.userInteractionEnabled = true
}

@objc func onTwoFingerPinch(recognizer: UIPinchGestureRecognizer) {
let maxZoom: CGFloat = 6.0
let pinchVelocityDividerFactor: CGFloat = 5.0
if recognizer.state == .Changed {
let desiredZoomFactor = min(maxZoom, cameraEngine.cameraZoomFactor + atan2(recognizer.velocity, pinchVelocityDividerFactor))
cameraEngine.cameraZoomFactor = desiredZoomFactor
}
}
}

0 comments on commit de8acb7

Please sign in to comment.