简体   繁体   中英

Adding a Switch Camera Button to Custom Camera View

I got this code off of GitHub as the most basic custom camera view controller. I have implemented a flash, which works if I test both front and back cameras. But how would I implement a button in which you could switch cameras while in the app? I have created a camera Button Pressed action which will run when the camera switch button is tapped.

class CameraViewController: UIViewController {

@IBOutlet weak var flashButton: UIButton!
@IBOutlet weak var cameraButton: UIButton!

var flashOn = false

@IBOutlet weak var previewView: PreviewView!


let captureSession = AVCaptureSession()
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
let capturePhotoOutput = AVCapturePhotoOutput()
let capturePhotoDelegate = CapturePhotoDelegate()

private var deviceInput: AVCaptureDeviceInput?
private var cameraPosition: CameraPosition = .back

enum CameraPosition {
    case front
    case back
}

override func viewDidLoad() {
    super.viewDidLoad()
    checkCameraUsagePermission()

    flashButton.setTitle("OFF", for: .normal)
    cameraButton.setTitle("BACK", for: .normal)
}

func initialiseCaptureSession() {

    let captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .unspecified)

    guard let input = try? AVCaptureDeviceInput(device: captureDevice!),
        captureSession.canAddInput(input)
        else { return }

    captureSession.addInput(input)
    self.previewView.videoPreviewLayer.session = self.captureSession
    self.previewView.videoPreviewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

    capturePhotoOutput.isHighResolutionCaptureEnabled = true
    captureSession.addOutput(capturePhotoOutput)

    captureSession.startRunning()
}

@IBAction func onTapTakePhoto(_ sender: UIButton) {

    let photoSettings = AVCapturePhotoSettings()
    photoSettings.isAutoStillImageStabilizationEnabled = true
    photoSettings.isHighResolutionPhotoEnabled = true
    photoSettings.flashMode = .auto
    if flashOn == true {
        photoSettings.flashMode = .on
    } else if flashOn == false {
        photoSettings.flashMode = .off
    }
    capturePhotoOutput.capturePhoto(with: photoSettings, delegate: capturePhotoDelegate)
}

func checkCameraUsagePermission() {
    switch AVCaptureDevice.authorizationStatus(for: .video) {
    case .authorized:
        self.initialiseCaptureSession()

    case .notDetermined:
        AVCaptureDevice.requestAccess(for: .video) { granted in
            if granted {
                self.initialiseCaptureSession()
            }
        }
    case .denied:
        return
    case .restricted:
        return
    }
}

@IBAction func flashButtonPressed(_ sender: UIButton) {
    if flashOn == false {
        flashOn = true
        flashButton.setTitle("ON", for: .normal)
    } else {
        flashOn = false
        flashButton.setTitle("OFF", for: .normal)

    }

}

func addVideoInput(position: AVCaptureDevice.Position) {
    guard let device: AVCaptureDevice = AVCaptureDevice.default(.builtInWideAngleCamera,
        for: .video, position: position) else { return }
    if let currentInput = self.deviceInput {
        self.captureSession.removeInput(currentInput)
        self.deviceInput = nil
    }
    do {
        let input = try AVCaptureDeviceInput(device: device)
        if self.captureSession.canAddInput(input) {
            self.captureSession.addInput(input)
            self.deviceInput = input
        }
    } catch {
        print(error)
    }
}

@IBAction func cameraButtonPressed(_ sender: UIButton) {
    switch self.cameraPosition {
    case .front:
        self.cameraPosition = .back
        self.addVideoInput(position: .back)
    case .back:
        self.cameraPosition = .front
        self.addVideoInput(position: .front)
    }
    //configure your session here
    DispatchQueue.main.async {
        self.captureSession.beginConfiguration()
        if self.captureSession.canAddOutput(self.capturePhotoOutput) {
            self.captureSession.addOutput(self.capturePhotoOutput)
        }
        self.captureSession.commitConfiguration()
    }
}

}

First of all, store your AVCaptureDeviceInput in property private var deviceInput: AVCaptureDeviceInput?

next create enum that will indicate which camera is active

enum CameraPosition {
    case front
    case back
}

and property for current camera mode

private var cameraPosition: CameraPosition = .back

Now create function that will be responsible for camera switching

func addVideoInput(position: AVCaptureDevice.Position) {
    guard let device: AVCaptureDevice = AVCaptureDevice.default(.builtInWideAngleCamera,
        for: .video, position: position) else { return }
    if let currentInput = self.deviceInput {
        self.captureSession.removeInput(currentInput)
        self.deviceInput = nil
    }
    do {
        let input = try AVCaptureDeviceInput(device: device)
        if self.captureSession.canAddInput(input) {
            self.captureSession.addInput(input)
            self.deviceInput = input
        }
    } catch {
        print(error)
    }
}

and now in cameraButtonPressed method you can switch cameras

@IBAction func cameraButtonPressed(_ sender: UIButton) {
    switch self.cameraPosition {
        case .front:
            self.cameraPosition = .back
            self.addVideoInput(position: .back)
        case .back:
            self.cameraPosition = .front
            self.addVideoInput(position: .front)
    }
    //configure your session here
    DispatchQueue.main.async { 
        self.captureSession.beginConfiguration()
        if self.captureSession.canAddOutput(self.capturePhotoOutput) {
            self.captureSession.addOutput(self.capturePhotoOutput)
        }
        self.captureSession.commitConfiguration()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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