Ios autofucus or continuous focus

#1

Hi, I hav an app that works fine but I need to add autofocus, or at least refocus. I dont mind if the user has to tap to trigger focus, or if it is constant autofocus.

I saw a few posts, the only recent ones I saw are:


and

I did not have any luck makign this work with an iphone 6.

Has anyone out there got a solution?

One road I started down is to try and make it myself with objective C, how could I get access to the captureInput.device (the avfoundation grabber) from my ofApp.cpp to apply focus?

#2

When I made artgram (https://itunes.apple.com/sa/app/artgram/id1251985477) I modified few OF classes to accept focus commands.

First of all, you can both set the focus to a specific distance (number between 0.0 and 1.0 where 0.0 is the closest distance and 1.0 is the furthest), or you can specify a set of XY coordinates for a location in your picture to set the focus point.

I added the following methods to
AVFoundationVideoGrabber

-(void)setAutofocusWithPointOfInterest:(CGPoint) p{

[captureInput.device setFocusMode:AVCaptureFocusModeAutoFocus];
[captureInput.device lockForConfiguration:nil];

[captureInput.device setFocusPointOfInterest:p];
[captureInput.device unlockForConfiguration];

}
-(void)setFocusManuallyTo:(float)newFocus{

 [captureInput.device lockForConfiguration:nil];
 [captureInput.device setFocusModeLockedWithLensPosition:newFocus completionHandler:^(CMTime syncTime) {     }];
 [captureInput.device unlockForConfiguration];

}

And I modiefied ofxiOSVideoGrabber to have the following methods:

void ofxiOSVideoGrabber::setAutofocusWithPointOfInterest(int x, int y){

grabber->setAutofocusWithPointOfInterest(CGPointMake(x,y));

}

void ofxiOSVideoGrabber::setFocusManuallyTo(float newFocus){

grabber->setFocusManuallyTo(newFocus);

}

So from OF you can just call:

videoFeed.getGrabber<ofxiOSVideoGrabber>()->setAutofocusWithPointOfInterest(0, 0);

or

videoFeed.getGrabber<ofxiOSVideoGrabber>()->setFocusManuallyTo(0.5);

1 Like
Drawing a custom overlay on iOS Camera [SOLVED]
#3

Hey thanks that seems to work pretty well. In case anyone else sees this I had to wrap the objective C functions in AVFoundationVideoGrabber with C++ functions.

One thing that setFocusPointOfInterest needs a float between 0 and 1 for the position.

1 Like