2013-04-05 6 views
8

मैं ऐप्पल के ऐपकैम ऐप नमूने के आधार पर एवी कैप्चर सत्र का उपयोग कर ऐप्पल के कैमरा ऐप को क्लोन कर रहा हूं। समस्या यह है कि मैं वीडियो पूर्वावलोकन स्क्रीन में फोकस आयताकार नहीं देख सकता। मैंने फोकस सेट करने के लिए निम्नलिखित कोड का उपयोग किया, लेकिन अभी भी आयताकार फोकस नहीं दिखाया गया है।आईफोन कैमरा शो फोकस आयताकार

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

हम्म, यह कोई भी नहीं लगता थी पता रों। – 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]; 

अब टैप-टू-फोकस विधि ही लागू:

पहली बात तुम क्या करने के लिए टैप फोकस लागू कर रहा है चाहता हूँ, शायद तुम कहाँ पूर्वावलोकन परत आरंभ

आखिरी बात, जिसे मैंने अभी तक अपने द्वारा लागू नहीं किया है, फोकस करने वाली एनीमेशन को पूर्वावलोकन परत में या दृश्य नियंत्रक को देखने के लिए है जो पूर्वावलोकन परत धारण कर रहा है। मेरा मानना ​​है कि टैप टॉफोकस में किया जा सकता है:। वहां आपके पास पहले से ही टच पॉइंट है। बस एक एनिमेटेड छवि दृश्य या कुछ अन्य दृश्य जोड़ें जिसमें स्पर्श केंद्र की केंद्र है। एनीमेशन समाप्त होने के बाद, छवि दृश्य को हटा दें।

+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") 
     } 
    } 
} 
संबंधित मुद्दे