2013-04-05 26 views
8

Apple'ın kamera uygulaması Apple'ın AppCam uygulaması örneğini temel alarak AVCaptureSession kullanarak klon ediyorum. Sorun, video önizleme ekranında odak dikdörtgenini göremiyorum. Odağı ayarlamak için aşağıdaki kodu kullandım, ancak yine de odak dikdörtgen görünmüyor. Ben UIImagePickerController, otomatik odak kullandığınızdaiphone kamera show odak dikdörtgen

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

dokunun odak varsayılan olarak desteklenir ve odak dikdörtgeni görebilirsiniz. AVCaptureSession kullanarak video önizleme katmanında odak dikdörtgenini göstermenin bir yolu yok mu?

+0

hmm kimsenin görünüyor thi biliyorum s. – ttotto

cevap

11

Odak animasyon, kendi başınıza oluşturmanız gereken eksiksiz bir özel animasyondur. Şu anda sizin gibi aynı sorunu yaşıyorum: Önizleme katmanına dokunduktan sonra kullanıcı için bir geri bildirim olarak dikdörtgeni göstermek istiyorum.

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

Şimdi için hafifçe vurma odak yöntemini kendisi uygulamak:

Eğer için hafifçe vurma odağı uyguluyor yapmak istediğim ilk şey, muhtemelen önizleme katmanını başlatmak nereye

Henüz kendim tarafından uygulanmadığım son şey, önizleme katmanını önizleme katmanına ya da önizleme katmanını tutan görünüm denetleyicisine eklemektir. TapToFocus'da yapılabileceğine inanıyorum. Orada zaten temas noktan var. Animasyonlu bir görüntü görünümü veya merkez olarak dokunmatik konuma sahip başka bir görünüm eklemeniz yeterlidir. Animasyon bittikten sonra, görüntü görünümünü kaldırın.

+1

Ayrıca aşağıdaki adrese de bakabilirsiniz: http://stackoverflow.com/questions/15449271/avfoundation-tap-to-focus-feedback-rectangle Odak animasyonunun UIView ile nasıl uygulanacağı hakkında iyi bir açıklama var. – xxtesaxx

2

Swift uygulama

Gesture:

private func focusGesture() -> UITapGestureRecognizer { 

    let tapRec: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(kTapToFocus)) 
    tapRec.cancelsTouchesInView = false 
    tapRec.numberOfTapsRequired = 1 
    tapRec.numberOfTouchesRequired = 1 

    return tapRec 
} 

Eylem:

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 uygulaması

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") 
     } 
    } 
}