Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
Added uWide camera support; added public var for capture device to be…
Browse files Browse the repository at this point in the history
… more customizable.
  • Loading branch information
radupaula committed Apr 1, 2022
1 parent 08c28d2 commit 2a85922
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion camera/CameraManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public enum CameraState {
}

public enum CameraDevice {
case front, back
case front, back, uWide
}

public enum CameraFlashMode: Int {
Expand Down Expand Up @@ -152,6 +152,18 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
return !hasFlashDevices.isEmpty
}()

/// Property to determine if current device has Ultra Wide camera.
open var hasuWideCamera: Bool = {
var frontDevices: AVCaptureDevice?
if #available(iOS 13.0, *) {
frontDevices = AVCaptureDevice.default(.builtInUltraWideCamera, for: .video, position: .back)
} else {
// Fallback on earlier versions
frontDevices = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)
}
return (frontDevices != nil)
}()

/**
Property to enable or disable flip animation when switch between back and front camera.
- note: Default value is **true**
Expand Down Expand Up @@ -192,6 +204,14 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
}

public var captureDevice: AVCaptureDevice? {
switch self.cameraDevice {
case .front: return frontCameraDevice
case .back: return backCameraDevice
case .uWide: return uWideCameraDevice
}
}

/// Property to change camera flash mode.
open var flashMode = CameraFlashMode.off {
didSet {
Expand Down Expand Up @@ -252,10 +272,22 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
return AVCaptureDevice.videoDevices.filter { $0.position == .front }.first
}()

public var currentPreset: AVCaptureSession.Preset = .high
public var hdrModeOn: Bool = false

fileprivate lazy var backCameraDevice: AVCaptureDevice? = {
return AVCaptureDevice.videoDevices.filter { $0.position == .back }.first
}()

fileprivate lazy var uWideCameraDevice: AVCaptureDevice? = {
if #available(iOS 13.0, *) {
return AVCaptureDevice.default(.builtInUltraWideCamera, for: .video, position: .back)
} else {
// Fallback on earlier versions
return AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)
}
}()

fileprivate lazy var mic: AVCaptureDevice? = {
return AVCaptureDevice.default(for: AVMediaType.audio)
}()
Expand Down Expand Up @@ -716,11 +748,14 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
return device.hasFlash
} else if device.position == .front && cameraDevice == .front {
return device.hasFlash
} else if device.position == .back, cameraDevice == .uWide {
return device.hasFlash
}
}
return false
}


// MARK: - AVCaptureFileOutputRecordingDelegate
public func fileOutput(captureOutput: AVCaptureFileOutput, didStartRecordingTo fileURL: URL, from connections: [AVCaptureConnection]) {
captureSession?.beginConfiguration()
Expand Down Expand Up @@ -811,6 +846,8 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
device = backCameraDevice
case .front:
device = frontCameraDevice
case .uWide:
device = uWideCameraDevice
}

do {
Expand Down Expand Up @@ -858,6 +895,8 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
device = backCameraDevice
case .front:
device = frontCameraDevice
case .uWide:
device = uWideCameraDevice
}

_changeExposureMode(mode: .continuousAutoExposure)
Expand Down Expand Up @@ -1027,6 +1066,8 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
device = backCameraDevice
case .front:
device = frontCameraDevice
case .uWide:
device = uWideCameraDevice
}
if (device?.exposureMode == mode) {
return
Expand All @@ -1052,6 +1093,8 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
device = backCameraDevice
case .front:
device = frontCameraDevice
case .uWide:
device = uWideCameraDevice
}

do {
Expand Down Expand Up @@ -1380,6 +1423,8 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
maxZoom = backCameraDevice.activeFormat.videoMaxZoomFactor
} else if cameraDevice == .front, let frontCameraDevice = frontCameraDevice {
maxZoom = frontCameraDevice.activeFormat.videoMaxZoomFactor
} else if cameraDevice == .uWide, let uWideCameraDevice = uWideCameraDevice {
maxZoom = uWideCameraDevice.activeFormat.videoMaxZoomFactor
}

maxZoomScale = maxZoom
Expand Down Expand Up @@ -1654,6 +1699,11 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
!inputs.contains(validBackDevice) {
validCaptureSession.addInput(validBackDevice)
}
case .uWide:
if let validuWideDevice = _deviceInputFromDevice(uWideCameraDevice),
!inputs.contains(validuWideDevice) {
validCaptureSession.addInput(validuWideDevice)
}
}
}
}
Expand Down

0 comments on commit 2a85922

Please sign in to comment.