无法在前置摄像头的情况下添加输入,将摄像头从后切换到前置?
Not able to add input in case of Front camera, Switching camera from back to front?
我在自定义视图 (cameraView) 中打开我的摄像头,它在后置摄像头上工作正常,但是当我使用自定义转动 button(TurnCamera)
将它切换到前置时出现问题,我得到以下信息:
input "AVCaptureFigVideoDevice: 0x1035c9e80 [Front
Camera][com.apple.avfoundation.avcapturedevice.built-in_video:1]"
但是我的自定义摄像头视图仍然显示后置摄像头,并且正在获取 captureSession.canAddInput(input) = false
,我是否应该为前置摄像头授予对 info.plist 的任何访问权限。
我打开相机的代码是:-
func camera(){
let devices = AVCaptureDevice.devices(for: AVMediaType.video)
currentCaptureDevice = (usingFrontCamera ? getFrontCamera() : getBackCamera())
print(currentCaptureDevice)
for device in devices{
do{
let input = try AVCaptureDeviceInput(device: currentCaptureDevice!)
if captureSession.canAddInput(input){
captureSession.addInput(input)
sessionOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
if captureSession.canAddOutput(sessionOutput){
captureSession.startRunning()
captureSession.addOutput(sessionOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
previewLayer.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
cameraView.layer.addSublayer(previewLayer)
previewLayer.position = CGPoint(x: self.cameraView.frame.width/2, y: self.cameraView.frame.height/2)
cameraView.contentMode = .scaleToFill
previewLayer.bounds = cameraView.frame
}
}
}
catch{
print("error")
}
}
}
取回摄像头的功能是:-
func getBackCamera() -> AVCaptureDevice{
return AVCaptureDevice.default(for: AVMediaType.video)!
}
获取前置摄像头的函数是:-
func getFrontCamera() -> AVCaptureDevice?{
let videoDevices = AVCaptureDevice.devices(for: AVMediaType.video)
for device in videoDevices{
let device = device as! AVCaptureDevice
if device.position == AVCaptureDevice.Position.front {
return device
}
}
return nil
}
切换相机的按钮操作:-
@IBAction func TurnCamera(_ sender: UIButton) {
usingFrontCamera = !usingFrontCamera
camera()
}
在 :- https://www.dropbox.com/s/6cfmbh3tnw5y4f7/SwitchingCamera.zip?dl=0
上查找演示项目
提前致谢
我得到了答案,当从后切换到前时,我没有删除捕获会话输入。
我只修改了两个功能
1:- 早些时候
func getBackCamera() -> AVCaptureDevice{
return AVCaptureDevice.default(for: AVMediaType.video)!
}
现在,
func getBackCamera() -> AVCaptureDevice{
for i in captureSession.inputs {
captureSession.removeInput(i as! AVCaptureInput)
}
return AVCaptureDevice.default(for: AVMediaType.video)!
}
2:- 早些时候,
func getFrontCamera() -> AVCaptureDevice?{
let videoDevices = AVCaptureDevice.devices(for: AVMediaType.video)
for device in videoDevices{
let device = device as! AVCaptureDevice
if device.position == AVCaptureDevice.Position.front {
return device
}
}
return nil
}
现在,
func getFrontCamera() -> AVCaptureDevice?{
for i in captureSession.inputs {
captureSession.removeInput(i as! AVCaptureInput)
}
let videoDevices = AVCaptureDevice.devices(for: AVMediaType.video)
for device in videoDevices{
let device = device
if device.position == AVCaptureDevice.Position.front {
return device
}
}
return nil
}
获取演示:- https://www.dropbox.com/s/p6opina5gtg8nvd/Updated%20Switching%20Camera%202.zip?dl=0
我在自定义视图 (cameraView) 中打开我的摄像头,它在后置摄像头上工作正常,但是当我使用自定义转动 button(TurnCamera)
将它切换到前置时出现问题,我得到以下信息:
input "AVCaptureFigVideoDevice: 0x1035c9e80 [Front Camera][com.apple.avfoundation.avcapturedevice.built-in_video:1]"
但是我的自定义摄像头视图仍然显示后置摄像头,并且正在获取 captureSession.canAddInput(input) = false
,我是否应该为前置摄像头授予对 info.plist 的任何访问权限。
我打开相机的代码是:-
func camera(){
let devices = AVCaptureDevice.devices(for: AVMediaType.video)
currentCaptureDevice = (usingFrontCamera ? getFrontCamera() : getBackCamera())
print(currentCaptureDevice)
for device in devices{
do{
let input = try AVCaptureDeviceInput(device: currentCaptureDevice!)
if captureSession.canAddInput(input){
captureSession.addInput(input)
sessionOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
if captureSession.canAddOutput(sessionOutput){
captureSession.startRunning()
captureSession.addOutput(sessionOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
previewLayer.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
cameraView.layer.addSublayer(previewLayer)
previewLayer.position = CGPoint(x: self.cameraView.frame.width/2, y: self.cameraView.frame.height/2)
cameraView.contentMode = .scaleToFill
previewLayer.bounds = cameraView.frame
}
}
}
catch{
print("error")
}
}
}
取回摄像头的功能是:-
func getBackCamera() -> AVCaptureDevice{
return AVCaptureDevice.default(for: AVMediaType.video)!
}
获取前置摄像头的函数是:-
func getFrontCamera() -> AVCaptureDevice?{
let videoDevices = AVCaptureDevice.devices(for: AVMediaType.video)
for device in videoDevices{
let device = device as! AVCaptureDevice
if device.position == AVCaptureDevice.Position.front {
return device
}
}
return nil
}
切换相机的按钮操作:-
@IBAction func TurnCamera(_ sender: UIButton) {
usingFrontCamera = !usingFrontCamera
camera()
}
在 :- https://www.dropbox.com/s/6cfmbh3tnw5y4f7/SwitchingCamera.zip?dl=0
上查找演示项目提前致谢
我得到了答案,当从后切换到前时,我没有删除捕获会话输入。
我只修改了两个功能 1:- 早些时候
func getBackCamera() -> AVCaptureDevice{
return AVCaptureDevice.default(for: AVMediaType.video)!
}
现在,
func getBackCamera() -> AVCaptureDevice{
for i in captureSession.inputs {
captureSession.removeInput(i as! AVCaptureInput)
}
return AVCaptureDevice.default(for: AVMediaType.video)!
}
2:- 早些时候,
func getFrontCamera() -> AVCaptureDevice?{
let videoDevices = AVCaptureDevice.devices(for: AVMediaType.video)
for device in videoDevices{
let device = device as! AVCaptureDevice
if device.position == AVCaptureDevice.Position.front {
return device
}
}
return nil
}
现在,
func getFrontCamera() -> AVCaptureDevice?{
for i in captureSession.inputs {
captureSession.removeInput(i as! AVCaptureInput)
}
let videoDevices = AVCaptureDevice.devices(for: AVMediaType.video)
for device in videoDevices{
let device = device
if device.position == AVCaptureDevice.Position.front {
return device
}
}
return nil
}
获取演示:- https://www.dropbox.com/s/p6opina5gtg8nvd/Updated%20Switching%20Camera%202.zip?dl=0