Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect When AVFoundation Camera Finishes Focusing

I currently am using AVFoundation to run my camera, but I want to be able to detect when the camera finishes focusing. Is there any way to do this?

Thanks so much!!

like image 366
Zachary Christopoulos Avatar asked Jan 29 '13 23:01

Zachary Christopoulos


2 Answers

According to doc

You can use the adjustingFocus property to determine whether a device is currently focusing. You can observe the property using key-value observing to be notified when a device starts and stops focusing.

If you change the focus mode settings, you can return them to the default configuration as follows:

like image 186
Shubhank Avatar answered Nov 15 '22 14:11

Shubhank


In the initialisation:

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

and then:

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

        if (adjustingFocus) {
             focusing=true;
        }
        else {
           if (focusing) {
                focusing = false;
                finishedFocus = true;
           }
        }
    }
}
like image 43
Alexi Akl Avatar answered Nov 15 '22 14:11

Alexi Akl