2013-04-05 17 views
8

Klonuję aplikację aparatu firmy Apple przy użyciu AVCaptureSession na podstawie próbki aplikacji AppCam firmy Apple. Problem polega na tym, że nie widzę prostokąta skupienia na ekranie podglądu wideo. Użyłem następującego kodu do ustawiania ostrości, ale nadal ustawiono prostokąt focusowy.Kamera iPhone Focus pokazowa prostokąt

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

Gdy używam UIImagePickerController, auto focus, dotknij ostrości są obsługiwane domyślnie i widać ramkę aktywności. Czy nie można wyświetlić prostokąta skupienia w warstwie podglądu wideo za pomocą programu AVCaptureSession?

+0

hmm, wydaje się, nikt wiedzieć Thi s. – ttotto

Odpowiedz

11

Animacja ostrości to kompletna niestandardowa animacja, którą trzeba utworzyć samodzielnie. Obecnie mam dokładnie taki sam problem jak ty: Chcę pokazać prostokąt jako informację zwrotną dla użytkownika po dotknięciu warstwy podglądu.

Pierwszą rzeczą, którą chcesz zrobić, to wdrożenie ostrość tap-to-prawdopodobnie gdzie inicjowania warstwę podglądu

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

Teraz wdrożenia metody tap-to-focus sam:

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

Ostatnią rzeczą, której jeszcze nie wdrożyłem, jest dodanie animacji skupienia do warstwy podglądu lub raczej kontrolera widoku, który trzyma warstwę podglądu. Wierzę, że można to zrobić w tapToFocus :. Tam już masz punkt kontaktu. Po prostu dodaj animowany widok obrazu lub inny widok, który ma środkową pozycję dotykową. Po zakończeniu animacji usuń widok obrazu.

+1

Możesz również rzucić okiem na: http://stackoverflow.com/questions/15449271/avfoundation-tap-to-focus-feedback-rectangle Istnieje dobry opis, jak zaimplementować animację ostrości za pomocą UIView – xxtesaxx

2

Swift realizacja

Gest:

private func focusGesture() -> UITapGestureRecognizer { 

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

    return tapRec 
} 

Działanie:

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

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