gpt4 book ai didi

ios - 无法在 Swift 中使用前置摄像头

转载 作者:行者123 更新时间:2023-11-28 10:14:58 25 4
gpt4 key购买 nike

我想构建前置摄像头,但无法在 Swift 中构建前置摄像头,即使我可以构建后置摄像头。

当我只把“back”改成“front”时,相机不工作了...

如何使用前置摄像头?

var captureSesssion: AVCaptureSession!
var stillImageOutput: AVCapturePhotoOutput?
var previewLayer: AVCaptureVideoPreviewLayer?
var captureDevice : AVCaptureDevice!

override func viewDidLoad() {
super.viewDidLoad()

captureSesssion = AVCaptureSession()
stillImageOutput = AVCapturePhotoOutput()

captureSesssion.sessionPreset = AVCaptureSessionPreset1920x1080

captureDevice = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front)
//When change "front" to "back", camera is built.

do {
let input = try AVCaptureDeviceInput(device: captureDevice)

if (captureSesssion.canAddInput(input)) {
captureSesssion.addInput(input)

if (captureSesssion.canAddOutput(stillImageOutput)) {
captureSesssion.addOutput(stillImageOutput)
captureSesssion.startRunning()

previewLayer = AVCaptureVideoPreviewLayer(session: captureSesssion)
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait

self.view.layer.addSublayer(previewLayer!)

previewLayer?.position = CGPoint(x: self.view.frame.width / 2, y: self.view.frame.height / 2)
previewLayer?.bounds = self.view.frame
}
}
}
catch {
print(error)
}

最佳答案

iPhone 7 和 iPhone 7 Plus 的前置摄像头可以录制 1080p,但在旧机型上只能录制 720p。因此,您只需要更改 session 预设,即:

captureSesssion.sessionPreset = AVCaptureSessionPreset1280x720

swift 4:

captureSesssion.sessionPreset = AVCaptureSession.Preset.hd1280x720

关于ios - 无法在 Swift 中使用前置摄像头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42522451/

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