How to Get the Front Camera in Swift

Accessing front Camera with swift?

Your currently void frontCamera has a pretty similar behavior to your backCamera one. You need to use devicesWithMediaType(_ mediaType: String!) that allows to store in an array all the devices capable of capturing data of type AVMediaTypeVideo.

let captureDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
var frontCamera: AVCaptureDevice

for device in captureDevices {
let device = element as! AVCaptureDevice
if device.position == AVCaptureDevicePosition.Front {
frontCamera = device
break
}
}

Access front camera in Swift 4

You can set up your AVCaptureDevice this way. Just need to set your default position to front camera.

        var defaultVideoDevice: AVCaptureDevice?

// Choose the back dual camera if available, otherwise default to a wide angle camera.
if let dualCameraDevice = AVCaptureDevice.default(.builtInDualCamera, for: AVMediaType.video, position: .back) {
defaultVideoDevice = dualCameraDevice
}

else if let backCameraDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back) {
defaultVideoDevice = backCameraDevice
}

else if let frontCameraDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .front) {
defaultVideoDevice = frontCameraDevice
}

let videoDeviceInput = try AVCaptureDeviceInput(device: defaultVideoDevice!)

Can't use front camera in Swift

The front camera for iPhone 7 and iPhone 7 Plus can record 1080p but on older models it can only record 720p. Thus, you just need to change the session preset, i.e.:

captureSesssion.sessionPreset = AVCaptureSessionPreset1280x720

Swift 4:

captureSesssion.sessionPreset = AVCaptureSession.Preset.hd1280x720

How to get front camera, back camera and audio with AVCaptureDeviceDiscoverySession

You can get the front camera with the following:

AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .front)

The back camera:

AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back)

And the microphone:

AVCaptureDevice.default(.builtInMicrophone, for: AVMediaType.audio, position: .unspecified)

UIImagePickerController - How do I access the front facing camera by default?

UIImagePickerControllerCameraDeviceFront is not a valid enum for the sourceType property. The sourceType property defines whether you're using the camera or the photo library. You need to set the cameraDevice property instead.

Objective-C

self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imgPicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;

Swift 2

imgPicker.sourceType = .Camera
imgPicker.cameraDevice = .Front

Swift 3

imgPicker.sourceType = .camera
imgPicker.cameraDevice = .front

Switching my cameras back/front with a Button

you don't need to declare variables for backCamera, frontCamera, currentCamera, you only need to save variables for AVCaptureDeviceInput.

try to comment:

//    var backCamera: AVCaptureDevice?
// var frontCamera: AVCaptureDevice?
// var currentCamera: AVCaptureDevice?

and add those:

var frontCameraDeviceInput: AVCaptureDeviceInput?
var backCameraDeviceInput: AVCaptureDeviceInput?

your setupDevice function looks like this

func setupDevice() {
let frontCamera = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front)
let backCamera = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)

frontCameraDeviceInput = try? AVCaptureDeviceInput(device: frontCamera!)
backCameraDeviceInput = try? AVCaptureDeviceInput(device: backCamera!)
}

change setupInputOutput function to:

func setupInputOutput() {
captureSession.addInput(backCameraDeviceInput!)
photoOutput = AVCapturePhotoOutput()
photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format:[AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: nil)
captureSession.addOutput(photoOutput!)
}

and finally your switchCamera

@IBAction func switchCamera(_ sender: Any) {
// The button which should switch the Cameras back an forth.

captureSession.beginConfiguration()
//Change camera device inputs from back to front or opposite
if captureSession.inputs.contains(frontCameraDeviceInput!) == true {
captureSession.removeInput(frontCameraDeviceInput!)
captureSession.addInput(backCameraDeviceInput!)
} else if captureSession.inputs.contains(backCameraDeviceInput!) == true {
captureSession.removeInput(backCameraDeviceInput!)
captureSession.addInput(frontCameraDeviceInput!)
}

//Commit all the configuration changes at once
captureSession.commitConfiguration();
}

How to focus FRONT camera on device using iOS, swift?

The front-facing camera does not support tap-to-focus on any iPhone. You can use device.focusPointOfInterestSupported property to check if you can do tap-to-focus (but you will get false, as with isFocusModeSupported())

What you are seeing is tap-for-exposure, and you can check for that with device.exposurePointOfInterestSupported. Once you know that you can use it, use device.exposurePointOfInterest to set your PoI.

All the details of each mode is explained to detail in Apple Docs

Hope it helps!



Related Topics



Leave a reply



Submit