繁体   English   中英

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

[英]Change front to back camera

我需要将前置摄像头更改为后置摄像头。 我尝试创建一个按钮,当按下更改相机时,而不是怎么做。 这是我的代码(此代码运行前置摄像头):

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

很快就做到了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
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM