Files
ux/darwin/Camera/PhotoOutput.swift
agra 8ab672c12a camera: per-platform capture-orientation extension + macOS sensor=0
macOS preview was stretching (aspect wrong) and macOS photo capture
was rotating the landscape sensor 90° because the shared
PhotoOutput / CameraInstance code was setting
`AVCaptureConnection.videoOrientation` from the orientation snapshot
unconditionally. iOS needs that to rotate sample buffers to portrait;
macOS desktop cams are physically landscape and any rotation just
skews the result.

Moved the rotation call behind a per-platform extension on
`AVCaptureConnection`:
  - `ios/Classes/Camera/AVCaptureConnection+iOS.swift` applies the
    snapshot orientation (current behavior).
  - `macos/Classes/Camera/AVCaptureConnection+macOS.swift` is a
    no-op. macOS-flavoured photos / preview frames now flow at
    native landscape orientation.

`CaptureDevice` reports sensorOrientation=0 on macOS (was hardcoded
90 for iOS); on macOS the page's `normalizeCameraCapture` math then
collapses to identity and the saved JPEG stays the landscape the
sensor produced. iOS keeps sensorOrientation=90 (matches
camera_avfoundation's reported value and the existing capture-
transform math).

Photo and video paths now both produce upright content on macOS
(video already worked because VideoRecorder's transform table maps
the always-portraitUp macOS snapshot to `.identity`).
2026-05-13 19:07:29 +03:00

117 lines
4.7 KiB
Swift

import AVFoundation
import Foundation
/// Wraps `AVCapturePhotoOutput`. One instance per
/// [CameraInstance]; gets added to the session at create time and
/// stays for the session's lifetime (no swap on camera flip the
/// output is generic, only the connection's video device changes).
///
/// `take(orientation:flashMode:completion:)` is the only public entry.
/// It sets the photo connection's `videoOrientation` to the
/// snapshotted orientation just before firing, hands off to a
/// per-capture delegate, and resets the connection back to portrait
/// afterward (so a takePicture without an explicit snapshot should
/// the path ever exist falls back cleanly).
final class PhotoOutput {
let avOutput = AVCapturePhotoOutput()
private var inFlight: PhotoCaptureDelegate?
/// Capture a single still. [orientation] applies to the photo
/// connection. [flashMode] is applied to the per-shot
/// `AVCapturePhotoSettings`. [completion] is invoked on `.main`
/// with either the saved file path or an `NSError`.
func take(
orientation: DeviceOrientationFlutter,
flashMode: AVCaptureDevice.FlashMode,
completion: @escaping (Result<String, NSError>) -> Void
) {
guard let connection = avOutput.connection(with: .video) else {
completion(.failure(NSError(
domain: "ux.camera",
code: -1,
userInfo: [NSLocalizedDescriptionKey: "Photo connection unavailable"]
)))
return
}
// Rotation handled per-platform: iOS applies the snapshot
// orientation to the connection (which rotates the captured
// JPEG); macOS is a no-op (desktop cams are physically
// landscape, any rotation skews the photo). See
// `AVCaptureConnection+iOS.swift` / `+macOS.swift`.
connection.applyUxCaptureOrientation(orientation)
// The recorded photo carries no mirror; mirroring is a
// preview-only concern.
if connection.isVideoMirroringSupported {
connection.automaticallyAdjustsVideoMirroring = false
connection.isVideoMirrored = false
}
let settings = AVCapturePhotoSettings()
// `supportedFlashModes` arrived in macOS 11; `flashMode` setter
// in macOS 13. iOS has both since iOS 10. Gated via Swift
// availability so we don't have to bump the macOS deployment
// target just to use a flash that almost no Mac has anyway.
if #available(macOS 11.0, *),
avOutput.supportedFlashModes.contains(flashMode) {
if #available(macOS 13.0, *) {
settings.flashMode = flashMode
}
}
let delegate = PhotoCaptureDelegate { [weak self] result in
// Reset orientation back to portraitUp on the photo
// connection so a follow-up shot without an explicit
// snapshot defaults cleanly. No-op on macOS (the
// extension method is empty there).
self?.avOutput.connection(with: .video)?
.applyUxCaptureOrientation(.portraitUp)
self?.inFlight = nil
DispatchQueue.main.async { completion(result) }
}
// Retain the delegate for the duration of the capture
// AVCapturePhotoOutput holds it weakly.
inFlight = delegate
avOutput.capturePhoto(with: settings, delegate: delegate)
}
}
/// Per-shot delegate. Receives the photo, writes
/// `fileDataRepresentation()` to a unique path under
/// `NSTemporaryDirectory()`, invokes the completion. The plugin
/// retains it via [PhotoOutput.inFlight] across the async hop.
private final class PhotoCaptureDelegate: NSObject, AVCapturePhotoCaptureDelegate {
private let completion: (Result<String, NSError>) -> Void
init(completion: @escaping (Result<String, NSError>) -> Void) {
self.completion = completion
}
func photoOutput(
_ output: AVCapturePhotoOutput,
didFinishProcessingPhoto photo: AVCapturePhoto,
error: Error?
) {
if let error = error as NSError? {
completion(.failure(error))
return
}
guard let data = photo.fileDataRepresentation() else {
completion(.failure(NSError(
domain: "ux.camera",
code: -2,
userInfo: [NSLocalizedDescriptionKey: "No photo data"]
)))
return
}
let url = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent("ux_camera_\(UUID().uuidString).jpg")
do {
try data.write(to: url, options: .atomic)
completion(.success(url.path))
} catch let error as NSError {
completion(.failure(error))
}
}
}