【问题标题】:iphone camera show focus rectangleiphone相机显示焦点矩形
【发布时间】:2013-04-05 16:00:53
【问题描述】:

我正在使用基于 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 在视频预览层显示焦点矩形?

【问题讨论】:

  • 嗯,这个好像没人知道。

标签: iphone avcapturesession autofocus


【解决方案1】:

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

您要做的第一件事是实现点击对焦,可能是您启动预览层的位置:

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

【讨论】:

【解决方案2】:

Swift 实现

手势:

 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 {

        }
    }

}

【讨论】:

    【解决方案3】:

    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")
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 2021-11-08
      • 1970-01-01
      • 2015-09-05
      相关资源
      最近更新 更多