Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching Camera with a button in Swift

This seems to work to switch the camera from the back to the front, but I'm trying to come up with an 'if' statement so that I can switch it back too. Any ideas or advice?

    @IBAction func didTouchSwitchButton(sender: UIButton) {


    let camera = getDevice(.Front)
    let cameraBack = getDevice(.Back)


    do {
        input = try AVCaptureDeviceInput(device: camera)
    } catch let error as NSError {
        print(error)
        input = nil
    }
    if(captureSession?.canAddInput(input) == true){
        captureSession?.addInput(input)
        stillImageOutput?.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]
        if(captureSession?.canAddOutput(stillImageOutput) == true){
            captureSession?.addOutput(stillImageOutput)
            previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
            previewLayer?.frame = cameraView.bounds
            cameraView.layer.addSublayer(previewLayer!)
            captureSession?.startRunning()
        }
    }

}

like image 671
craycades Avatar asked Aug 21 '16 01:08

craycades


2 Answers

func switchCamera(_ sender: UIButton) {
    if let session = AVCaptureSession() {
        let currentCameraInput: AVCaptureInput = session.inputs[0]
        session.removeInput(currentCameraInput)
        var newCamera: AVCaptureDevice
        newCamera = AVCaptureDevice.default(for: AVMediaType.video)!

            if (currentCameraInput as! AVCaptureDeviceInput).device.position == .back {
                UIView.transition(with: self.cameraView, duration: 0.5, options: .transitionFlipFromLeft, animations: {
                    newCamera = self.cameraWithPosition(.front)!
                }, completion: nil)
            } else {
                UIView.transition(with: self.cameraView, duration: 0.5, options: .transitionFlipFromRight, animations: {
                    newCamera = self.cameraWithPosition(.back)!
                }, completion: nil)
            }
            do {
                try self.captureSession?.addInput(AVCaptureDeviceInput(device: newCamera))
            }
            catch {
                print("error: \(error.localizedDescription)")
            }

    }
}

func cameraWithPosition(_ position: AVCaptureDevice.Position) -> AVCaptureDevice? {
    let deviceDescoverySession = AVCaptureDevice.DiscoverySession.init(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)

    for device in deviceDescoverySession.devices {
        if device.position == position {
            return device
        }
    }
    return nil
}
like image 79
urvashi bhagat Avatar answered Nov 12 '22 01:11

urvashi bhagat


First create an enum to check the type of camera:

enum CameraDirection {
    case .front
    case .back
}

Then create a variable for the enum:

var currentDirection: CameraDirection = .front//or initial direction

Then in your didTouchSwitchButton function:

if (currentDirection == .front) {
    currentDirection = .back
} else {
    currentDirection = .front
}
reload()

You can see that I called a function called reload. Create that:

func reload() {
    let camera = getDevice(.Front)
    let cameraBack = getDevice(.Back)


    do {
        if currentDirection == .front {
            input = try AVCaptureDeviceInput(device: camera)
        } else {
            input = try AVCaptureDeviceInput(device: cameraBack)
        }
    } catch let error as NSError {
        print(error)
        input = nil
    }
    //rest of code
}
like image 27
Pranav Wadhwa Avatar answered Nov 12 '22 00:11

Pranav Wadhwa