iPhone相机显示焦点矩形

标签 iphone avcapturesession autofocus

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

最佳答案

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

您要做的第一件事是实现点击聚焦,可能是您启动预览层的地方:

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];
        }    
    }
}

最后一件事,我自己还没有实现,是将聚焦动画添加到预览层,或者更确切地说,添加到保存预览层的 View Controller 。我相信这可以在 tapToFocus: 中完成。在那里你已经有了接触点。只需添加动画 ImageView 或以触摸位置为中心的其他 View 即可。动画完成后,删除 ImageView 。

关于iPhone相机显示焦点矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15838443/

相关文章:

iphone - 打开 App Store 以在我的应用程序中评分

ios - 录制视频/音频时播放系统声音

objective-c - CoreML 使用 AVCaptureSession 返回不同的值

vue.js - 当父节点中有很多 v-if 时,自定义 v-focus 不起作用

accessibility - 自动对焦表单属性是否违反可访问性标准?

iphone - 应用程序退出并返回主屏幕,可以尝试+捕获处理吗?

iphone - 我想在我的应用程序中调用 DropBox Api 来上传文件并下载

iphone - 锁定 UIDatePicker 列

ios - 扫描二维码经常崩溃-AVCaptureSession

input - 如何专注于从 Svelte 组件加载的输入字段?