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

Front camera #68

Merged
merged 6 commits into from
Jan 9, 2025
Merged
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 YOLO/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>55</string>
<string>83</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSRequiresIPhoneOS</key>
Expand Down
7 changes: 7 additions & 0 deletions YOLO/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@
<action selector="shareButton:" destination="BYZ-38-t0r" id="t5S-8x-M27"/>
</connections>
</barButtonItem>
<barButtonItem enabled="NO" width="20" systemItem="fixedSpace" id="vzs-nM-KXr"/>
<barButtonItem image="camera.rotate" catalog="system" springLoaded="YES" id="tPm-bz-9sp">
<connections>
<action selector="switchCameraTapped:" destination="BYZ-38-t0r" id="Jtd-Ds-swh"/>
</connections>
</barButtonItem>
<barButtonItem enabled="NO" width="20" systemItem="fixedSpace" id="JO8-jv-HRS"/>
<barButtonItem enabled="NO" systemItem="flexibleSpace" id="aMO-nw-6x7"/>
</items>
Expand Down Expand Up @@ -329,6 +335,7 @@
<color key="tintColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<resources>
<image name="Focus" width="414" height="414"/>
<image name="camera.rotate" catalog="system" width="128" height="93"/>
<image name="ultralytics_yolo_logotype.png" width="1406" height="394"/>
<systemColor name="groupTableViewBackgroundColor">
<color red="0.94901960784313721" green="0.94901960784313721" blue="0.96862745098039216" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand Down
54 changes: 41 additions & 13 deletions YOLO/VideoCapture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,45 @@ public protocol VideoCaptureDelegate: AnyObject {
}

// Identifies the best available camera device based on user preferences and device capabilities.
func bestCaptureDevice() -> AVCaptureDevice {
if UserDefaults.standard.bool(forKey: "use_telephoto"),
let device = AVCaptureDevice.default(.builtInTelephotoCamera, for: .video, position: .back)
{
return device
} else if let device = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .back) {
return device
} else if let device = AVCaptureDevice.default(
.builtInWideAngleCamera, for: .video, position: .back)
{
return device
func bestCaptureDevice(for position: AVCaptureDevice.Position) -> AVCaptureDevice {
if position == .back {
// バックカメラの場合
if UserDefaults.standard.bool(forKey: "use_telephoto"),
let device = AVCaptureDevice.default(.builtInTelephotoCamera, for: .video, position: .back)
{
return device
} else if let device = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .back)
{
return device
} else if let device = AVCaptureDevice.default(
.builtInWideAngleCamera, for: .video, position: .back)
{
return device
} else {
fatalError("Expected back camera device is not available.")
}
} else if position == .front {
// フロントカメラの場合
if let device = AVCaptureDevice.default(.builtInTrueDepthCamera, for: .video, position: .front)
{
return device
} else if let device = AVCaptureDevice.default(
.builtInWideAngleCamera, for: .video, position: .front)
{
return device
} else {
fatalError("Expected front camera device is not available.")
}
} else {
fatalError("Expected back camera device is not available.")
fatalError("Unsupported camera position: \(position)")
}
}

public class VideoCapture: NSObject {
public var previewLayer: AVCaptureVideoPreviewLayer?
public weak var delegate: VideoCaptureDelegate?

let captureDevice = bestCaptureDevice()
let captureDevice = bestCaptureDevice(for: .back)
let captureSession = AVCaptureSession()
let videoOutput = AVCaptureVideoDataOutput()
var cameraOutput = AVCapturePhotoOutput()
Expand Down Expand Up @@ -137,6 +155,7 @@ public class VideoCapture: NSObject {
captureSession.stopRunning()
}
}

func updateVideoOrientation() {
guard let connection = videoOutput.connection(with: .video) else { return }
switch UIDevice.current.orientation {
Expand All @@ -151,8 +170,17 @@ public class VideoCapture: NSObject {
default:
return
}

let currentInput = self.captureSession.inputs.first as? AVCaptureDeviceInput
if currentInput?.device.position == .front {
connection.isVideoMirrored = true
} else {
connection.isVideoMirrored = false
}

self.previewLayer?.connection?.videoOrientation = connection.videoOrientation
}

}

// Extension to handle AVCaptureVideoDataOutputSampleBufferDelegate events.
Expand Down
24 changes: 18 additions & 6 deletions YOLO/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,21 @@ class ViewController: UIViewController {
self.videoCapture.captureSession.beginConfiguration()
let currentInput = self.videoCapture.captureSession.inputs.first as? AVCaptureDeviceInput
self.videoCapture.captureSession.removeInput(currentInput!)
// let newCameraDevice = currentInput?.device == .builtInWideAngleCamera ? getCamera(with: .front) : getCamera(with: .back)
guard let currentPosition = currentInput?.device.position else { return }

let device = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)!
guard let videoInput1 = try? AVCaptureDeviceInput(device: device) else {
let nextCameraPosition: AVCaptureDevice.Position = currentPosition == .back ? .front : .back

let newCameraDevice = bestCaptureDevice(for: nextCameraPosition)

guard let videoInput1 = try? AVCaptureDeviceInput(device: newCameraDevice) else {
return
}

self.videoCapture.captureSession.addInput(videoInput1)
self.videoCapture.updateVideoOrientation()

self.videoCapture.captureSession.commitConfiguration()

}

// share image
Expand Down Expand Up @@ -707,12 +713,18 @@ extension ViewController: AVCapturePhotoCaptureDelegate {
let cgImageRef: CGImage! = CGImage(
jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true,
intent: .defaultIntent)
var orientation = CGImagePropertyOrientation.right
var isCameraFront = false
if let currentInput = self.videoCapture.captureSession.inputs.first as? AVCaptureDeviceInput,
currentInput.device.position == .front
{
isCameraFront = true
}
var orientation: CGImagePropertyOrientation = isCameraFront ? .leftMirrored : .right
switch UIDevice.current.orientation {
case .landscapeLeft:
orientation = .up
orientation = isCameraFront ? .downMirrored : .up
case .landscapeRight:
orientation = .down
orientation = isCameraFront ? .upMirrored : .down
default:
break
}
Expand Down
Loading