4

I am creating an app in which user takes the picture of a image with text and upload to server. I have used AVCaptureSession to open camera and placed a bar button which captures the latest frame and uploads it to the server. In this app, user can send multiple images to the server one by one by clicking the bar button.

I was wondering if it was possible that user does not have to press the button to capture a frame. Is this possible that current frame is captured automatically with auto focus? Like user just places the camera for a second on the image and when the image is well-focused, it is automatically captured so that user can move to next image without having to press any button?

My code to capture frame is as follows:

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

}

I will be grateful for your help. Thanks.

1
  • Did you get this working? I need to auto capture an image as soon as the criteria is fulfilled in a region of interest. So, if I understand this correctly, I need to add AVCaptureVideoDataOutput to receive camera information and use the adjustingFocus observer to captureStillImage. Do you have any idea how I could define the region of interest (a sub section of the view) and ignore the rest of the view?
    – Patricia
    Sep 4, 2014 at 17:26

1 Answer 1

2

You have to add adjustingFocus observer to your video input:

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

and then add the observeValueForKeyPath method that takes a photo when adjusting focus is finished:

- (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];
        }
    }
}
1
  • Where is captureManager declared ?
    – Biranchi
    May 21, 2015 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.