0

当我在装有 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;
}

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

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

4

1 回答 1

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

}

于 2013-10-30T05:09:19.617 回答