在前置摄像头的情况下无法添加输入,从后到前切换摄像头?

问题描述 投票:2回答:1

我在自定义视图(cameraView)中打开我的相机,它对后置摄像头工作正常,但当我使用自定义转button(TurnCamera)将其切换到前面时出现问题,我得到以下内容:

输入“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")
            }
        }
    }

get get camera的功能是: -

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

提前致谢

ios swift xcode avcapturedevice avcapture
1个回答
3
投票

我得到了答案,当从后面切换到前面时,我没有删除捕获会话输入。

我只做了两个功能的改变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

© www.soinside.com 2019 - 2024. All rights reserved.