Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Triple/Dual Cameras #492

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Demo/ImagePickerDemo/Podfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
platform :ios, '9.2'
platform :ios, '13.0'

use_frameworks!
inhibit_all_warnings!
Expand Down
2 changes: 1 addition & 1 deletion ImagePicker.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Pod::Spec.new do |s|
s.author = { "Hyper Interaktiv AS" => "[email protected]" }
s.source = { :git => "https://github.com/hyperoslo/ImagePicker.git", :tag => s.version.to_s }
s.social_media_url = 'https://twitter.com/hyperoslo'
s.platform = :ios, '9.0'
s.platform = :ios, '13.0'
s.requires_arc = true
s.source_files = 'Source/**/*'
s.resource_bundles = { 'ImagePicker' => ['Images/*.{png}'] }
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription
let package = Package(
name: "ImagePicker",
platforms: [
.iOS(.v9)
.iOS(.v13)
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
Expand Down
78 changes: 45 additions & 33 deletions Source/CameraView/CameraMan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,48 @@ class CameraMan {
func setupDevices() {
// Input
AVCaptureDevice
.devices()
.filter {
return $0.hasMediaType(AVMediaType.video)
}.forEach {
.DiscoverySession(deviceTypes: [.builtInTripleCamera,.builtInTelephotoCamera], mediaType: AVMediaType.video, position: .front)
.devices
.forEach {
switch $0.position {
case .front:
self.frontCamera = try? AVCaptureDeviceInput(device: $0)
case .back:
self.backCamera = try? AVCaptureDeviceInput(device: $0)

default:
break
}
}

let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTripleCamera, .builtInDualCamera, .builtInWideAngleCamera], mediaType: .video, position: .back)
var selectedDevice: AVCaptureDevice?

// Attempt to find a triple camera first
if let tripleCameraDevice = discoverySession.devices.first(where: { $0.deviceType == .builtInTripleCamera }) {
selectedDevice = tripleCameraDevice
} else if let dualCameraDevice = discoverySession.devices.first(where: { $0.deviceType == .builtInDualCamera }) {
// Fallback to dual camera if no triple camera is found
selectedDevice = dualCameraDevice
} else if let wideAngleCameraDevice = discoverySession.devices.first(where: { $0.deviceType == .builtInWideAngleCamera }) {
// Fallback to wide angle camera if no dual camera is found
selectedDevice = wideAngleCameraDevice
}

// If a device was selected, create the AVCaptureDeviceInput
if let selectedDevice = selectedDevice {
do {
let backCameraInput = try AVCaptureDeviceInput(device: selectedDevice)
self.backCamera = backCameraInput
} catch {
print("Error creating AVCaptureDeviceInput for the selected device: \(error)")
}
}

// Output
stillImageOutput = AVCaptureStillImageOutput()
stillImageOutput?.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
}

func addInput(_ input: AVCaptureDeviceInput) {
configurePreset(input)

if session.canAddInput(input) {
session.addInput(input)
Expand Down Expand Up @@ -98,6 +119,10 @@ class CameraMan {
}

fileprivate func start() {
self.session.beginConfiguration()

session.sessionPreset = .photo

// Devices
setupDevices()

Expand All @@ -109,6 +134,8 @@ class CameraMan {
session.addOutput(output)
}

self.session.commitConfiguration()

queue.async {
self.session.startRunning()

Expand Down Expand Up @@ -203,15 +230,19 @@ class CameraMan {
}
}

func zoom(_ zoomFactor: CGFloat) {
guard let device = currentInput?.device, device.position == .back else { return }
func zoom(_ zoomFactor: CGFloat) {
guard var device = currentInput?.device, device.position == .back else { return }

queue.async {
self.lock {
device.videoZoomFactor = zoomFactor
}
queue.async {
do
{
var newZoomFactor = zoomFactor < 1.0 ? 1.0 : zoomFactor
self.lock {
device.ramp(toVideoZoomFactor: newZoomFactor, withRate: 4.0)
}
}
}
}
}

// MARK: - Lock

Expand All @@ -228,23 +259,4 @@ class CameraMan {
block()
session.commitConfiguration()
}

// MARK: - Preset

func configurePreset(_ input: AVCaptureDeviceInput) {
for asset in preferredPresets() {
if input.device.supportsSessionPreset(AVCaptureSession.Preset(rawValue: asset)) && self.session.canSetSessionPreset(AVCaptureSession.Preset(rawValue: asset)) {
self.session.sessionPreset = AVCaptureSession.Preset(rawValue: asset)
return
}
}
}

func preferredPresets() -> [String] {
return [
AVCaptureSession.Preset.high.rawValue,
AVCaptureSession.Preset.high.rawValue,
AVCaptureSession.Preset.low.rawValue
]
}
}
9 changes: 2 additions & 7 deletions Source/CameraView/CameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ class CameraView: UIViewController, CLLocationManagerDelegate, CameraManDelegate
var locationManager: LocationManager?
var startOnFrontCamera: Bool = false

private let minimumZoomFactor: CGFloat = 1.0
private let maximumZoomFactor: CGFloat = 3.0

private var currentZoomFactor: CGFloat = 1.0
private var previousZoomFactor: CGFloat = 1.0

Expand Down Expand Up @@ -156,7 +153,7 @@ class CameraView: UIViewController, CLLocationManagerDelegate, CameraManDelegate

layer.backgroundColor = configuration.mainColor.cgColor
layer.autoreverses = true
layer.videoGravity = AVLayerVideoGravity.resizeAspectFill
layer.videoGravity = AVLayerVideoGravity.resizeAspect

view.layer.insertSublayer(layer, at: 0)
layer.frame = view.layer.frame
Expand Down Expand Up @@ -264,11 +261,9 @@ class CameraView: UIViewController, CLLocationManagerDelegate, CameraManDelegate
func zoomTo(_ zoomFactor: CGFloat) {
guard let device = cameraMan.currentInput?.device else { return }

let maximumDeviceZoomFactor = device.activeFormat.videoMaxZoomFactor
let newZoomFactor = previousZoomFactor * zoomFactor
currentZoomFactor = min(maximumZoomFactor, max(minimumZoomFactor, min(newZoomFactor, maximumDeviceZoomFactor)))

cameraMan.zoom(currentZoomFactor)
cameraMan.zoom(newZoomFactor)
}

// MARK: - Tap
Expand Down