启动时禁用相机焦点

时间:2012-10-30 14:21:19

标签: ios ios6 avfoundation camera-calibration

当我使用iOS 6在iPad 3上启动相机时,相机会进行对焦操作。这就是我使用AVFoundation框架的方式:\

//
// AppDelegate.h
//

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@property (strong, nonatomic) AVCaptureSession *session;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;
@property (strong, nonatomic) AVCaptureDevice *device;
@property (strong, nonatomic) AVCaptureDeviceInput *input;
@property(nonatomic, strong) AVCaptureStillImageOutput *stillImageOutput;

@end

//
//  AppDelegate.m
//


@implementation AppDelegate
@synthesize session;
@synthesize captureVideoPreviewLayer;
@synthesize device;
@synthesize input;
@synthesize stillImageOutput;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;

    if (([device hasMediaType:AVMediaTypeVideo]) &&
        ([device position] == AVCaptureDevicePositionBack) ) {
        [device lockForConfiguration:&error];
        if ([device isFocusModeSupported:AVCaptureFocusModeLocked]) {
            device.focusMode = AVCaptureFocusModeLocked;
            NSLog(@"Focus locked");
        }

        [device unlockForConfiguration];
    }

    session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetPhoto;

    captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    [captureVideoPreviewLayer setAffineTransform:CGAffineTransformMakeRotation(-M_PI / 2)];


    input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (!input) {
        // Handle the error appropriately.
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [session addInput:input];

    stillImageOutput = [[AVCaptureStillImageOutput alloc] init];

    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [stillImageOutput setOutputSettings:outputSettings];

    [session addOutput:stillImageOutput];
    [session startRunning];

    return YES;
}

我在启动时专门锁定AVDevicedevice.focusMode = AVCaptureFocusModeLocked;),但是当相机启动时,它会执行对焦操作。在我的应用中,出于相机校准的目的,我不希望发生这种情况。

如何避免在相机初始化时引发焦点?

1 个答案:

答案 0 :(得分:2)

-(void)disableAutoFocus
   {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    [device lockForConfiguration:nil];
    [device setTorchMode:AVCaptureTorchModeOff];
    [device setFlashMode:AVCaptureFlashModeOff];

     NSArray *devices = [AVCaptureDevice devices];
     NSError *error;
     for (AVCaptureDevice *device in devices) {
     if (([device hasMediaType:AVMediaTypeVideo]) &&
        ([device position] == AVCaptureDevicePositionBack) ) {
        [device lockForConfiguration:&error];
        if ([device isFocusModeSupported:AVCaptureFocusModeLocked]) {
            device.focusMode = AVCaptureFocusModeLocked;
            NSLog(@"Focus locked");
        }

        [device unlockForConfiguration];
    }
}

}