我正在使用基于 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/