2016-11-25 63 views
0

我正在開發一個相機相關的應用程序。現在我成功捕捉了相機預覽。但是,當我嘗試將自動對焦設置到我的應用程序時,它不起作用。我嘗試了AVCaptureFocusModeContinuousAutoFocus和AVCaptureFocusModeAutoFocus,他們都沒有工作。順便說一下,我測試了iPhone 6s。爲什麼自動對焦不起作用?

我ViewController.h文件

#import <UIKit/UIKit.h> 
#include <AVFoundation/AVFoundation.h> 

@interface ViewController : UIViewController 
{ 
    AVCaptureSession *cameraCaptureSession; 
    AVCaptureVideoPreviewLayer *cameraPreviewLayer; 
} 

- (void) initializeCaptureSession; 

@end 

我ViewController.m文件

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    [self initializeCaptureSession]; 

} 

- (void) initializeCaptureSession 
{ 
    //Attempt to initialize AVCaptureDevice with back camera 
    NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; 
    AVCaptureDevice *captureDevice = nil; 
    for (AVCaptureDevice *device in videoDevices){ 
     if (device.position == AVCaptureDevicePositionBack) 
     { 
      captureDevice = device; 
      break; 
     } 
    } 

    //If camera is accessible by capture session 
    if (captureDevice) 
    { 
     if ([captureDevice isFocusModeSupported:AVCaptureFocusModeAutoFocus]){ 
      NSError *error; 
      if ([captureDevice lockForConfiguration:&error]){ 
       [captureDevice setFocusPointOfInterest:CGPointMake(0.5f, 0.5f)]; 
       [captureDevice setFocusMode:AVCaptureFocusModeAutoFocus]; 
       [captureDevice unlockForConfiguration]; 
      }else{ 
       NSLog(@"Error: %@", error); 
      } 
     } 


     //Allocate camera capture session 
     cameraCaptureSession = [[AVCaptureSession alloc] init]; 
     cameraCaptureSession.sessionPreset = AVCaptureSessionPresetMedium; 

     //Configure capture session input 
     AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:nil]; 
     [cameraCaptureSession addInput:videoIn]; 

     //Configure capture session output 
     AVCaptureVideoDataOutput *videoOut = [[AVCaptureVideoDataOutput alloc] init]; 
     [videoOut setAlwaysDiscardsLateVideoFrames:YES]; 
     [cameraCaptureSession addOutput:videoOut]; 


     //Bind preview layer to capture session data 
     cameraPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:cameraCaptureSession]; 
     CGRect layerRect = self.view.bounds; 
     cameraPreviewLayer.bounds = self.view.bounds; 
     cameraPreviewLayer.position = CGPointMake(CGRectGetMidX(layerRect), CGRectGetMidY(layerRect)); 
     cameraPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 

     //Add preview layer to UIView layer 
     [self.view.layer addSublayer:cameraPreviewLayer]; 

     //Begin camera capture 
     [cameraCaptureSession startRunning]; 

    } 

} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
    [cameraCaptureSession stopRunning]; 
} 


@end 

回答

0
AVCaptureSession *captureSession = [[AVCaptureSession alloc]init]; 
previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession]; 
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 

AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
NSError *err = nil; 
AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error: &err]; 
if (err == nil){ 
    if ([captureSession canAddInput:videoIn]){ 
     [captureSession addInput:videoIn]; 
    }else{ 
     NSLog(@"Failed to add video input"); 
    } 
}else{ 
    NSLog(@"Error: %@",err); 
} 
[captureSession startRunning]; 

previewLayer.frame = [self.view bounds]; 
[self.view.layer addSublayer:previewLayer]; 

我不知道發生了什麼,但如果我使用上面的代碼相機可以給我一個清晰的預覽。

0

這裏是我的SWIFT代碼,它的測試在我的iPhone 5S,只在背部攝像頭的工作,我不知道怎麼樣iPhone 6S

`if let device = captureDevice { 
     try! device.lockForConfiguration() 
     if device.isFocusModeSupported(.autoFocus) { 
      device.focusMode = .autoFocus 
     } 
     device.unlockForConfiguration() 
    }` 

我認爲你需要刪除

[captureDevice setFocusPointOfInterest:CGPointMake(0.5f, 0.5f)]; 
+0

其實即使我刪除它仍然沒有工作。我從stackoverflow答案之一,我無法找到目前的線程。無論如何,這是行不通的。 – OtakuFitness

0

嘗試一些事情是這樣的:

if ([self.session canAddInput:videoDeviceInput]) { 

     [[NSNotificationCenter defaultCenter] removeObserver:self name:AVCaptureDeviceSubjectAreaDidChangeNotification object:currentDevice]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subjectAreaDidChange:) name:AVCaptureDeviceSubjectAreaDidChangeNotification object:videoDevice]; 

     [self.session addInput:videoDeviceInput]; 
     self.videoDeviceInput = videoDeviceInput; 
    } 

然後:

- (void)subjectAreaDidChange:(NSNotification *)notification 
{ 
    CGPoint devicePoint = CGPointMake(0.5, 0.5); 
    [self focusWithMode:AVCaptureFocusModeContinuousAutoFocus exposeWithMode:AVCaptureExposureModeContinuousAutoExposure atDevicePoint:devicePoint monitorSubjectAreaChange:NO]; 
} 

然後輸入:

- (void)focusWithMode:(AVCaptureFocusMode)focusMode exposeWithMode:(AVCaptureExposureMode)exposureMode atDevicePoint:(CGPoint)point monitorSubjectAreaChange:(BOOL)monitorSubjectAreaChange 
{ 
    dispatch_async(self.sessionQueue, ^{ 
     AVCaptureDevice *device = self.videoDeviceInput.device; 
     NSError *error = nil; 
     if ([device lockForConfiguration:&error]) { 
      // Setting (focus/exposure)PointOfInterest alone does not initiate a (focus/exposure) operation. 
      // Call -set(Focus/Exposure)Mode: to apply the new point of interest. 
      if (device.isFocusPointOfInterestSupported && [device isFocusModeSupported:focusMode]) { 
       device.focusPointOfInterest = point; 
       device.focusMode = focusMode; 
      } 

      if (device.isExposurePointOfInterestSupported && [device isExposureModeSupported:exposureMode]) { 
       device.exposurePointOfInterest = point; 
       device.exposureMode = exposureMode; 
      } 

      device.subjectAreaChangeMonitoringEnabled = monitorSubjectAreaChange; 
      [device unlockForConfiguration]; 
     } 
     else { 
      NSLog(@"Could not lock device for configuration: %@", error); 
     } 
    }); 
} 

這是從一個項目,我的工作守則。隨意問你是否有任何疑問。

相關問題