@capgo/camera-preview 8.12.5 → 8.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -12
- package/android/src/main/java/app/capgo/capacitor/camera/preview/CameraPreview.java +13 -2
- package/android/src/main/java/app/capgo/capacitor/camera/preview/CameraXView.java +17 -4
- package/android/src/main/java/app/capgo/capacitor/camera/preview/CaptureOrientationResolver.java +28 -0
- package/android/src/test/java/app/capgo/capacitor/camera/preview/CaptureOrientationResolverTest.java +31 -0
- package/dist/docs.json +16 -0
- package/dist/esm/definitions.d.ts +8 -0
- package/dist/esm/definitions.js.map +1 -1
- package/ios/Sources/CapgoCameraPreviewPlugin/CameraController.swift +94 -33
- package/ios/Sources/CapgoCameraPreviewPlugin/CaptureExifGps.swift +58 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/CaptureImageExifEncoder.swift +79 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/CaptureLifecycleCoordination.swift +37 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/CaptureOrientationResolver.swift +23 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/Plugin.swift +33 -85
- package/ios/Tests/CameraPreviewPluginTests/CaptureExifGpsTests.swift +95 -0
- package/ios/Tests/CameraPreviewPluginTests/CaptureImageExifEncoderTests.swift +141 -0
- package/ios/Tests/CameraPreviewPluginTests/CaptureLifecycleCoordinationTests.swift +73 -0
- package/ios/Tests/CameraPreviewPluginTests/CaptureOrientationResolverTests.swift +30 -0
- package/package.json +1 -1
|
@@ -55,6 +55,22 @@ class CameraController: NSObject, CXCallObserverDelegate {
|
|
|
55
55
|
return orientation
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
private func getPreviewLayerVideoOrientation() -> AVCaptureVideoOrientation? {
|
|
59
|
+
if Thread.isMainThread {
|
|
60
|
+
if let connection = self.previewLayer?.connection, connection.isVideoOrientationSupported {
|
|
61
|
+
return connection.videoOrientation
|
|
62
|
+
}
|
|
63
|
+
return nil
|
|
64
|
+
}
|
|
65
|
+
var orientation: AVCaptureVideoOrientation?
|
|
66
|
+
DispatchQueue.main.sync {
|
|
67
|
+
if let connection = self.previewLayer?.connection, connection.isVideoOrientationSupported {
|
|
68
|
+
orientation = connection.videoOrientation
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return orientation
|
|
72
|
+
}
|
|
73
|
+
|
|
58
74
|
// For capture only - uses accelerometer to detect physical orientation to properly position videos/images
|
|
59
75
|
private var lastReliableCaptureOrientation: AVCaptureVideoOrientation?
|
|
60
76
|
|
|
@@ -482,9 +498,30 @@ class CameraController: NSObject, CXCallObserverDelegate {
|
|
|
482
498
|
// Track output preparation status
|
|
483
499
|
private var outputsPrepared: Bool = false
|
|
484
500
|
|
|
485
|
-
// Capture/stop coordination
|
|
486
|
-
var
|
|
487
|
-
|
|
501
|
+
// Capture/stop coordination (main-thread only; see CaptureLifecycleCoordination)
|
|
502
|
+
var captureLifecycle = CaptureLifecycleState()
|
|
503
|
+
|
|
504
|
+
var isCapturingPhoto: Bool {
|
|
505
|
+
get { self.captureLifecycle.isCapturingPhoto }
|
|
506
|
+
set { self.captureLifecycle.isCapturingPhoto = newValue }
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
var stopRequestedAfterCapture: Bool {
|
|
510
|
+
get { self.captureLifecycle.stopRequestedAfterCapture }
|
|
511
|
+
set { self.captureLifecycle.stopRequestedAfterCapture = newValue }
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
func finishPhotoCaptureLifecycle() {
|
|
515
|
+
let work = { [weak self] in
|
|
516
|
+
guard let self = self else { return }
|
|
517
|
+
CaptureLifecycleCoordination.finishCapture(state: &self.captureLifecycle, cleanup: { self.cleanup() })
|
|
518
|
+
}
|
|
519
|
+
if Thread.isMainThread {
|
|
520
|
+
work()
|
|
521
|
+
} else {
|
|
522
|
+
DispatchQueue.main.async(execute: work)
|
|
523
|
+
}
|
|
524
|
+
}
|
|
488
525
|
|
|
489
526
|
var isUsingMultiLensVirtualCamera: Bool {
|
|
490
527
|
guard let device = (currentCameraPosition == .rear) ? rearCamera : frontCamera else { return false }
|
|
@@ -1505,21 +1542,29 @@ extension CameraController {
|
|
|
1505
1542
|
self.updateVideoOrientation()
|
|
1506
1543
|
}
|
|
1507
1544
|
|
|
1508
|
-
func captureImage(width: Int?, height: Int?, quality: Float, gpsLocation: CLLocation?, embedTimestamp: Bool, embedLocation: Bool, photoQualityPrioritization: String, completion: @escaping (UIImage?, Data?, [AnyHashable: Any]?, Error?) -> Void) {
|
|
1545
|
+
func captureImage(width: Int?, height: Int?, quality: Float, gpsLocation: CLLocation?, embedTimestamp: Bool, embedLocation: Bool, photoQualityPrioritization: String, captureOrientationMode: String?, completion: @escaping (UIImage?, Data?, [AnyHashable: Any]?, Error?) -> Void) {
|
|
1509
1546
|
guard let photoOutput = self.photoOutput else {
|
|
1510
1547
|
completion(nil, nil, nil, NSError(domain: "Camera", code: 0, userInfo: [NSLocalizedDescriptionKey: "Photo output is not available"]))
|
|
1511
1548
|
return
|
|
1512
1549
|
}
|
|
1513
1550
|
|
|
1514
|
-
|
|
1551
|
+
var orientationUsedForCapture = self.getPhysicalOrientation()
|
|
1515
1552
|
if let connection = photoOutput.connection(with: .video) {
|
|
1516
|
-
let
|
|
1517
|
-
|
|
1553
|
+
let previewOrientation = self.getPreviewLayerVideoOrientation() ?? self.getVideoOrientation()
|
|
1554
|
+
let captureOrientation = CaptureOrientationResolver.resolveOrientation(
|
|
1555
|
+
mode: captureOrientationMode,
|
|
1556
|
+
previewOrientation: previewOrientation,
|
|
1557
|
+
deviceOrientation: self.getPhysicalOrientation()
|
|
1558
|
+
)
|
|
1559
|
+
orientationUsedForCapture = captureOrientation
|
|
1560
|
+
if CaptureOrientationResolver.usesDeviceOrientation(mode: captureOrientationMode) {
|
|
1561
|
+
self.lastCaptureOrientation = captureOrientation
|
|
1562
|
+
}
|
|
1518
1563
|
self.setVideoOrientation(captureOrientation, on: connection)
|
|
1519
1564
|
}
|
|
1520
1565
|
// For fill + cover the preview shows a centered crop of the frame. Snapshot its
|
|
1521
1566
|
// shape now so the photo can be cropped to the same visible area.
|
|
1522
|
-
let visibleFillRatio = self.visibleFillCaptureAspectRatio()
|
|
1567
|
+
let visibleFillRatio = self.visibleFillCaptureAspectRatio(captureOrientation: orientationUsedForCapture)
|
|
1523
1568
|
let settings = AVCapturePhotoSettings()
|
|
1524
1569
|
// Configure photo capture settings optimized for speed
|
|
1525
1570
|
let shouldUseHighRes = CaptureResolutionSupport.shouldEnableHighResolutionPhoto(
|
|
@@ -1563,27 +1608,25 @@ extension CameraController {
|
|
|
1563
1608
|
}
|
|
1564
1609
|
}
|
|
1565
1610
|
|
|
1566
|
-
|
|
1611
|
+
if Thread.isMainThread {
|
|
1612
|
+
self.isCapturingPhoto = true
|
|
1613
|
+
} else {
|
|
1614
|
+
DispatchQueue.main.sync {
|
|
1615
|
+
self.isCapturingPhoto = true
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1567
1618
|
|
|
1568
1619
|
self.photoCaptureCompletionBlock = { [weak self] (image, photoData, metadata, error) in
|
|
1569
1620
|
guard let self = self else { return }
|
|
1570
1621
|
if let error = error {
|
|
1571
1622
|
completion(nil, nil, nil, error)
|
|
1572
|
-
|
|
1573
|
-
self.isCapturingPhoto = false
|
|
1574
|
-
if self.stopRequestedAfterCapture {
|
|
1575
|
-
DispatchQueue.main.async { self.cleanup(); self.stopRequestedAfterCapture = false }
|
|
1576
|
-
}
|
|
1623
|
+
self.finishPhotoCaptureLifecycle()
|
|
1577
1624
|
return
|
|
1578
1625
|
}
|
|
1579
1626
|
|
|
1580
1627
|
guard let image = image else {
|
|
1581
1628
|
completion(nil, nil, nil, NSError(domain: "Camera", code: 0, userInfo: [NSLocalizedDescriptionKey: "Failed to capture image"]))
|
|
1582
|
-
|
|
1583
|
-
self.isCapturingPhoto = false
|
|
1584
|
-
if self.stopRequestedAfterCapture {
|
|
1585
|
-
DispatchQueue.main.async { self.cleanup(); self.stopRequestedAfterCapture = false }
|
|
1586
|
-
}
|
|
1629
|
+
self.finishPhotoCaptureLifecycle()
|
|
1587
1630
|
return
|
|
1588
1631
|
}
|
|
1589
1632
|
|
|
@@ -1598,7 +1641,12 @@ extension CameraController {
|
|
|
1598
1641
|
// When max dimensions are specified, we used high-res capture
|
|
1599
1642
|
// First crop to aspect ratio if needed, then resize to max dimensions
|
|
1600
1643
|
if let aspectRatio = self.requestedAspectRatio {
|
|
1601
|
-
finalImage = self.cropCapturedImage(
|
|
1644
|
+
finalImage = self.cropCapturedImage(
|
|
1645
|
+
image,
|
|
1646
|
+
aspectRatio: aspectRatio,
|
|
1647
|
+
visibleFillRatio: visibleFillRatio,
|
|
1648
|
+
captureOrientation: orientationUsedForCapture
|
|
1649
|
+
)
|
|
1602
1650
|
print("[CameraPreview] Cropped high-res image to aspect ratio \(aspectRatio)")
|
|
1603
1651
|
}
|
|
1604
1652
|
// Then resize to fit within maximum dimensions while maintaining aspect ratio
|
|
@@ -1607,7 +1655,12 @@ extension CameraController {
|
|
|
1607
1655
|
} else if let aspectRatio = self.requestedAspectRatio {
|
|
1608
1656
|
// No max dimensions specified, but aspect ratio is specified
|
|
1609
1657
|
// Always apply aspect ratio cropping to ensure correct orientation
|
|
1610
|
-
finalImage = self.cropCapturedImage(
|
|
1658
|
+
finalImage = self.cropCapturedImage(
|
|
1659
|
+
image,
|
|
1660
|
+
aspectRatio: aspectRatio,
|
|
1661
|
+
visibleFillRatio: visibleFillRatio,
|
|
1662
|
+
captureOrientation: orientationUsedForCapture
|
|
1663
|
+
)
|
|
1611
1664
|
print("[CameraPreview] Applied aspect ratio cropping for \(aspectRatio): \(finalImage.size.width)x\(finalImage.size.height)")
|
|
1612
1665
|
}
|
|
1613
1666
|
|
|
@@ -1629,12 +1682,7 @@ extension CameraController {
|
|
|
1629
1682
|
}
|
|
1630
1683
|
|
|
1631
1684
|
completion(finalImage, photoData, metadata, nil)
|
|
1632
|
-
|
|
1633
|
-
// End capture lifecycle
|
|
1634
|
-
self.isCapturingPhoto = false
|
|
1635
|
-
if self.stopRequestedAfterCapture {
|
|
1636
|
-
DispatchQueue.main.async { self.cleanup(); self.stopRequestedAfterCapture = false }
|
|
1637
|
-
}
|
|
1685
|
+
self.finishPhotoCaptureLifecycle()
|
|
1638
1686
|
}
|
|
1639
1687
|
|
|
1640
1688
|
photoOutput.capturePhoto(with: settings, delegate: self)
|
|
@@ -1926,7 +1974,7 @@ extension CameraController {
|
|
|
1926
1974
|
|
|
1927
1975
|
/// Ratio of the visible preview area for `fill` + `cover`, in capture orientation.
|
|
1928
1976
|
/// Returns `nil` for numeric ratios and for `contain`, where the full frame is visible.
|
|
1929
|
-
func visibleFillCaptureAspectRatio() -> CGFloat? {
|
|
1977
|
+
func visibleFillCaptureAspectRatio(captureOrientation: AVCaptureVideoOrientation? = nil) -> CGFloat? {
|
|
1930
1978
|
guard let aspectRatio = self.requestedAspectRatio,
|
|
1931
1979
|
AspectRatioLayout.isFillMode(aspectRatio),
|
|
1932
1980
|
self.requestedAspectMode == "cover",
|
|
@@ -1934,7 +1982,7 @@ extension CameraController {
|
|
|
1934
1982
|
return nil
|
|
1935
1983
|
}
|
|
1936
1984
|
|
|
1937
|
-
let orientation = self.lastCaptureOrientation ?? self.getPhysicalOrientation()
|
|
1985
|
+
let orientation = captureOrientation ?? self.lastCaptureOrientation ?? self.getPhysicalOrientation()
|
|
1938
1986
|
let captureIsPortrait = orientation == .portrait || orientation == .portraitUpsideDown
|
|
1939
1987
|
let screenBounds = UIScreen.main.bounds
|
|
1940
1988
|
return AspectRatioLayout.visibleFillCaptureAspectRatio(
|
|
@@ -1945,7 +1993,12 @@ extension CameraController {
|
|
|
1945
1993
|
}
|
|
1946
1994
|
|
|
1947
1995
|
/// Crops a captured photo so it matches what the preview shows.
|
|
1948
|
-
func cropCapturedImage(
|
|
1996
|
+
func cropCapturedImage(
|
|
1997
|
+
_ image: UIImage,
|
|
1998
|
+
aspectRatio: String,
|
|
1999
|
+
visibleFillRatio: CGFloat?,
|
|
2000
|
+
captureOrientation: AVCaptureVideoOrientation? = nil
|
|
2001
|
+
) -> UIImage {
|
|
1949
2002
|
if AspectRatioLayout.isFillMode(aspectRatio) {
|
|
1950
2003
|
guard let ratio = visibleFillRatio else {
|
|
1951
2004
|
// fill + contain shows the whole frame, so keep the full photo.
|
|
@@ -1953,7 +2006,11 @@ extension CameraController {
|
|
|
1953
2006
|
}
|
|
1954
2007
|
return cropImage(image, toAspectRatio: ratio) ?? image
|
|
1955
2008
|
}
|
|
1956
|
-
return cropImageToAspectRatio(
|
|
2009
|
+
return cropImageToAspectRatio(
|
|
2010
|
+
image: image,
|
|
2011
|
+
aspectRatio: aspectRatio,
|
|
2012
|
+
captureOrientation: captureOrientation
|
|
2013
|
+
) ?? image
|
|
1957
2014
|
}
|
|
1958
2015
|
|
|
1959
2016
|
/// Center-crops `image` (after normalizing its orientation) to a width / height ratio.
|
|
@@ -1973,7 +2030,11 @@ extension CameraController {
|
|
|
1973
2030
|
return UIImage(cgImage: croppedCGImage, scale: normalizedImage.scale, orientation: .up)
|
|
1974
2031
|
}
|
|
1975
2032
|
|
|
1976
|
-
func cropImageToAspectRatio(
|
|
2033
|
+
func cropImageToAspectRatio(
|
|
2034
|
+
image: UIImage,
|
|
2035
|
+
aspectRatio: String,
|
|
2036
|
+
captureOrientation: AVCaptureVideoOrientation? = nil
|
|
2037
|
+
) -> UIImage? {
|
|
1977
2038
|
let components = aspectRatio.split(separator: ":").compactMap {Float(String($0))}
|
|
1978
2039
|
guard components.count == 2 else {
|
|
1979
2040
|
print("[CameraPreview] cropImageToAspectRatio - Failed to parse aspect ratio: \(aspectRatio)")
|
|
@@ -1981,7 +2042,7 @@ extension CameraController {
|
|
|
1981
2042
|
}
|
|
1982
2043
|
|
|
1983
2044
|
// Use physical orientation for capture works with portrait lock
|
|
1984
|
-
let orientation = self.lastCaptureOrientation ?? self.getPhysicalOrientation()
|
|
2045
|
+
let orientation = captureOrientation ?? self.lastCaptureOrientation ?? self.getPhysicalOrientation()
|
|
1985
2046
|
let isPortrait = (orientation == .portrait || orientation == .portraitUpsideDown)
|
|
1986
2047
|
|
|
1987
2048
|
let ratioWidth: CGFloat
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import CoreLocation
|
|
2
|
+
import Foundation
|
|
3
|
+
|
|
4
|
+
/// GPS EXIF helpers for still capture (heading / image direction).
|
|
5
|
+
enum CaptureExifGps {
|
|
6
|
+
struct ImageDirection: Equatable {
|
|
7
|
+
let degrees: Double
|
|
8
|
+
/// `T` (true north) or `M` (magnetic north).
|
|
9
|
+
let reference: String
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
static func imageDirection(
|
|
13
|
+
trueHeading: Double,
|
|
14
|
+
magneticHeading: Double,
|
|
15
|
+
headingAccuracy: Double
|
|
16
|
+
) -> ImageDirection? {
|
|
17
|
+
guard headingAccuracy >= 0 else { return nil }
|
|
18
|
+
if trueHeading >= 0 {
|
|
19
|
+
return ImageDirection(degrees: trueHeading, reference: "T")
|
|
20
|
+
}
|
|
21
|
+
return ImageDirection(degrees: magneticHeading, reference: "M")
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static func imageDirection(from heading: CLHeading) -> ImageDirection? {
|
|
25
|
+
imageDirection(
|
|
26
|
+
trueHeading: heading.trueHeading,
|
|
27
|
+
magneticHeading: heading.magneticHeading,
|
|
28
|
+
headingAccuracy: heading.headingAccuracy
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/// Reads the latest compass heading from the main queue (where `CLLocationManager` delivers updates).
|
|
34
|
+
enum CaptureExifHeadingReader {
|
|
35
|
+
static func readCurrentHeading(_ provider: @escaping () -> CLHeading?) -> CLHeading? {
|
|
36
|
+
if Thread.isMainThread {
|
|
37
|
+
return provider()
|
|
38
|
+
}
|
|
39
|
+
return DispatchQueue.main.sync { provider() }
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/// Heading storage rules shared by `getCurrentLocation` and capture cleanup.
|
|
44
|
+
enum CaptureExifHeadingLifecycle {
|
|
45
|
+
enum Event {
|
|
46
|
+
case locationRefreshStarted
|
|
47
|
+
case captureExifFinished
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static func heading(after event: Event, current: CLHeading?) -> CLHeading? {
|
|
51
|
+
switch event {
|
|
52
|
+
case .locationRefreshStarted:
|
|
53
|
+
return current
|
|
54
|
+
case .captureExifFinished:
|
|
55
|
+
return nil
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import CoreLocation
|
|
2
|
+
import ImageIO
|
|
3
|
+
import MobileCoreServices
|
|
4
|
+
import UIKit
|
|
5
|
+
|
|
6
|
+
enum CaptureImageExifEncoder {
|
|
7
|
+
typealias JpegEncoder = (UIImage, Int) -> Data?
|
|
8
|
+
|
|
9
|
+
static func createImageData(
|
|
10
|
+
from image: UIImage,
|
|
11
|
+
quality: Int,
|
|
12
|
+
location: CLLocation?,
|
|
13
|
+
heading: CLHeading?,
|
|
14
|
+
originalPhotoData: Data?,
|
|
15
|
+
jpegEncoder: JpegEncoder = defaultJpegEncoder
|
|
16
|
+
) -> Data? {
|
|
17
|
+
guard let cgImage = image.cgImage else {
|
|
18
|
+
return jpegEncoder(image, quality)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let sourceDataForMetadata: CFData
|
|
22
|
+
if let originalPhotoData {
|
|
23
|
+
sourceDataForMetadata = originalPhotoData as CFData
|
|
24
|
+
} else if let encoded = jpegEncoder(image, quality) {
|
|
25
|
+
sourceDataForMetadata = encoded as CFData
|
|
26
|
+
} else {
|
|
27
|
+
return nil
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
guard let imageSource = CGImageSourceCreateWithData(sourceDataForMetadata, nil),
|
|
31
|
+
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [String: Any] else {
|
|
32
|
+
return jpegEncoder(image, quality)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
var finalProperties = imageProperties
|
|
36
|
+
|
|
37
|
+
// Ensure orientation reflects the pixel data (we pass an orientation-fixed UIImage)
|
|
38
|
+
finalProperties[kCGImagePropertyOrientation as String] = 1
|
|
39
|
+
|
|
40
|
+
if let location {
|
|
41
|
+
let formatter = DateFormatter()
|
|
42
|
+
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
|
|
43
|
+
formatter.timeZone = TimeZone(abbreviation: "UTC")
|
|
44
|
+
|
|
45
|
+
var gpsDict: [String: Any] = [
|
|
46
|
+
kCGImagePropertyGPSLatitude as String: abs(location.coordinate.latitude),
|
|
47
|
+
kCGImagePropertyGPSLatitudeRef as String: location.coordinate.latitude >= 0 ? "N" : "S",
|
|
48
|
+
kCGImagePropertyGPSLongitude as String: abs(location.coordinate.longitude),
|
|
49
|
+
kCGImagePropertyGPSLongitudeRef as String: location.coordinate.longitude >= 0 ? "E" : "W",
|
|
50
|
+
kCGImagePropertyGPSTimeStamp as String: formatter.string(from: location.timestamp),
|
|
51
|
+
kCGImagePropertyGPSAltitude as String: location.altitude,
|
|
52
|
+
kCGImagePropertyGPSAltitudeRef as String: location.altitude >= 0 ? 0 : 1
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
if let direction = heading.flatMap(CaptureExifGps.imageDirection) {
|
|
56
|
+
gpsDict[kCGImagePropertyGPSImgDirection as String] = direction.degrees
|
|
57
|
+
gpsDict[kCGImagePropertyGPSImgDirectionRef as String] = direction.reference
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
finalProperties[kCGImagePropertyGPSDictionary as String] = gpsDict
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
var tiffDict = finalProperties[kCGImagePropertyTIFFDictionary as String] as? [String: Any] ?? [:]
|
|
64
|
+
tiffDict[kCGImagePropertyTIFFMake as String] = "Apple"
|
|
65
|
+
tiffDict[kCGImagePropertyTIFFModel as String] = UIDevice.current.model
|
|
66
|
+
tiffDict[kCGImagePropertyTIFFOrientation as String] = 1
|
|
67
|
+
finalProperties[kCGImagePropertyTIFFDictionary as String] = tiffDict
|
|
68
|
+
|
|
69
|
+
if let encoded = ImageIOJpegEncoding.encode(cgImage: cgImage, properties: finalProperties, qualityPercent: quality) {
|
|
70
|
+
return encoded
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return jpegEncoder(image, quality)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private static func defaultJpegEncoder(image: UIImage, quality: Int) -> Data? {
|
|
77
|
+
image.jpegData(compressionQuality: CGFloat(Double(quality) / 100.0))
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
/// Flags coordinating `stop()` with an in-flight still capture. Mutate only on the main thread.
|
|
4
|
+
struct CaptureLifecycleState {
|
|
5
|
+
var isCapturingPhoto: Bool = false
|
|
6
|
+
var stopRequestedAfterCapture: Bool = false
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
enum CaptureLifecycleCoordination {
|
|
10
|
+
/// Handles `stop()` when a capture may be in flight. Call on the main thread only.
|
|
11
|
+
static func handleStop(
|
|
12
|
+
state: inout CaptureLifecycleState,
|
|
13
|
+
force: Bool,
|
|
14
|
+
cleanup: () -> Void
|
|
15
|
+
) {
|
|
16
|
+
if state.isCapturingPhoto && !force {
|
|
17
|
+
state.stopRequestedAfterCapture = true
|
|
18
|
+
return
|
|
19
|
+
}
|
|
20
|
+
if force {
|
|
21
|
+
state.stopRequestedAfterCapture = false
|
|
22
|
+
}
|
|
23
|
+
cleanup()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/// Ends the capture lifecycle after processing completes. Call on the main thread only.
|
|
27
|
+
static func finishCapture(
|
|
28
|
+
state: inout CaptureLifecycleState,
|
|
29
|
+
cleanup: () -> Void
|
|
30
|
+
) {
|
|
31
|
+
state.isCapturingPhoto = false
|
|
32
|
+
if state.stopRequestedAfterCapture {
|
|
33
|
+
cleanup()
|
|
34
|
+
state.stopRequestedAfterCapture = false
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import Foundation
|
|
3
|
+
|
|
4
|
+
/// Selects whether still capture follows on-screen preview orientation or physical device tilt.
|
|
5
|
+
enum CaptureOrientationResolver {
|
|
6
|
+
static let orientationPreview = "preview"
|
|
7
|
+
static let orientationDevice = "device"
|
|
8
|
+
|
|
9
|
+
static func usesDeviceOrientation(mode: String?) -> Bool {
|
|
10
|
+
mode != orientationPreview
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
static func resolveOrientation(
|
|
14
|
+
mode: String?,
|
|
15
|
+
previewOrientation: AVCaptureVideoOrientation,
|
|
16
|
+
deviceOrientation: AVCaptureVideoOrientation
|
|
17
|
+
) -> AVCaptureVideoOrientation {
|
|
18
|
+
if mode == orientationPreview {
|
|
19
|
+
return previewOrientation
|
|
20
|
+
}
|
|
21
|
+
return deviceOrientation
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -35,7 +35,7 @@ extension UIWindow {
|
|
|
35
35
|
*/
|
|
36
36
|
@objc(CameraPreview)
|
|
37
37
|
public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelegate {
|
|
38
|
-
private let pluginVersion: String = "8.
|
|
38
|
+
private let pluginVersion: String = "8.13.1"
|
|
39
39
|
public let identifier = "CameraPreviewPlugin"
|
|
40
40
|
public let jsName = "CameraPreview"
|
|
41
41
|
public let pluginMethods: [CAPPluginMethod] = [
|
|
@@ -676,7 +676,10 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
676
676
|
if let pendingStartCall = self.pendingStartCall, !self.hasResolvedStartCall {
|
|
677
677
|
self.failPendingStart(pendingStartCall, message: "Camera start cancelled")
|
|
678
678
|
}
|
|
679
|
-
|
|
679
|
+
let captureLifecycleBusy = DispatchQueue.main.sync {
|
|
680
|
+
self.cameraController.isCapturingPhoto || self.cameraController.stopRequestedAfterCapture
|
|
681
|
+
}
|
|
682
|
+
if self.isInitializing || self.isInitialized || captureLifecycleBusy {
|
|
680
683
|
self.startGeneration += 1
|
|
681
684
|
self.cancelFirstFrameTimeout()
|
|
682
685
|
// Force stop everything synchronously
|
|
@@ -717,7 +720,10 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
717
720
|
return
|
|
718
721
|
}
|
|
719
722
|
// Guard against starting while a deferred stop is pending due to in-flight capture
|
|
720
|
-
|
|
723
|
+
let captureLifecycleBusy = DispatchQueue.main.sync {
|
|
724
|
+
self.cameraController.isCapturingPhoto || self.cameraController.stopRequestedAfterCapture
|
|
725
|
+
}
|
|
726
|
+
if captureLifecycleBusy {
|
|
721
727
|
call.reject("camera is stopping or busy, please retry shortly")
|
|
722
728
|
return
|
|
723
729
|
}
|
|
@@ -1171,16 +1177,11 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
1171
1177
|
self.isGeneratingDeviceOrientationNotifications = false
|
|
1172
1178
|
}
|
|
1173
1179
|
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
if force {
|
|
1180
|
-
self.cameraController.stopRequestedAfterCapture = false
|
|
1181
|
-
}
|
|
1182
|
-
self.cameraController.cleanup()
|
|
1183
|
-
}
|
|
1180
|
+
CaptureLifecycleCoordination.handleStop(
|
|
1181
|
+
state: &self.cameraController.captureLifecycle,
|
|
1182
|
+
force: force,
|
|
1183
|
+
cleanup: { self.cameraController.cleanup() }
|
|
1184
|
+
)
|
|
1184
1185
|
|
|
1185
1186
|
call.resolve()
|
|
1186
1187
|
}
|
|
@@ -1367,10 +1368,11 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
1367
1368
|
let height = call.getInt("height")
|
|
1368
1369
|
let photoQualityPrioritization = call.getString("photoQualityPrioritization", "speed")
|
|
1369
1370
|
let mirrorFrontCamera = call.getBool("mirrorFrontCamera", false) ?? false
|
|
1371
|
+
let captureOrientation = call.getString("orientation", CaptureOrientationResolver.orientationDevice)
|
|
1370
1372
|
|
|
1371
1373
|
print("[CameraPreview] Raw parameter values - width: \(String(describing: width)), height: \(String(describing: height))")
|
|
1372
1374
|
|
|
1373
|
-
print("[CameraPreview] Capture params - quality: \(quality), saveToGallery: \(saveToGallery), withExifLocation: \(withExifLocation ?? false), embedTimestamp: \(embedTimestamp), embedLocation: \(effectiveEmbedLocation) (requested=\(embedLocationRequested)), width: \(width ?? -1), height: \(height ?? -1), mirrorFrontCamera: \(mirrorFrontCamera)")
|
|
1375
|
+
print("[CameraPreview] Capture params - quality: \(quality), saveToGallery: \(saveToGallery), withExifLocation: \(withExifLocation ?? false), embedTimestamp: \(embedTimestamp), embedLocation: \(effectiveEmbedLocation) (requested=\(embedLocationRequested)), width: \(width ?? -1), height: \(height ?? -1), mirrorFrontCamera: \(mirrorFrontCamera), orientation: \(captureOrientation ?? CaptureOrientationResolver.orientationDevice)")
|
|
1374
1376
|
print("[CameraPreview] Current location: \(self.currentLocation?.description ?? "nil")")
|
|
1375
1377
|
// Safely read frame from main thread for logging
|
|
1376
1378
|
let (previewWidth, previewHeight): (CGFloat, CGFloat) = {
|
|
@@ -1390,9 +1392,8 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
1390
1392
|
let capturedFromFrontCamera = self.cameraController.currentCameraPosition == .front
|
|
1391
1393
|
let gpsForThisCapture = (withExifLocation ?? false) ? self.currentLocation : nil
|
|
1392
1394
|
let locationForExif = (withExifLocation ?? false) ? self.currentLocation : nil
|
|
1393
|
-
let headingForExif = (withExifLocation ?? false) ? self.currentHeading : nil
|
|
1394
1395
|
let storeToFile = self.storeToFile
|
|
1395
|
-
self.cameraController.captureImage(width: width, height: height, quality: quality, gpsLocation: gpsForThisCapture, embedTimestamp: embedTimestamp, embedLocation: effectiveEmbedLocation, photoQualityPrioritization: photoQualityPrioritization) { (image, originalPhotoData, _, error) in
|
|
1396
|
+
self.cameraController.captureImage(width: width, height: height, quality: quality, gpsLocation: gpsForThisCapture, embedTimestamp: embedTimestamp, embedLocation: effectiveEmbedLocation, photoQualityPrioritization: photoQualityPrioritization, captureOrientationMode: captureOrientation) { (image, originalPhotoData, _, error) in
|
|
1396
1397
|
print("[CameraPreview] captureImage callback received")
|
|
1397
1398
|
CapturePhotoProcessingQueue.async {
|
|
1398
1399
|
print("[CameraPreview] Processing capture on background queue (isMain=\(Thread.isMainThread))")
|
|
@@ -1400,7 +1401,10 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
1400
1401
|
if withExifLocation ?? false {
|
|
1401
1402
|
DispatchQueue.main.async {
|
|
1402
1403
|
self.locationManager?.stopUpdatingHeading()
|
|
1403
|
-
self.currentHeading =
|
|
1404
|
+
self.currentHeading = CaptureExifHeadingLifecycle.heading(
|
|
1405
|
+
after: .captureExifFinished,
|
|
1406
|
+
current: self.currentHeading
|
|
1407
|
+
)
|
|
1404
1408
|
}
|
|
1405
1409
|
}
|
|
1406
1410
|
}
|
|
@@ -1423,7 +1427,14 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
1423
1427
|
imageToEncode = capturedImage
|
|
1424
1428
|
}
|
|
1425
1429
|
|
|
1426
|
-
|
|
1430
|
+
let headingForExif: CLHeading?
|
|
1431
|
+
if withExifLocation ?? false {
|
|
1432
|
+
headingForExif = CaptureExifHeadingReader.readCurrentHeading { self.currentHeading }
|
|
1433
|
+
} else {
|
|
1434
|
+
headingForExif = nil
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
guard let imageDataWithExif = CaptureImageExifEncoder.createImageData(
|
|
1427
1438
|
from: imageToEncode,
|
|
1428
1439
|
quality: Int(quality),
|
|
1429
1440
|
location: locationForExif,
|
|
@@ -1562,72 +1573,6 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
1562
1573
|
}
|
|
1563
1574
|
}
|
|
1564
1575
|
|
|
1565
|
-
private func createImageDataWithExif(from image: UIImage, quality: Int, location: CLLocation?, heading: CLHeading?, originalPhotoData: Data?) -> Data? {
|
|
1566
|
-
guard let jpegDataAtQuality = image.jpegData(compressionQuality: CGFloat(Double(quality) / 100.0)) else {
|
|
1567
|
-
return nil
|
|
1568
|
-
}
|
|
1569
|
-
|
|
1570
|
-
// Prefer metadata from the original AVCapturePhoto file data to preserve lens/EXIF
|
|
1571
|
-
let sourceDataForMetadata = (originalPhotoData ?? jpegDataAtQuality) as CFData
|
|
1572
|
-
guard let imageSource = CGImageSourceCreateWithData(sourceDataForMetadata, nil),
|
|
1573
|
-
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [String: Any],
|
|
1574
|
-
let cgImage = image.cgImage else {
|
|
1575
|
-
return jpegDataAtQuality
|
|
1576
|
-
}
|
|
1577
|
-
|
|
1578
|
-
var finalProperties = imageProperties
|
|
1579
|
-
|
|
1580
|
-
// Ensure orientation reflects the pixel data (we pass an orientation-fixed UIImage)
|
|
1581
|
-
finalProperties[kCGImagePropertyOrientation as String] = 1
|
|
1582
|
-
|
|
1583
|
-
// Add GPS location if available
|
|
1584
|
-
if let location = location {
|
|
1585
|
-
let formatter = DateFormatter()
|
|
1586
|
-
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
|
|
1587
|
-
formatter.timeZone = TimeZone(abbreviation: "UTC")
|
|
1588
|
-
|
|
1589
|
-
var gpsDict: [String: Any] = [
|
|
1590
|
-
kCGImagePropertyGPSLatitude as String: abs(location.coordinate.latitude),
|
|
1591
|
-
kCGImagePropertyGPSLatitudeRef as String: location.coordinate.latitude >= 0 ? "N" : "S",
|
|
1592
|
-
kCGImagePropertyGPSLongitude as String: abs(location.coordinate.longitude),
|
|
1593
|
-
kCGImagePropertyGPSLongitudeRef as String: location.coordinate.longitude >= 0 ? "E" : "W",
|
|
1594
|
-
kCGImagePropertyGPSTimeStamp as String: formatter.string(from: location.timestamp),
|
|
1595
|
-
kCGImagePropertyGPSAltitude as String: location.altitude,
|
|
1596
|
-
kCGImagePropertyGPSAltitudeRef as String: location.altitude >= 0 ? 0 : 1
|
|
1597
|
-
]
|
|
1598
|
-
|
|
1599
|
-
// Add image direction (compass heading) when available
|
|
1600
|
-
if let heading = heading {
|
|
1601
|
-
let directionDegrees: Double
|
|
1602
|
-
let directionRef: String
|
|
1603
|
-
if heading.trueHeading >= 0 {
|
|
1604
|
-
directionDegrees = heading.trueHeading
|
|
1605
|
-
directionRef = "T"
|
|
1606
|
-
} else {
|
|
1607
|
-
directionDegrees = heading.magneticHeading
|
|
1608
|
-
directionRef = "M"
|
|
1609
|
-
}
|
|
1610
|
-
gpsDict[kCGImagePropertyGPSImgDirection as String] = directionDegrees
|
|
1611
|
-
gpsDict[kCGImagePropertyGPSImgDirectionRef as String] = directionRef
|
|
1612
|
-
}
|
|
1613
|
-
|
|
1614
|
-
finalProperties[kCGImagePropertyGPSDictionary as String] = gpsDict
|
|
1615
|
-
}
|
|
1616
|
-
|
|
1617
|
-
// Create or update TIFF dictionary for device info and set orientation to Up
|
|
1618
|
-
var tiffDict = finalProperties[kCGImagePropertyTIFFDictionary as String] as? [String: Any] ?? [:]
|
|
1619
|
-
tiffDict[kCGImagePropertyTIFFMake as String] = "Apple"
|
|
1620
|
-
tiffDict[kCGImagePropertyTIFFModel as String] = UIDevice.current.model
|
|
1621
|
-
tiffDict[kCGImagePropertyTIFFOrientation as String] = 1
|
|
1622
|
-
finalProperties[kCGImagePropertyTIFFDictionary as String] = tiffDict
|
|
1623
|
-
|
|
1624
|
-
if let encoded = ImageIOJpegEncoding.encode(cgImage: cgImage, properties: finalProperties, qualityPercent: quality) {
|
|
1625
|
-
return encoded
|
|
1626
|
-
}
|
|
1627
|
-
|
|
1628
|
-
return jpegDataAtQuality
|
|
1629
|
-
}
|
|
1630
|
-
|
|
1631
1576
|
@objc func captureSample(_ call: CAPPluginCall) {
|
|
1632
1577
|
let quality: Int = call.getInt("quality") ?? 85
|
|
1633
1578
|
let mirrorFrontCamera = call.getBool("mirrorFrontCamera", false) ?? false
|
|
@@ -2169,7 +2114,10 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
2169
2114
|
|
|
2170
2115
|
private func getCurrentLocation(completion: @escaping (CLLocation?) -> Void) {
|
|
2171
2116
|
print("[CameraPreview] getCurrentLocation called")
|
|
2172
|
-
self.currentHeading =
|
|
2117
|
+
self.currentHeading = CaptureExifHeadingLifecycle.heading(
|
|
2118
|
+
after: .locationRefreshStarted,
|
|
2119
|
+
current: self.currentHeading
|
|
2120
|
+
)
|
|
2173
2121
|
self.locationCompletion = completion
|
|
2174
2122
|
self.locationManager?.startUpdatingLocation()
|
|
2175
2123
|
print("[CameraPreview] Started updating location")
|