9.6 C
New York
Friday, February 28, 2025

ios – change from entrance digital camera to again digital camera in AVFoundation


I’ve CaptureDelegate that has a way setupCaptureDevice that I name from .job:

class CaptureDelegate: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate, ObservableObject {
    
    
    let captureSession = AVCaptureSession()         // For capturing what is available in from the system's digital camera
    let captureSessionQueue = DispatchQueue(label: "captureSessionQueue")   // For processing photographs
    

    @Printed var cameraIsActive: Bool = false 


    func setupCaptureSession(captureDevice: AVCaptureDevice?) {

        
        let videoCaptureDevice: AVCaptureDevice
        
        if let captureDevice {
            videoCaptureDevice = captureDevice
        } else {
            //will decide the most effective digital camera or the one digital camera(for iphone SE)
            if let system = AVCaptureDevice.default(.builtInDualCamera,
                                                    for: .video, place: .again) {
                videoCaptureDevice = system
            } else if let system = AVCaptureDevice.default(.builtInWideAngleCamera,
                                                           for: .video, place: .again) {
                videoCaptureDevice = system
            } else {
                fatalError("Lacking anticipated again digital camera system.")
            }

        }
                
        guard let videoDeviceInput = attempt? AVCaptureDeviceInput(system: videoCaptureDevice) else {      //enter from the again Digicam
            print( "Did not create AVCaptureDeviceInput")
            return
        }
        
        let videoOutput = AVCaptureVideoDataOutput()                //the output that digital camera catches
        videoOutput.setSampleBufferDelegate(self, queue: captureSessionQueue)
        
        captureSession.beginConfiguration( )
        
        if captureSession.canAddInput(videoDeviceInput) {
            captureSession.addInput(videoDeviceInput)
        } else {
            print("Failed so as to add enter to seize session")
        }
        
        if captureSession.canAddOutput(videoOutput) {
            captureSession.addOutput(videoOutput)
        } else {
            print("Failed so as to add output to seize session")
        }
        
        captureSession.sessionPreset = .excessive
        
        captureSession.commitConfiguration()    //commit the brand new frames to CaptureSession
        
        
        DispatchQueue.international().async {
            self.captureSession.startRunning()
        }
        

it takes an non-compulsory AVCaptureDevice in order that the primary time the app runs it initiates with the again digital camera. I wish to enable the person to toggle from again to entrance and vice verse so I’m making an attempt to have a broadcast property that the person can toggle through a way name In CaptureDelegate:

@Printed var currentCamera: AVCaptureDevice?


//Change from entrance to again digital camera and so on
func toggleCaptureDevice() {
captureSession.stopRunning()

//Now outline the AVCapturedevice, change to again cam whether it is entrance and change to entrance cam if its again

//Now name setupCaptureSession however this time truly move in that system
}
    }

My drawback is how do I do know if the App is presently utilizing the entrance or again digital camera to have the ability to toggle it and replace my currentCamera?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles