Skip to content
Permalink
Browse files

support zoom and add focus point getter

  • Loading branch information...
felix8888 authored and lickel committed May 4, 2017
1 parent d9ec41e commit 8b4ed3478e5efa1bfb15c4f61cb377bf895ba508
Showing with 34 additions and 4 deletions.
  1. +3 −0 Source/PBJVision.h
  2. +31 −4 Source/PBJVision.m
@@ -232,6 +232,9 @@ static CGFloat const PBJVideoBitRate1280x750 = 5000000 * 8;
@property (nonatomic, readonly) Float64 capturedAudioSeconds;
@property (nonatomic, readonly) Float64 capturedVideoSeconds;

@property (nonatomic, assign) CGFloat videoZoomFactor;
@property (nonatomic, readonly) CGPoint focusPoint;

- (void)startVideoCapture;
- (void)pauseVideoCapture;
- (void)resumeVideoCapture;
@@ -48,7 +48,7 @@
static CGFloat const PBJVisionThumbnailWidth = 160.0f;

// KVO contexts

static NSString * const PBJVisionFocusModeObserverContext = @"PBJVisionFocusModeObserverContext";
static NSString * const PBJVisionFocusObserverContext = @"PBJVisionFocusObserverContext";
static NSString * const PBJVisionExposureObserverContext = @"PBJVisionExposureObserverContext";
static NSString * const PBJVisionWhiteBalanceObserverContext = @"PBJVisionWhiteBalanceObserverContext";
@@ -124,6 +124,7 @@ @interface PBJVision () <
BOOL _autoFreezePreviewDuringCapture;
BOOL _usesApplicationAudioSession;
BOOL _automaticallyConfiguresApplicationAudioSession;
BOOL _locked;

PBJFocusMode _focusMode;
PBJExposureMode _exposureMode;
@@ -485,7 +486,8 @@ - (void)setFocusMode:(PBJFocusMode)focusMode
return;

_focusMode = focusMode;

_locked = focusMode == PBJFocusModeLocked;

NSError *error = nil;
if (_currentDevice && [_currentDevice lockForConfiguration:&error]) {
[_currentDevice setFocusMode:(AVCaptureFocusMode)focusMode];
@@ -847,6 +849,7 @@ - (void)_setupCamera
[notificationCenter addObserver:self selector:@selector(_deviceSubjectAreaDidChange:) name:AVCaptureDeviceSubjectAreaDidChangeNotification object:nil];

// current device KVO notifications
[self addObserver:self forKeyPath:@"currentDevice.focusMode" options:NSKeyValueObservingOptionNew context:(__bridge void *)PBJVisionFocusModeObserverContext];
[self addObserver:self forKeyPath:@"currentDevice.adjustingFocus" options:NSKeyValueObservingOptionNew context:(__bridge void *)PBJVisionFocusObserverContext];
[self addObserver:self forKeyPath:@"currentDevice.adjustingExposure" options:NSKeyValueObservingOptionNew context:(__bridge void *)PBJVisionExposureObserverContext];
[self addObserver:self forKeyPath:@"currentDevice.adjustingWhiteBalance" options:NSKeyValueObservingOptionNew context:(__bridge void *)PBJVisionWhiteBalanceObserverContext];
@@ -865,6 +868,7 @@ - (void)_destroyCamera
return;

// current device KVO notifications
[self removeObserver:self forKeyPath:@"currentDevice.focusMode"];
[self removeObserver:self forKeyPath:@"currentDevice.adjustingFocus"];
[self removeObserver:self forKeyPath:@"currentDevice.adjustingExposure"];
[self removeObserver:self forKeyPath:@"currentDevice.adjustingWhiteBalance"];
@@ -1363,7 +1367,7 @@ - (void)_adjustFocusExposureAndWhiteBalance
if ([_delegate respondsToSelector:@selector(visionWillStartFocus:)])
[_delegate visionWillStartFocus:self];

CGPoint focusPoint = CGPointMake(0.5f, 0.5f);
CGPoint focusPoint = _locked ? _currentDevice.focusPointOfInterest : CGPointMake(0.5f, 0.5f);
[self focusAtAdjustedPointOfInterest:focusPoint];
}

@@ -2442,7 +2446,11 @@ - (void)_deviceSubjectAreaDidChange:(NSNotification *)notification

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ( context == (__bridge void *)PBJVisionFocusObserverContext ) {
if ( context == (__bridge void *)PBJVisionFocusModeObserverContext ) {
PBJFocusMode newMode = (PBJFocusMode)[[change objectForKey:NSKeyValueChangeNewKey] integerValue];
_focusMode = newMode;

} else if ( context == (__bridge void *)PBJVisionFocusObserverContext ) {

[self _enqueueBlockOnMainQueue:^{
BOOL isFocusing = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
@@ -2728,4 +2736,23 @@ - (void)_destroyGL
}
}

- (CGFloat)videoZoomFactor {
return _currentDevice.videoZoomFactor;
}

- (CGPoint)focusPoint {
return _currentDevice.focusPointOfInterest;
}

- (void)setVideoZoomFactor:(CGFloat)videoZoomFactor {
NSError *error = nil;
if ([_currentDevice lockForConfiguration:&error]) {
CGFloat factor = MAX(1, MIN(videoZoomFactor, _currentDevice.activeFormat.videoMaxZoomFactor));
_currentDevice.videoZoomFactor = factor;
[_currentDevice unlockForConfiguration];
} else {
NSLog(@"error: %@", error);
}
}

@end

0 comments on commit 8b4ed34

Please sign in to comment.
You can’t perform that action at this time.