2013-04-05 44 views
8

Tôi đang sao chép ứng dụng máy ảnh của Apple bằng AVCaptureSession dựa trên mẫu ứng dụng AppCam của Apple. Vấn đề là tôi không thể nhìn thấy hình chữ nhật lấy nét trong màn hình xem trước video. Tôi đã sử dụng mã sau để đặt tiêu điểm, nhưng hình chữ nhật tiêu điểm vẫn không được hiển thị.máy ảnh iphone hiển thị hình chữ nhật lấy né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]; 
     } 
    }  
} 

Khi tôi sử dụng UIImagePickerController, tiêu điểm tự động, nhấn lấy nét được hỗ trợ theo mặc định và có thể nhìn thấy hình chữ nhật tiêu điểm. Không có cách nào để hiển thị hình chữ nhật tiêu điểm trong lớp xem trước video bằng AVCaptureSession?

+0

hmm, có vẻ như không ai biết thi S. – ttotto

Trả lời

11

Hoạt ảnh tiêu điểm là hoạt ảnh tùy chỉnh hoàn chỉnh mà bạn phải tự tạo. Tôi hiện đang có chính xác cùng một vấn đề như bạn: Tôi muốn hiển thị hình chữ nhật dưới dạng phản hồi cho người dùng sau khi anh ấy nhấn vào lớp xem trước.

Điều đầu tiên bạn muốn làm là thực hiện gõ để lấy nét, có lẽ nơi bạn bắt đầu lớp preview:

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

Bây giờ thực hiện gõ để tập trung phương pháp riêng của mình:

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

Điều cuối cùng mà tôi chưa triển khai, là thêm hoạt ảnh lấy nét vào lớp xem trước hoặc thay vì bộ điều khiển chế độ xem đang giữ lớp xem trước. Tôi tin rằng có thể được thực hiện trong tapToFocus :. Ở đó bạn đã có điểm tiếp xúc. Chỉ cần thêm chế độ xem hình ảnh động hoặc một số chế độ xem khác có vị trí cảm ứng làm trung tâm của nó. Sau khi hoạt ảnh kết thúc, hãy xóa chế độ xem hình ảnh.

+1

Bạn cũng có thể xem: http://stackoverflow.com/questions/15449271/avfoundation-tap-to-focus-feedback-rectangle Có mô tả hay về cách triển khai hoạt ảnh tiêu điểm bằng UIView – xxtesaxx

2

thực hiện Swift

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 
} 

Hành động:

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

thực hiện 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") 
     } 
    } 
} 
Các vấn đề liên quan