2014-12-06 108 views
0

这对于那些在iPhone上获得一致的校准参数时遇到困难的人是很困难的。旧技巧是将手机对准无穷远,这样您就可以确保获得一致的相机矩阵而无需重新校准。设置iPhone镜头对焦位置

所以问题是怎么没有一种方法可以让你手动设置我们的尝试计算机视觉应用程序的重点。

回答

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); 

由苹果API在iOS8上提供让你手动设置对焦镜头位置的方法。

一些快速启动代码

-(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]; 
     } 
    } 
}