10

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

  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 在视频预览层中显示焦点矩形?

4

3 回答 3

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: 中完成。你已经有了接触点。只需添加一个动画图像视图或其他以触摸位置为中心的视图。动画完成后,移除图像视图。

于 2013-06-10T13:28:47.407 回答
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 {

        }
    }

}
于 2016-03-31T07:17:12.993 回答
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")
        }
    }
}
于 2016-11-17T11:49:13.823 回答