这适用于在iPhone上获得一致校准参数时遇到问题的所有人。最常见的技巧是将手机对准无限远,这样您就可以确保获得一致的相机矩阵而无需重新校准。
所以问题是如何找到一种方法,让您手动设置焦点,让我们尝试计算机视觉应用。
答案 0 :(得分:0)
/*!
@method setFocusModeLockedWithLensPosition:completionHandler:
@abstract
Sets focusMode to AVCaptureFocusModeLocked and locks lensPosition at an explicit value.
@param lensPosition
The lens position, as described in the documentation for the lensPosition property. A value of AVCaptureLensPositionCurrent can be used
to indicate that the caller does not wish to specify a value for lensPosition.
@param handler
A block to be called when lensPosition has been set to the value specified and focusMode is set to AVCaptureFocusModeLocked. If
setFocusModeLockedWithLensPosition:completionHandler: is called multiple times, the completion handlers will be called in FIFO order.
The block receives a timestamp which matches that of the first buffer to which all settings have been applied. Note that the timestamp
is synchronized to the device clock, and thus must be converted to the master clock prior to comparison with the timestamps of buffers
delivered via an AVCaptureVideoDataOutput. The client may pass nil for the handler parameter if knowledge of the operation's completion
is not required.
@discussion
This is the only way of setting lensPosition.
This method throws an NSRangeException if lensPosition is set to an unsupported level.
This method throws an NSGenericException if called without first obtaining exclusive access to the receiver using lockForConfiguration:.
*/
- (void)setFocusModeLockedWithLensPosition:(float)lensPosition completionHandler:(void (^)(CMTime syncTime))handler NS_AVAILABLE_IOS(8_0);
iOS 8中由Apple API提供的方法,可让您手动设置对焦镜头位置。
-(IBAction)focusAndLock:(id)sender
{
if (self.isFocusLocked)
{
NSLog(@"unlock the lens");
[[[self videoDeviceInput] device] setExposureMode:AVCaptureExposureModeAutoExpose];
[[[self videoDeviceInput] device] setFocusMode:AVCaptureFocusModeAutoFocus];
[self.focusAndLock setTitle:@"set Focus lock" forState:UIControlStateNormal];
[self.focusAndLock setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
self.isFocusLocked = NO;
}
else
{
NSLog(@"lock the lens");
NSError *error;
if ([[[self videoDeviceInput] device] lockForConfiguration:&error])
{
[[[self videoDeviceInput] device] setFocusModeLockedWithLensPosition:[[self videoDeviceInput] device].lensPosition completionHandler:nil];
[[[self videoDeviceInput] device] setExposureMode:AVCaptureExposureModeLocked];
[[[self videoDeviceInput] device] setFocusMode:AVCaptureFocusModeLocked];
[self.focusAndLock setTitle:[NSString stringWithFormat:@"FOCUS LOCKED: %f",[[self videoDeviceInput] device].lensPosition] forState:UIControlStateNormal];
[self.focusAndLock setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
self.isFocusLocked = YES;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *selectedPath = [[NSURL alloc] initFileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"focusData.plist"]];
NSMutableDictionary *focusDict = [[NSMutableDictionary alloc] init];
[focusDict setValue:[NSNumber numberWithFloat:[[self videoDeviceInput] device].lensPosition] forKey:@"focusLock"];
[focusDict writeToURL:selectedPath atomically:YES];
}
}
}