【问题标题】:Custom camera in xcode, Swift 3xcode,Swift 3中的自定义相机
【发布时间】:2017-09-23 01:57:32
【问题描述】:

所以我遇到了这个问题,我试图在 Xcode 中创建一个自定义摄像头,但是由于某种原因我无法获得它,因此它被设置为使用前置摄像头。无论我对代码进行什么更改,它似乎都只使用后置摄像头,我希望有人足够慷慨,可以快速查看我下面的代码,看看是否有我遗漏的东西或我去过的地方错误的。任何帮助将不胜感激,感谢您的宝贵时间。

func SelectInputDevice() {

let devices = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, 
              mediaType: AVMediaTypeVideo, position: .front)
 if devices?.position == AVCaptureDevicePosition.front {

 print(devices?.position)
 frontCamera = devices

}

currentCameraDevice = frontCamera
do {

   let captureDeviceInput = try AVCaptureDeviceInput(device: currentCameraDevice)
   captureSession.addInput(captureDeviceInput)

 } catch {

    print(error.localizedDescription)

  }

}

这是 frontCamera 和 currentCameraDevice 是 AVCaptureDevice 的地方。

【问题讨论】:

    标签: swift swift3 camera avcapturesession


    【解决方案1】:

    您的代码中似乎缺少一些内容:

    1) 要更改输入设备,您需要通过调用session.beginConfiguration() 重新配置会话,然后再添加新设备并以session.commitConfiguration() 结束。此外,所有更改都应在后台队列(希望您已为会话创建)上进行,以便在配置会话时不会阻止 UI。

    2) 代码在使用session.canAddInput(captureDeviceInput) 添加之前检查它允许新设备的会话会更安全 + 删除以前的设备(后置摄像头),因为不允许前+后配置。

    3) 之前检查您的设备是否有一个正常工作的前置摄像头(可能已损坏)以防止任何崩溃也会更干净。

    将捕获设备更改为前置摄像头的完整代码如下所示:

    func switchCameraToFront() {
    
        //session & sessionQueue are references to the capture session and its dispatch queue 
        sessionQueue.async { [unowned self] in
    
            let currentVideoInput = self.videoDeviceInput //ref to current videoInput as setup in initial session config
    
            let preferredPosition: AVCaptureDevicePosition = .front
            let preferredDeviceType: AVCaptureDeviceType = .builtInWideAngleCamera
    
            let devices = self.videoDeviceDiscoverySession.devices!
            var newVideoDevice: AVCaptureDevice? = nil
    
            // First, look for a device with both the preferred position and device type. Otherwise, look for a device with only the preferred position.
            if let device = devices.filter({ $0.position == preferredPosition && $0.deviceType == preferredDeviceType }).first {
                newVideoDevice = device
            }
            else if let device = devices.filter({ $0.position == preferredPosition }).first {
                newVideoDevice = device
            }
    
            if let videoDevice = newVideoDevice {
                do {
                    let videoDeviceInput = try AVCaptureDeviceInput(device: videoDevice)
    
                    self.session.beginConfiguration()
    
                    // Remove the existing device input first, since using the front and back camera simultaneously is not supported.
                    self.session.removeInput(currentVideoInput)
    
                    if self.session.canAddInput(videoDeviceInput) {                                              
    
                        self.session.addInput(videoDeviceInput)
                        self.videoDeviceInput = videoDeviceInput
                    }
                    else {
                        //fallback to current device
                        self.session.addInput(self.videoDeviceInput);
                    }
                    self.session.commitConfiguration()
                }
                catch {
    
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 2016-04-04
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多