iPhone相机聚焦

时间:2011-11-09 13:02:41

标签: iphone objective-c avcapturesession avcapturedevice avcam

我使用以下代码聚焦iphone相机。但它没有用。我从Apple的AVCam示例代码中获取此代码。我做错了吗?有什么办法吗? 检测iPhone是否集中注意力?

-(void) focusAtPoint:(CGPoint)point   
{
    AVCaptureDevice *device =  [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];;

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

            NSError *error;

            if ([device lockForConfiguration:&error]) {             
                [device setFocusPointOfInterest:point];             
                [device setFocusMode:AVCaptureFocusModeAutoFocus];              
                [device unlockForConfiguration];                
            } else {                
                NSLog(@"Error in Focus Mode");
            }        

        }
    }
}

1 个答案:

答案 0 :(得分:7)

[AVCaptureDevice setFocusPointOfInterest:]的点与视图的触点不同,而是0,0和1,1之间的点(参见文档)。 所以你必须这样做:

CGPoint point = [touch locationInView:self.cameraView];
point.x /= self.cameraView.frame.size.width;
point.y /= self.cameraView.frame.size.height;
[self focusAtPoint:point];