在AVFoundation中澄清

时间:2018-08-03 17:49:16

标签: swift xcode camera avfoundation

我从GitHub上获得了此代码,这是最基本的自定义相机视图控制器。我已经安装了闪光灯,可以同时测试前后摄像头。以前,在Stack Overflow上,我问过如何创建一个切换相机按钮,一个友善的用户告诉我实现枚举和某些变量,这是一个很大的帮助。可悲的是,我对编程非常陌生,并且不了解他写的所有内容。太新了,我真的需要尽可能以外行的术语解释所有事情。他告诉我要做的最后一件事是存储deviceInput并在initialiseCaptureSession中实例化AVCaptureDeviceInput(将其存储到deviceInput属性中)。谁能向我解释这意味着什么并注释掉可以添加到现有代码中的代码行?谢谢。

class CameraViewController: UIViewController {

@IBOutlet weak var flashButton: UIButton!
@IBOutlet weak var cameraButton: UIButton!

var flashOn = false

@IBOutlet weak var previewView: PreviewView!


let captureSession = AVCaptureSession()
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
let capturePhotoOutput = AVCapturePhotoOutput()
let capturePhotoDelegate = CapturePhotoDelegate()

private var deviceInput: AVCaptureDeviceInput?
private var cameraPosition: CameraPosition = .back

enum CameraPosition {
case front
case back
}

override func viewDidLoad() {
  super.viewDidLoad()
checkCameraUsagePermission()

flashButton.setTitle("OFF", for: .normal)
cameraButton.setTitle("BACK", for: .normal)
}

func initialiseCaptureSession() {

let captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .unspecified)

guard let input = try? AVCaptureDeviceInput(device: captureDevice!),
    captureSession.canAddInput(input)
    else { return }

captureSession.addInput(input)
self.previewView.videoPreviewLayer.session = self.captureSession
self.previewView.videoPreviewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

capturePhotoOutput.isHighResolutionCaptureEnabled = true
captureSession.addOutput(capturePhotoOutput)

captureSession.startRunning()
}

@IBAction func onTapTakePhoto(_ sender: UIButton) {

let photoSettings = AVCapturePhotoSettings()
photoSettings.isAutoStillImageStabilizationEnabled = true
photoSettings.isHighResolutionPhotoEnabled = true
photoSettings.flashMode = .auto
if flashOn == true {
    photoSettings.flashMode = .on
} else if flashOn == false {
    photoSettings.flashMode = .off
}
capturePhotoOutput.capturePhoto(with: photoSettings, delegate: capturePhotoDelegate)
}

func checkCameraUsagePermission() {
switch AVCaptureDevice.authorizationStatus(for: .video) {
case .authorized:
    self.initialiseCaptureSession()

case .notDetermined:
    AVCaptureDevice.requestAccess(for: .video) { granted in
        if granted {
            self.initialiseCaptureSession()
        }
    }
case .denied:
    return
case .restricted:
    return
   }
}

@IBAction func flashButtonPressed(_ sender: UIButton) {
if flashOn == false {
    flashOn = true
    flashButton.setTitle("ON", for: .normal)
} else {
    flashOn = false
    flashButton.setTitle("OFF", for: .normal)

     }

 }

func addVideoInput(position: AVCaptureDevice.Position) {
guard let device: AVCaptureDevice = AVCaptureDevice.default(.builtInWideAngleCamera,
    for: .video, position: position) else { return }
if let currentInput = self.deviceInput {
    self.captureSession.removeInput(currentInput)
    self.deviceInput = nil
}
do {
    let input = try AVCaptureDeviceInput(device: device)
    if self.captureSession.canAddInput(input) {
        self.captureSession.addInput(input)
        self.deviceInput = input
    }
} catch {
    print(error)
    }
}

@IBAction func cameraButtonPressed(_ sender: UIButton) {
switch self.cameraPosition {
case .front:
    self.cameraPosition = .back
    self.addVideoInput(position: .back)
case .back:
    self.cameraPosition = .front
    self.addVideoInput(position: .front)
}
//configure your session here
DispatchQueue.main.async {
    self.captureSession.beginConfiguration()
    if self.captureSession.canAddOutput(self.capturePhotoOutput) {
        self.captureSession.addOutput(self.capturePhotoOutput)
    }
    self.captureSession.commitConfiguration()
    }
   }
  }
}

0 个答案:

没有答案