【问题标题】:Adding a Switch Camera Button to Custom Camera View将切换相机按钮添加到自定义相机视图
【发布时间】:2019-01-06 23:49:33
【问题描述】:

我从 GitHub 上获取了这段代码,作为最基本的自定义相机视图控制器。我已经实现了一个闪光灯,如果我测试前后摄像头,它就可以工作。但是我将如何实现一个按钮,您可以在应用程序中切换相机?我创建了一个相机按钮按下操作,当点击相机开关按钮时该操作将运行。

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()
    }
}

}

【问题讨论】:

    标签: ios swift camera avfoundation


    【解决方案1】:

    首先,将您的AVCaptureDeviceInput 存储在属性中 private var deviceInput: AVCaptureDeviceInput?

    接下来创建枚举,指示哪个摄像头处于活动状态

    enum CameraPosition {
        case front
        case back
    }
    

    和当前相机模式的属性

    private var cameraPosition: CameraPosition = .back
    

    现在创建负责相机切换的函数

    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)
        }
    }
    

    现在在cameraButtonPressed 方法中你可以切换摄像头

    @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()
        }
    }
    

    【讨论】:

    • 非常感谢您的回答。我对编程很陌生,一定没有正确实现您的代码,因为我仍然无法让相机翻转。我应该简单地添加您的代码或用它替换我的一些代码吗?对不起。
    • 只需添加我的代码。更多信息可以参考我的 GitHub 仓库github.com/ChernyshenkoTaras/TCCoreCamera/blob/master/…
    • 有趣。应用不会崩溃,但相机不会切换。
    • 您存储了 deviceInput 吗?请提供您的更新代码
    • 什么是存储设备输入?
    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多