将前置摄像头更改为后置摄像头

Posted

技术标签:

【中文标题】将前置摄像头更改为后置摄像头【英文标题】:Change front to back camera 【发布时间】:2017-01-28 22:57:26 【问题描述】:

我需要。我尝试创建一个按钮,当按下更换相机时,而不是如何做到这一点。这是我的代码(这段代码运行前置摄像头):

var captureSession = AVCaptureSession()
var sessionOutput = AVCapturePhotoOutput()
var sessionOutputSetting = AVCapturePhotoSettings(format:[AVVideoCodecKey:AVVideoCodecJPEG])

var previewLayer : AVCaptureVideoPreviewLayer?


@IBOutlet var cameraView: UIView!

override func viewDidLoad() 
    super.viewDidLoad()

    // Do any additional setup after loading the view.


override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.



override func viewDidAppear(_ animated: Bool) 
    super.viewDidAppear(animated)
    previewLayer?.frame = cameraView.bounds


override func viewWillAppear(_ animated: Bool) 
    super.viewWillAppear(animated)



    let deviceDiscoverySession = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDuoCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.unspecified)
    for device in (deviceDiscoverySession?.devices)! 
        if device.position == AVCaptureDevicePosition.back 

            do 
                let input = try AVCaptureDeviceInput(device: device)
                if captureSession.canAddInput(input) 
                    captureSession.addInput(input)

                    if captureSession.canAddOutput(sessionOutput) 
                        captureSession.addOutput(sessionOutput)
                        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                        previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
                        previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
                        cameraView.layer.addSublayer(previewLayer!)

                    
                

             catch 
                print("Error")

            
        
    
    captureSession.startRunning()
    print("????")



对不起,我还是个新手。还有人可以告诉你是否曾经使用过 SwiftyCam (https://github.com/Awalz/SwiftyCam)

【问题讨论】:

***.com/a/34470541/4475605 How to switch to front camera in Swift 2?的可能重复 【参考方案1】:

我已经在 swift 2 中完成了。您可以通过将语法转换为swift 3来使用它。

@IBAction func onFrontRearCameraClick(sender: AnyObject)

    if self.captureDevice != nil
    
        if captureSession.running
        
            print("captureSession running")
        

        if btnFrontRearCamera.selected
        
            captureDevice = self.getDeviceWithPosition(AVCaptureDevicePosition.Back)
        
        else
        
            captureDevice = self.getDeviceWithPosition(AVCaptureDevicePosition.Front)
        

        if captureSession.running
        
            //Indicate that some changes will be made to the session
            captureSession.beginConfiguration()

            let currentInput : AVCaptureInput = captureSession.inputs[0] as! AVCaptureInput
            captureSession.removeInput(currentInput)

            do 
                try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice))
             catch 
                print("Error adding video input device")
            

            //Commit all the configuration changes at once
            captureSession.commitConfiguration()

            return
        
    

【讨论】:

以上是关于将前置摄像头更改为后置摄像头的主要内容,如果未能解决你的问题,请参考以下文章

带有前置或后置摄像头的 ios zxing

是否可以在Android中同时使用前置和后置摄像头[重复]

android Camera如何判断当前使用的摄像头是前置还是后置

前置摄像头和后置摄像头同时打开?

如何使用后置摄像头代替前置摄像头?

android Camera 如何判断当前使用的摄像头是前置还是后置