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

feat(camera): wide zoom cameras in custom camera #948

Merged
merged 5 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
59 changes: 55 additions & 4 deletions Sources/Camera/ZLCustomCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,30 @@ open class ZLCustomCamera: UIViewController {
cameraConfigureFinish = true

sessionQueue.async {
self.setInitialZoomFactor(for: camera)
self.session.startRunning()
}
}

private func setInitialZoomFactor(for device: AVCaptureDevice) {
guard cameraConfig.enableWideCameras else { return }
do {
try device.lockForConfiguration()
device.videoZoomFactor = device.defaultZoomFactor
device.unlockForConfiguration()
} catch {
zl_debugPrint("Failed to set initial zoom factor: \(error.localizedDescription)")
}
}

private func findFirstDevice(ofTypes types: [AVCaptureDevice.DeviceType], in session: AVCaptureDevice.DiscoverySession) -> AVCaptureDevice? {
for type in types {
if let device = session.devices.first(where: { $0.deviceType == type }) {
return device
}
}
return nil
}

private func refreshSessionPreset(device: AVCaptureDevice) {
func setSessionPreset(_ preset: AVCaptureSession.Preset) {
Expand All @@ -593,8 +614,30 @@ open class ZLCustomCamera: UIViewController {
}

private func getCamera(position: AVCaptureDevice.Position) -> AVCaptureDevice? {
let devices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: .video, position: position).devices
for device in devices {
let deviceTypes: [AVCaptureDevice.DeviceType] = [.builtInWideAngleCamera]
var extendedDeviceTypes: [AVCaptureDevice.DeviceType] = []
let allDeviceTypes: [AVCaptureDevice.DeviceType]

if #available(iOS 13.0, *), cameraConfig.enableWideCameras {
extendedDeviceTypes = [.builtInTripleCamera, .builtInDualWideCamera, .builtInDualCamera]
allDeviceTypes = deviceTypes + extendedDeviceTypes
} else {
allDeviceTypes = deviceTypes
}

let session = AVCaptureDevice.DiscoverySession(
deviceTypes: allDeviceTypes,
mediaType: .video,
position: position
)

if #available(iOS 13.0, *), cameraConfig.enableWideCameras {
if let camera = findFirstDevice(ofTypes: extendedDeviceTypes, in: session) {
return camera
}
}

for device in session.devices {
if device.position == position {
return device
}
Expand Down Expand Up @@ -847,6 +890,7 @@ open class ZLCustomCamera: UIViewController {
self.session.addInput(currInput)
}

self.setInitialZoomFactor(for: newVideoInput.device)
self.session.commitConfiguration()
}
} catch {
Expand Down Expand Up @@ -1020,7 +1064,8 @@ open class ZLCustomCamera: UIViewController {
return 1
}
if #available(iOS 11.0, *) {
return min(15, device.maxAvailableVideoZoomFactor)
let factor = cameraConfig.enableWideCameras ? device.defaultZoomFactor : 1
return min(15 * factor, device.maxAvailableVideoZoomFactor)
} else {
return min(15, device.activeFormat.videoMaxZoomFactor)
}
Expand All @@ -1032,7 +1077,13 @@ open class ZLCustomCamera: UIViewController {
}
do {
try device.lockForConfiguration()
device.videoZoomFactor = zoomFactor
if cameraConfig.enableWideCameras {
let minZoomFactor = device.minAvailableVideoZoomFactor
Copy link
Owner

@longitachi longitachi Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@available(iOS 11.0, *)
open var minAvailableVideoZoomFactor: CGFloat { get }

The minAvailableVideoZoomFactor property supports iOS 11 at the minimum, and an error will be reported when running here.

I noticed that when you used enableWideCameras, there were several places where @available(iOS 13.0, *) was judged, so I think it would be better to mark the enableWideCameras property as @available(iOS 13.0, *), what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @longitachi, yes, it makes sense, I wanted this PR to have as few LOC changes as possible since an explicit availability check will still be needed for AVCaptureDevice.DeviceType. I provided changes, thanks!

let clampedZoomFactor = max(minZoomFactor, min(zoomFactor, getMaxZoomFactor()))
device.videoZoomFactor = clampedZoomFactor
} else {
device.videoZoomFactor = zoomFactor
}
device.unlockForConfiguration()
} catch {
zl_debugPrint("调整焦距失败 \(error.localizedDescription)")
Expand Down
46 changes: 46 additions & 0 deletions Sources/Extensions/AVCaptureDevice.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// AVCaptureDevice.swift
// ZLPhotoBrowser
//
// Created by tsinis on 2024/11/1.
//
// Copyright (c) 2020 Long Zhang <495181165@qq.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import AVFoundation

extension AVCaptureDevice {
var defaultZoomFactor: CGFloat {
let fallback: CGFloat = 1.0
guard #available(iOS 13.0, *) else { return fallback }

if let wideAngleIndex = self.constituentDevices.firstIndex(where: { $0.deviceType == .builtInWideAngleCamera }) {
guard wideAngleIndex >= 1 else { return fallback }
return CGFloat(self.virtualDeviceSwitchOverVideoZoomFactors[wideAngleIndex - 1].doubleValue)
}

return fallback
}

func normalizedZoomFactor(for zoomFactor: CGFloat) -> CGFloat {
zoomFactor / self.defaultZoomFactor
}
}

10 changes: 10 additions & 0 deletions Sources/General/ZLCameraConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public class ZLCameraConfiguration: NSObject {
/// If `allowTakePhoto` is true, `tapToRecordVideo` will be ignored.
public var tapToRecordVideo: Bool = false

/// Enable the use of wide cameras (e.g., .builtInTripleCamera, .builtInDualWideCamera, .builtInDualCamera).
/// Defaults to false.
public var enableWideCameras: Bool = false

/// Video export format for recording video and editing video. Defaults to mov.
public var videoExportType: ZLCameraConfiguration.VideoExportType = .mov

Expand Down Expand Up @@ -300,4 +304,10 @@ public extension ZLCameraConfiguration {
tapToRecordVideo = value
return self
}

@discardableResult
func enableWideCameras(_ value: Bool) -> ZLCameraConfiguration {
enableWideCameras = value
return self
}
}