2013-04-05 12 views
8

AppleのAppCamアプリサンプルに基づいてAVCaptureSessionを使用してAppleのカメラアプリを複製しています。 問題は、ビデオプレビュー画面でフォーカス矩形が見えないことです。 次のコードを使用してフォーカスを設定しましたが、フォーカス矩形は表示されません。iphone cameraフォーカス矩形を表示

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

うーん、誰もがTHIを知らないようですs。 – ttotto

答えて

11

フォーカスアニメーションは、自分で作成しなければならない完全なカスタムアニメーションです。私は現在、あなたのような正確に同じ問題を抱えています: プレビューレイヤーをタップした後、ユーザーのフィードバックとして長方形を表示したいと思います。

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

UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToFocus:)]; 
[tapGR setNumberOfTapsRequired:1]; 
[tapGR setNumberOfTouchesRequired:1]; 
[self.captureVideoPreviewView addGestureRecognizer:tapGR]; 

今すぐタップ・ツー・フォーカス方式自体を実装:

あなたは、タップ・ツー・フォーカスを実施してやりたいまず最初は、おそらくあなたは、プレビュー層を開始する場所

私がまだ自分で実装していない最後のものは、プレビューレイヤーまたはプレビューレイヤーを保持しているビューコントローラーにフォーカシングアニメーションを追加することです。私はそれがtapToFocus:でできると信じています。あなたはすでにタッチポイントを持っています。タッチ位置を中心としたアニメーション画像ビューまたはその他のビューを追加するだけです。アニメーションが終了したら、イメージビューを削除します。

+1

また見てみることもできます:http://stackoverflow.com/questions/15449271/avfoundation-tap-to-focus-feedback-rectangle UIViewでフォーカスアニメーションを実装する方法についての良い説明があります – xxtesaxx

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 { 

     } 
    } 

} 
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") 
     } 
    } 
} 
関連する問題