gpt4 book ai didi

swift - 自动为 AVCaptureDevice 预览层使用前置摄像头,如 Snapchat 或 Houseparty (Swift 3)

转载 作者:行者123 更新时间:2023-11-28 11:00:00 27 4
gpt4 key购买 nike

<分区>

本质上,我想要完成的是让 AVCaptureDevice 的前置摄像头成为 AVCaptureSession 期间应用程序的第一个也是唯一一个选项。

我查看了 StackOverflow,从 iOS 10、Swift 3 和 Xcode 8 开始,所有提供的方法和答案都已弃用。

我知道您应该使用 AVCaptureDeviceDiscoverySession 枚举设备并查看它们以区分正面和背面,但我不确定该怎么做。

有人能帮忙吗?如果是这样就太棒了!

这是我的代码:

    override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

previewLayer.frame = singleViewCameraSlot.bounds
self.singleViewCameraSlot.layer.addSublayer(previewLayer)
captureSession.startRunning()

}



lazy var captureSession: AVCaptureSession = {
let capture = AVCaptureSession()
capture.sessionPreset = AVCaptureSessionPreset1920x1080
return capture
}()

lazy var previewLayer: AVCaptureVideoPreviewLayer = {
let preview = AVCaptureVideoPreviewLayer(session: self.captureSession)

preview?.videoGravity = AVLayerVideoGravityResizeAspect
preview?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
preview?.bounds = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
preview?.position = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY)

return preview!
}()


func setupCameraSession() {


let frontCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) as AVCaptureDevice

do {
let deviceInput = try AVCaptureDeviceInput(device: frontCamera)

captureSession.beginConfiguration()

if (captureSession.canAddInput(deviceInput) == true) {
captureSession.addInput(deviceInput)
}

let dataOutput = AVCaptureVideoDataOutput()

dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString) : NSNumber(value: kCVPixelFormatType_420YpCbCr8BiPlanarFullRange as UInt32)]
dataOutput.alwaysDiscardsLateVideoFrames = true

if (captureSession.canAddOutput(dataOutput) == true) {
captureSession.addOutput(dataOutput)
}

captureSession.commitConfiguration()

let queue = DispatchQueue(label: "io.goodnight.videoQueue")
dataOutput.setSampleBufferDelegate(self, queue: queue)

}
catch let error as NSError {
NSLog("\(error), \(error.localizedDescription)")
}

}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com