0

I want to create focus functionality just like the native camera app using AVCam, but unable to create the same. i am using the above code:

if ([[[captureManager videoInput] device] isFocusPointOfInterestSupported]) {
        CGPoint tapPoint = [tgr locationInView:[self videoPreviewView]];
        CGPoint convertedFocusPoint = [self convertToPointOfInterestFromViewCoordinates:tapPoint];
        [self addLayoutsOfAutoFocus:tapPoint];
        [USERDEFAULTS setValue:[NSString stringWithFormat:@"%f",convertedFocusPoint.x] forKey:x_value];
        [USERDEFAULTS setValue:[NSString stringWithFormat:@"%f",convertedFocusPoint.y] forKey:y_value];
        [USERDEFAULTS synchronize];
        [captureManager continuousFocusAtPoint:convertedFocusPoint];
        [captureManager continuousExpousureAtPoint:convertedFocusPoint];

    }

i am trying to save the focus point, but next time when i reload the camera the focus is lost.

Please help.

Mohit Manhas
  • 3,471
  • 1
  • 17
  • 21

1 Answers1

1

I have found the solution for this. I am posting here for future references. The trick i used is to setup the session again with new points where the user taps. Here is the code;

- (void)tapToContinouslyAutoFocus:(UIGestureRecognizer *)tgr
{
if ([[[captureManager videoInput] device] isFocusPointOfInterestSupported]) {
        CGPoint tapPoint = [tgr locationInView:[self videoPreviewView]];
        CGPoint convertedFocus = [self convertToPointOfInterestFromViewCoordinates:tapPoint];
        CGPoint convertedFocusPoint=CGPointMake(convertedFocus.y, convertedFocus.x);
        [self addLayoutsOfAutoFocus:tapPoint];
        [USERDEFAULTS setValue:[NSString stringWithFormat:@"%f",convertedFocusPoint.x] forKey:x_value];
        [USERDEFAULTS setValue:[NSString stringWithFormat:@"%f",convertedFocusPoint.y] forKey:y_value];
        [USERDEFAULTS synchronize];

        [[self captureManager] setSession:nil];
        [[self captureManager] setupSession];
        [captureManager autoFocusAtPoint:convertedFocusPoint];
        [captureManager autoExposureAtPoint:convertedFocusPoint];

    }


}

The above method is called when user double taps the screen to focus.

Mohit Manhas
  • 3,471
  • 1
  • 17
  • 21