2014-10-04 4 views
15

카메라를 열고 비디오를 미리 볼 수있는 코드를 작성했습니다. 카메라 성공적으로Swift에서 AVCaptureSession의 출력을 서버에 보내려면

import UIKit 
import AVFoundation 

class ViewController: UIViewController { 

    let captureSession = AVCaptureSession() 
    var previewLayer : AVCaptureVideoPreviewLayer? 

    // If we find a device we'll store it here for later use 
    var captureDevice : AVCaptureDevice? 

    override func viewDidLoad() { 
     super.viewDidLoad()    
     // Do any additional setup after loading the view, typically from a nib. 

     captureSession.sessionPreset = AVCaptureSessionPresetHigh 

     let devices = AVCaptureDevice.devices() 

     // Loop through all the capture devices on this phone 
     for device in devices { 
      // Make sure this particular device supports video 
      if (device.hasMediaType(AVMediaTypeVideo)) { 
       // Finally check the position and confirm we've got the back camera 
       if(device.position == AVCaptureDevicePosition.Back) { 
        captureDevice = device as? AVCaptureDevice 
        if captureDevice != nil { 
         println("Capture device found") 
         beginSession() 
        } 
       } 
      } 
     } 

    } 

    func beginSession() { 

     var err : NSError? = nil 
     captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err)) 

     if err != nil { 
      println("error: \(err?.localizedDescription)") 
     } 

     previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
     self.view.layer.addSublayer(previewLayer) 
     previewLayer?.frame = self.view.layer.frame 

     captureSession.startRunning() 

    } 

} 

이 열려 나는 장면을 미리 볼 수 있습니다 : 지금 이상적으로 H.264 여기

로 인코딩 서버로 보낼 수있는 출력에서 ​​프레임을 캡처 할 내가있어 무엇 .

나는 출력을 얻는 것처럼 보이는이 Objective C 코드를 발견했지만이를 신속하게 변환하는 방법을 모른다. AVCaptureVideoDataOutput, AVAssetWriter, AVAssetWriterInput 및 AVAssetWriterInputPixelBufferAdaptor를 사용하여 프레임을 H.264로 인코딩 된 동영상 파일에 기록합니다.

Can use AVCaptureVideoDataOutput and AVCaptureMovieFileOutput at the same time?

누군가가 그것을 변환 할 또는 내 현재 코드에서 프레임을 활용하는 방법에 관해서는 저에게 조언 해 줄 수 있습니까?

+0

이러한 종류의 문서는 신속하게 제공되지 않습니다. 상당히 실망 스럽습니다. –

+0

해결책을 찾았습니까? –

+0

[AVCaptureVideoDataOutput과 AVCaptureMovieFileOutput을 동시에 사용할 수 있습니까?] (http://stackoverflow.com/questions/4944083/can-use-avcapturevideodataoutput-and-avcapturemoviefileoutput-at-the-same-time)에서 코드를 변환하려고하십니까?) 신속하게? –

답변

3

Apple은 ObjectiveC의 샘플 프로젝트 AVCam을 가지고 있습니다.

Here's Swift에서 AVCamera를 사용하는 것에 대한 또 다른 질문입니다.

저는 개인적으로이 https://github.com/alex-chan/AVCamSwift을 사용했으며 문제가 없습니다. Xcode의 Latest Swift 구문으로 변환하면됩니다.

또 다른 제안은 발견 한 ObjectiveC 코드를 사용하고 브리징 헤더를 통해 신속한 코드를 가져 오는 것입니다.