我正在使用基于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在视频预览图层中显示焦点矩形?
答案 0 :(得分:11)
焦点动画是一个完整的自定义动画,您必须自己创建。我目前遇到的问题和你一样: 我想在点击预览图层后显示一个矩形作为用户的反馈。
您要做的第一件事是实现点按焦点,可能是您启动预览图层的地方:
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToFocus:)];
[tapGR setNumberOfTapsRequired:1];
[tapGR setNumberOfTouchesRequired:1];
[self.captureVideoPreviewView addGestureRecognizer:tapGR];
现在实现tap-to-focus方法:
-(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 :(得分: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 {
}
}
}
答案 2 :(得分:0)
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")
}
}
}