2013-04-05 30 views
8

我使用基于Apple AppCam应用程序示例的AVCaptureSession来克隆Apple相机应用程序。 问题是我无法在视频预览屏幕中看到焦点矩形。 我用下面的代码来设置焦点,但仍然没有显示焦点矩形。iphone相机显示焦点矩形

AVCaptureDevice *device = [[self videoInput] device]; 
if ([device isFocusModeSupported:focusMode] && [device focusMode] != focusMode) { 
    NSError *error; 

     printf(" setFocusMode \n"); 
    if ([device lockForConfiguration:&error]) { 
     [device setFocusMode:focusMode]; 
     [device unlockForConfiguration]; 
    } else { 
     id delegate = [self delegate]; 
     if ([delegate respondsToSelector:@selector(acquiringDeviceLockFailedWithError:)]) { 
      [delegate acquiringDeviceLockFailedWithError:error]; 
     } 
    }  
} 

当我使用的UIImagePickerController,支持自动对焦,点对焦默认支持,并且可以看到焦点矩形。 有没有办法使用AVCaptureSession在视频预览图层中显示焦点矩形?

+0

嗯,似乎没有人知道THI秒。 – ttotto 2013-04-05 18:12:15

回答

11

焦点动画是一个完整的自定义动画,你必须自己创建。我目前有像你一样的问题: 我想在用户点击预览图层后显示矩形作为反馈。

你想要做的就是实现点触对焦的第一件事情,可能在您启动预览层:

UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToFocus:)]; 
[tapGR setNumberOfTapsRequired:1]; 
[tapGR setNumberOfTouchesRequired:1]; 
[self.captureVideoPreviewView addGestureRecognizer:tapGR]; 

现在实现点触对焦方式本身:

-(void)tapToFocus:(UITapGestureRecognizer *)singleTap{ 
    CGPoint touchPoint = [singleTap locationInView:self.captureVideoPreviewView]; 
    CGPoint convertedPoint = [self.captureVideoPreviewLayer captureDevicePointOfInterestForPoint:touchPoint]; 
    AVCaptureDevice *currentDevice = currentInput.device; 

    if([currentDevice isFocusPointOfInterestSupported] && [currentDevice isFocusModeSupported:AVCaptureFocusModeAutoFocus]){ 
     NSError *error = nil; 
     [currentDevice lockForConfiguration:&error]; 
     if(!error){ 
      [currentDevice setFocusPointOfInterest:convertedPoint]; 
      [currentDevice setFocusMode:AVCaptureFocusModeAutoFocus]; 
      [currentDevice unlockForConfiguration]; 
     }  
    } 
} 

最后一件事,我还没有实现自己呢,是将聚焦动画添加到预览图层或更确切地说是持有预览图层的视图控制器。我相信这可以在tapToFocus:中完成。你已经有了接触点。只需添加一个动画图像视图或以触摸位置为中心的其他视图。动画完成后,移除图像视图。

+1

你也可以看看:http://stackoverflow.com/questions/15449271/avfoundation-tap-to-focus-feedback-rectangle 有一个很好的描述如何实现与UIView焦点动画 – xxtesaxx 2013-06-10 13:56:12

2

快速实施

手势:

private func focusGesture() -> UITapGestureRecognizer { 

    let tapRec: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(kTapToFocus)) 
    tapRec.cancelsTouchesInView = false 
    tapRec.numberOfTapsRequired = 1 
    tapRec.numberOfTouchesRequired = 1 

    return tapRec 
} 

操作:

private func tapToFocus(gesture : UITapGestureRecognizer) { 

    let touchPoint:CGPoint = gesture.locationInView(self.previewView) 
    let convertedPoint:CGPoint = previewLayer!.captureDevicePointOfInterestForPoint(touchPoint) 

    let currentDevice:AVCaptureDevice = videoDeviceInput!.device 

    if currentDevice.focusPointOfInterestSupported && currentDevice.isFocusModeSupported(AVCaptureFocusMode.AutoFocus){ 
     do { 
      try currentDevice.lockForConfiguration() 
      currentDevice.focusPointOfInterest = convertedPoint 
      currentDevice.focusMode = AVCaptureFocusMode.AutoFocus 
      currentDevice.unlockForConfiguration() 
     } catch { 

     } 
    } 

} 
0

swift3实施

lazy var focusGesture: UITapGestureRecognizer = { 
    let instance = UITapGestureRecognizer(target: self, action: #selector(tapToFocus(_:))) 
    instance.cancelsTouchesInView = false 
    instance.numberOfTapsRequired = 1 
    instance.numberOfTouchesRequired = 1 
    return instance 
}() 

func tapToFocus(_ gesture: UITapGestureRecognizer) { 
    guard let previewLayer = previewLayer else { 
     print("Expected a previewLayer") 
     return 
    } 
    guard let device = device else { 
     print("Expected a device") 
     return 
    } 

    let touchPoint: CGPoint = gesture.location(in: cameraView) 
    let convertedPoint: CGPoint = previewLayer.captureDevicePointOfInterest(for: touchPoint) 
    if device.isFocusPointOfInterestSupported && device.isFocusModeSupported(AVCaptureFocusMode.autoFocus) { 
     do { 
      try device.lockForConfiguration() 
      device.focusPointOfInterest = convertedPoint 
      device.focusMode = AVCaptureFocusMode.autoFocus 
      device.unlockForConfiguration() 
     } catch { 
      print("unable to focus") 
     } 
    } 
}