Swift 4 AVcapturesession没有调用captureOutput Delegate

时间:2018-05-04 16:32:46

标签: ios video swift4.1

所以我试着创建一个处理我的应用程序的视频输出流的类和委托方法" captureoutput"永远不会被召唤。

我设置了输入设备并配置了输出,但我无法获得带有输出信息的委托来调用。我已经查看了其他建议并尝试了我读过的所有内容,但我真的很难过。

我正在使用Xcode 9和swift 4

import UIKit
import AVFoundation

protocol FrameExtractorDelegate: class {
func captured(image: UIImage)
}

class FrameExtractor: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {

private let position = AVCaptureDevice.Position.front
private let quality = AVCaptureSession.Preset.high

private var permissionGranted = false
private let sessionQueue = DispatchQueue(label: "session queue")
private let captureSession = AVCaptureSession()
private let context = CIContext()

weak var delegate: FrameExtractorDelegate?

override init() {
    super.init()
    checkPermission()
    sessionQueue.async { [unowned self] in

        self.captureSession.beginConfiguration()
        self.configureSession()
        self.captureSession.commitConfiguration()
        self.captureSession.startRunning()

    }
}

// MARK: AVSession configuration
private func checkPermission() {
    switch AVCaptureDevice.authorizationStatus(for: AVMediaType.video) {
    case .authorized:
        permissionGranted = true
    case .notDetermined:
        requestPermission()
    default:
        permissionGranted = false
    }
}

private func requestPermission() {
    sessionQueue.suspend()
    AVCaptureDevice.requestAccess(for: AVMediaType.video) { [unowned self] granted in
        self.permissionGranted = granted
        self.sessionQueue.resume()
    }
}

private func configureSession() {
    guard permissionGranted else { return }
    captureSession.sessionPreset = quality
    guard let captureDevice = selectCaptureDevice() else { return }
    guard let captureDeviceInput = try? AVCaptureDeviceInput(device: captureDevice) else { return }
    guard captureSession.canAddInput(captureDeviceInput) else { return }
    captureSession.addInput(captureDeviceInput)
    let videoOutput = AVCaptureVideoDataOutput()
    videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "sample buffer"))
    guard captureSession.canAddOutput(videoOutput) else { return }
    captureSession.addOutput(videoOutput)
    guard let connection = videoOutput.connection(with: AVFoundation.AVMediaType.video) else { return }
    guard connection.isVideoOrientationSupported else { return }
    guard connection.isVideoMirroringSupported else { return }
    connection.videoOrientation = .portrait
    connection.isVideoMirrored = position == .front
}

private func selectCaptureDevice() -> AVCaptureDevice? {
    return AVCaptureDevice.DiscoverySession.init(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.front).devices.first
}

// MARK: Sample buffer to UIImage conversion
private func imageFromSampleBuffer(sampleBuffer: CMSampleBuffer) -> UIImage? {
    guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return nil }
    let ciImage = CIImage(cvPixelBuffer: imageBuffer)
    guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else { return nil }
    return UIImage(cgImage: cgImage)
}

// MARK: AVCaptureVideoDataOutputSampleBufferDelegate
 func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    print("output captured")
    guard let uiImage = imageFromSampleBuffer(sampleBuffer: sampleBuffer) else { return }
    DispatchQueue.main.async { [unowned self] in
        self.delegate?.captured(image: uiImage)
    }
}

}

0 个答案:

没有答案