-->

如何在重点iPhone SDK自动拍照?(How to take a photo automatic

2019-07-30 08:53发布

我创建在用户需要带文本的图像的画面并上传到服务器的应用程序。 我已经使用AVCaptureSession打开相机,并放置在栏按钮捕捉最新的框架,并将其上传到服务器。 在这个应用程序,用户可以通过点击该栏按钮由一个发送多个图片的服务器之一。

我想知道是否有可能是用户没有按按钮来拍摄一帧。 这可能是当前帧,支持自动对焦自动捕获? 像用户只需放置相机的图像上的第二和图像倍受关注的时候,它会自动捕获,使用户可以移动到下一个图像,而无需按任何按钮?

我的代码来捕获框架如下:

- (void)initCapture {

    AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput 
                                          deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] 
                                          error:nil];

    AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];

    captureOutput.alwaysDiscardsLateVideoFrames = YES; 







    /*We create a serial queue to handle the processing of our frames*/
    dispatch_queue_t queue;
    queue = dispatch_queue_create("cameraQueue", NULL);
    [captureOutput setSampleBufferDelegate:self queue:queue];
    dispatch_release(queue);
    // Set the video output to store frame in BGRA (It is supposed to be faster)
    NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; 
    NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]; 
    NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key]; 
    [captureOutput setVideoSettings:videoSettings]; 
    /*And we create a capture session*/
    captureSession = [[AVCaptureSession alloc] init];
    /*We add input and output*/
    [captureSession addInput:captureInput];
    [captureSession addOutput:captureOutput];





    AVCaptureConnection *conn = [captureOutput connectionWithMediaType:AVMediaTypeVideo];

    if (conn.supportsVideoMinFrameDuration)
        conn.videoMinFrameDuration = CMTimeMake(5,1);
    if (conn.supportsVideoMaxFrameDuration)
        conn.videoMaxFrameDuration = CMTimeMake(5,1);





    [captureSession setSessionPreset:AVCaptureSessionPresetPhoto];

    customLayer = [CALayer layer];
    customLayer.frame = self.view.bounds;
    customLayer.transform = CATransform3DRotate(CATransform3DIdentity, M_PI/2.0f, 0, 0, 1);
    customLayer.contentsGravity = kCAGravityResizeAspectFill;
    //[self.view.layer addSublayer:customLayer];
    /*We add the imageView*/



    imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(0, 0, 100, 100);
    //imageView.frame = self.view.bounds;
    [self.view addSubview:imageView];



    [captureSession startRunning];

}

我将是你的帮助表示感谢。 谢谢。

Answer 1:

你必须添加adjustingFocus观测到您的视频输入:

[[[captureManager videoInput] device] addObserver:self forKeyPath:@"adjustingFocus" options:NSKeyValueObservingOptionNew context:nil];

然后添加observeValueForKeyPath方法,该方法当调整聚焦完成相片:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if( [keyPath isEqualToString:@"adjustingFocus"] ){
    BOOL adjustingFocus = [ [change objectForKey:NSKeyValueChangeNewKey] isEqualToNumber:[NSNumber numberWithInt:1] ];

    if (!adjustingFocus) {
             [[self captureManager] captureStillImage:self];
        }
    }
}


文章来源: How to take a photo automatically at focus in iPhone SDK?