senty senty - 7 months ago 162
Swift Question

AVFoundation Camera Check and Flip Variable

I created a custom camera tool. Now, I am trying to handle checking existence of cameras however, I only have Simulator (no camera) and iphone (both cameras). I handled no camera but I couldn't understand how it works for one camera, so I also couldn't figure out how to help the user flip the camera

Position

.Back
and
.Front
works, and I handled no camera, but I couldn't figure out how to


  • handle checks for 1 camera

  • assign a variable for the control of Back & Front cameras depending on their existence (So I can create a uibutton in the VC and control flipping of camera back and front).






// I call addVideoInput() while initializing

func addVideoInput() {
if let device: AVCaptureDevice = self.deviceWithMediaTypeWithPosition(AVMediaTypeVideo, position: AVCaptureDevicePosition.Front) {
do {
let input = try AVCaptureDeviceInput(device: device)
if self.session.canAddInput(input) {
self.session.addInput(input)
}
} catch {
print(error)
}
}
}

func deviceWithMediaTypeWithPosition(mediaType: NSString, position: AVCaptureDevicePosition) -> AVCaptureDevice? {
let devices: NSArray = AVCaptureDevice.devicesWithMediaType(mediaType as String)
if devices.count != 0 {
if var captureDevice: AVCaptureDevice = devices.firstObject as? AVCaptureDevice {

for device in devices {
let d = device as! AVCaptureDevice
if d.position == position {
captureDevice = d
break;
}
}
print(captureDevice)
return captureDevice
}
}
print("doesnt have any camera")
return nil

}

Answer

You need to remove the object and create new object with use of few values and boolean uses.

Here I post the code for the when create the position of the camera AVCaptureDevicePosition.

In the top of the class add enum.

enum CameraType {
    case Front
    case Back
}

Initialise the variable.

var cameraCheck = CameraType.Back

Just change the following function.

func addVideoInput() {
        if cameraCheck ==  CameraType.Front  {
            cameraCheck = CameraType.Back
            let device: AVCaptureDevice = self.deviceWithMediaTypeWithPosition(AVMediaTypeVideo, position: AVCaptureDevicePosition.Front)
            do {
                let input = try AVCaptureDeviceInput(device: device)
                if self.session.canAddInput(input) {
                    self.session.addInput(input)
                }
            } catch {
                print(error)
            }
        }else{
            cameraCheck = CameraType.Front
            let device: AVCaptureDevice = self.deviceWithMediaTypeWithPosition(AVMediaTypeVideo, position: AVCaptureDevicePosition.Back)
            do {
                let input = try AVCaptureDeviceInput(device: device)
                if self.session.canAddInput(input) {
                    self.session.addInput(input)
                }
            } catch {
                print(error)
            }
        }        
    }

Create one button into your storyboard.

Now into your viewcontroller create one @IBAction function.

@IBAction func changeCamera(){
        self.camera = nil

        self.initializeCamera()
        self.establishVideoPreviewArea()

        if isBackCamera == true {
           isBackCamera = false
            self.camera?.cameraCheck = CameraType.Front
        }else{
            isBackCamera = true
            self.camera?.cameraCheck = CameraType.Back
        }
    }

That's it your goal achieve.

Also you can download the source code from here.