1

Using the AVFoundation framework, I have tap to focus using this code:

- (void) autoFocusAtPoint:(CGPoint)point{

NSArray *devices = [AVCaptureDevice devices];    


    for (AVCaptureDevice *device in devices) {
        [device unlockForConfiguration];

        if ([device hasMediaType:AVMediaTypeVideo]) {

            if([device isFocusPointOfInterestSupported] && [device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {

                if([device lockForConfiguration:nil]) {

                    [device setFocusPointOfInterest:point];
                    [device setFocusMode:AVCaptureFocusModeAutoFocus];
                    [device setExposurePointOfInterest:point];
                    [device setExposureMode:AVCaptureExposureModeContinuousAutoExposure];

                    [device lockForConfiguration:nil];

                }
            }
        }
    }

}

This should go through both the front and back camera devices and focus them. It does go through each device but for the front camera it does not get past the isFocusPointOfInterestSupported line. But you are able to tap to focus on the front camera using an iPhone 5. At least you are able to on the camera app. So what is the problem???

Thanks for the help

evenodd
  • 2,026
  • 4
  • 26
  • 38
  • seems front camera does not support to focus, at least you can't do it on iphone 4,4s , only back camera is avaliable – adali Jun 21 '13 at 01:24

2 Answers2

0

I'm not sure if this is the right answer but the method [device setFocusPointOfInterest:point]; takes a CGPoint takes a point ranging from (0,0) to (1,1) where (0,0) is the top left and (1,1) is the bottom right. If you're passing a CGPoint to this method straight from the UIView, it won't focus on the proper point. I suggest you add this:

- (CGPoint)convertToPointOfInterestFromViewCoordinates:(CGPoint)viewCoordinates {
CGPoint pointOfInterest = CGPointMake(.5f, .5f);
CGSize frameSize = [[self videoPreviewView] frame].size;

AVCaptureVideoPreviewLayer *videoPreviewLayer = [self previewLayer];

if ([[videoPreviewLayer videoGravity] isEqualToString:AVLayerVideoGravityResize] ) {
    pointOfInterest = CGPointMake(viewCoordinates.y / frameSize.height, 1.f - (viewCoordinates.x / frameSize.width));
} else {
    CGRect cleanAperture;
    for (AVCaptureInputPort *port in self.visualInput.ports) {
        if ([port mediaType] == AVMediaTypeVideo) {
            cleanAperture = CMVideoFormatDescriptionGetCleanAperture([port formatDescription], YES);
            CGSize apertureSize = cleanAperture.size;
            CGPoint point = viewCoordinates;

            CGFloat apertureRatio = apertureSize.height / apertureSize.width;
            CGFloat viewRatio = frameSize.width / frameSize.height;
            CGFloat xc = .5f;
            CGFloat yc = .5f;

            if ([[videoPreviewLayer videoGravity] isEqualToString:AVLayerVideoGravityResizeAspect] ) {
                if (viewRatio > apertureRatio) {
                    CGFloat y2 = frameSize.height;
                    CGFloat x2 = frameSize.height * apertureRatio;
                    CGFloat x1 = frameSize.width;
                    CGFloat blackBar = (x1 - x2) / 2;
                    if (point.x >= blackBar && point.x <= blackBar + x2) {
                        xc = point.y / y2;
                        yc = 1.f - ((point.x - blackBar) / x2);
                    }
                } else {
                    CGFloat y2 = frameSize.width / apertureRatio;
                    CGFloat y1 = frameSize.height;
                    CGFloat x2 = frameSize.width;
                    CGFloat blackBar = (y1 - y2) / 2;
                    if (point.y >= blackBar && point.y <= blackBar + y2) {
                        xc = ((point.y - blackBar) / y2);
                        yc = 1.f - (point.x / x2);
                    }
                }
            } else if ([[videoPreviewLayer videoGravity] isEqualToString:AVLayerVideoGravityResizeAspectFill]) {
                if (viewRatio > apertureRatio) {
                    CGFloat y2 = apertureSize.width * (frameSize.width / apertureSize.height);
                    xc = (point.y + ((y2 - frameSize.height) / 2.f)) / y2;
                    yc = (frameSize.width - point.x) / frameSize.width;
                } else {
                    CGFloat x2 = apertureSize.height * (frameSize.height / apertureSize.width);
                    yc = 1.f - ((point.x + ((x2 - frameSize.width) / 2)) / x2);
                    xc = point.y / frameSize.height;
                }

            }

            pointOfInterest = CGPointMake(xc, yc);
            break;
        }
    }
}

return pointOfInterest;

}

to get the correct CGPoint just call [self convertToPointOfInterestFromViewCoordinates:point]; and it will return a CGPoint ranging from (0,0) to (1,1) which you can use for [device setFocusPointOfInterest:point];

yliu1021
  • 94
  • 5
0

i don't know whether iPhone 5 support front camera focus , but according to the official doc

https://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html#//apple_ref/doc/uid/TP40010188-CH5-SW2

the iPhone 4's front camera have no focus mode. maybe iPhone 4S either

adali
  • 5,977
  • 2
  • 33
  • 40
  • It doesn't I realized. Even though you can tap on it and a blue square shows up, it actually only changes the exposure. – evenodd Jun 21 '13 at 17:24