@capgo/camera-preview 8.13.0 → 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.
@@ -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
+ }
@@ -52,18 +52,9 @@ enum CaptureImageExifEncoder {
52
52
  kCGImagePropertyGPSAltitudeRef as String: location.altitude >= 0 ? 0 : 1
53
53
  ]
54
54
 
55
- if let heading {
56
- let directionDegrees: Double
57
- let directionRef: String
58
- if heading.trueHeading >= 0 {
59
- directionDegrees = heading.trueHeading
60
- directionRef = "T"
61
- } else {
62
- directionDegrees = heading.magneticHeading
63
- directionRef = "M"
64
- }
65
- gpsDict[kCGImagePropertyGPSImgDirection as String] = directionDegrees
66
- gpsDict[kCGImagePropertyGPSImgDirectionRef as String] = directionRef
55
+ if let direction = heading.flatMap(CaptureExifGps.imageDirection) {
56
+ gpsDict[kCGImagePropertyGPSImgDirection as String] = direction.degrees
57
+ gpsDict[kCGImagePropertyGPSImgDirectionRef as String] = direction.reference
67
58
  }
68
59
 
69
60
  finalProperties[kCGImagePropertyGPSDictionary as String] = gpsDict
@@ -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.13.0"
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] = [
@@ -1392,7 +1392,6 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
1392
1392
  let capturedFromFrontCamera = self.cameraController.currentCameraPosition == .front
1393
1393
  let gpsForThisCapture = (withExifLocation ?? false) ? self.currentLocation : nil
1394
1394
  let locationForExif = (withExifLocation ?? false) ? self.currentLocation : nil
1395
- let headingForExif = (withExifLocation ?? false) ? self.currentHeading : nil
1396
1395
  let storeToFile = self.storeToFile
1397
1396
  self.cameraController.captureImage(width: width, height: height, quality: quality, gpsLocation: gpsForThisCapture, embedTimestamp: embedTimestamp, embedLocation: effectiveEmbedLocation, photoQualityPrioritization: photoQualityPrioritization, captureOrientationMode: captureOrientation) { (image, originalPhotoData, _, error) in
1398
1397
  print("[CameraPreview] captureImage callback received")
@@ -1402,7 +1401,10 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
1402
1401
  if withExifLocation ?? false {
1403
1402
  DispatchQueue.main.async {
1404
1403
  self.locationManager?.stopUpdatingHeading()
1405
- self.currentHeading = nil
1404
+ self.currentHeading = CaptureExifHeadingLifecycle.heading(
1405
+ after: .captureExifFinished,
1406
+ current: self.currentHeading
1407
+ )
1406
1408
  }
1407
1409
  }
1408
1410
  }
@@ -1425,6 +1427,13 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
1425
1427
  imageToEncode = capturedImage
1426
1428
  }
1427
1429
 
1430
+ let headingForExif: CLHeading?
1431
+ if withExifLocation ?? false {
1432
+ headingForExif = CaptureExifHeadingReader.readCurrentHeading { self.currentHeading }
1433
+ } else {
1434
+ headingForExif = nil
1435
+ }
1436
+
1428
1437
  guard let imageDataWithExif = CaptureImageExifEncoder.createImageData(
1429
1438
  from: imageToEncode,
1430
1439
  quality: Int(quality),
@@ -2105,7 +2114,10 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
2105
2114
 
2106
2115
  private func getCurrentLocation(completion: @escaping (CLLocation?) -> Void) {
2107
2116
  print("[CameraPreview] getCurrentLocation called")
2108
- self.currentHeading = nil
2117
+ self.currentHeading = CaptureExifHeadingLifecycle.heading(
2118
+ after: .locationRefreshStarted,
2119
+ current: self.currentHeading
2120
+ )
2109
2121
  self.locationCompletion = completion
2110
2122
  self.locationManager?.startUpdatingLocation()
2111
2123
  print("[CameraPreview] Started updating location")
@@ -0,0 +1,95 @@
1
+ import CoreLocation
2
+ import XCTest
3
+ @testable import CapgoCameraPreview
4
+
5
+ private final class TestCLHeading: CLHeading {
6
+ private let storedTrue: CLLocationDirection
7
+ private let storedMagnetic: CLLocationDirection
8
+ private let storedAccuracy: CLLocationAccuracy
9
+
10
+ init(
11
+ trueHeading: CLLocationDirection,
12
+ magneticHeading: CLLocationDirection,
13
+ headingAccuracy: CLLocationAccuracy
14
+ ) {
15
+ self.storedTrue = trueHeading
16
+ self.storedMagnetic = magneticHeading
17
+ self.storedAccuracy = headingAccuracy
18
+ super.init()
19
+ }
20
+
21
+ @available(*, unavailable)
22
+ required init?(coder: NSCoder) {
23
+ fatalError("init(coder:) has not been implemented")
24
+ }
25
+
26
+ override var trueHeading: CLLocationDirection { storedTrue }
27
+ override var magneticHeading: CLLocationDirection { storedMagnetic }
28
+ override var headingAccuracy: CLLocationAccuracy { storedAccuracy }
29
+ }
30
+
31
+ final class CaptureExifGpsTests: XCTestCase {
32
+ func testImageDirectionUsesTrueHeadingWhenAvailable() {
33
+ let direction = CaptureExifGps.imageDirection(
34
+ trueHeading: 175.4,
35
+ magneticHeading: 180.0,
36
+ headingAccuracy: 5
37
+ )
38
+ XCTAssertEqual(direction?.degrees, 175.4)
39
+ XCTAssertEqual(direction?.reference, "T")
40
+ }
41
+
42
+ func testImageDirectionFallsBackToMagneticWhenTrueHeadingInvalid() {
43
+ let direction = CaptureExifGps.imageDirection(
44
+ trueHeading: -1,
45
+ magneticHeading: 182.5,
46
+ headingAccuracy: 5
47
+ )
48
+ XCTAssertEqual(direction?.degrees, 182.5)
49
+ XCTAssertEqual(direction?.reference, "M")
50
+ }
51
+
52
+ func testImageDirectionNilWhenAccuracyInvalid() {
53
+ XCTAssertNil(
54
+ CaptureExifGps.imageDirection(
55
+ trueHeading: 90,
56
+ magneticHeading: 90,
57
+ headingAccuracy: -1
58
+ )
59
+ )
60
+ }
61
+
62
+ func testHeadingReaderReturnsProviderValueOnMainThread() {
63
+ let expected = TestCLHeading(trueHeading: 10, magneticHeading: 11, headingAccuracy: 3)
64
+ let read = CaptureExifHeadingReader.readCurrentHeading { expected }
65
+ XCTAssertTrue(read === expected)
66
+ }
67
+
68
+ func testHeadingReaderFetchesFromMainWhenOffMainThread() {
69
+ let expected = TestCLHeading(trueHeading: 42, magneticHeading: 44, headingAccuracy: 2)
70
+ let expectation = expectation(description: "read on main")
71
+ DispatchQueue.global(qos: .userInitiated).async {
72
+ let read = CaptureExifHeadingReader.readCurrentHeading {
73
+ XCTAssertTrue(Thread.isMainThread)
74
+ return expected
75
+ }
76
+ XCTAssertTrue(read === expected)
77
+ expectation.fulfill()
78
+ }
79
+ wait(for: [expectation], timeout: 5)
80
+ }
81
+
82
+ func testHeadingFallbackAfterLocationRefresh() {
83
+ let prior = TestCLHeading(trueHeading: 88.0, magneticHeading: 90.0, headingAccuracy: 4)
84
+ let afterRefresh = CaptureExifHeadingLifecycle.heading(after: .locationRefreshStarted, current: prior)
85
+ XCTAssertTrue(afterRefresh === prior)
86
+ let direction = afterRefresh.flatMap(CaptureExifGps.imageDirection)
87
+ XCTAssertEqual(direction?.degrees, 88.0)
88
+ XCTAssertEqual(direction?.reference, "T")
89
+ }
90
+
91
+ func testCaptureExifFinishedClearsHeading() {
92
+ let prior = TestCLHeading(trueHeading: 88.0, magneticHeading: 90.0, headingAccuracy: 4)
93
+ XCTAssertNil(CaptureExifHeadingLifecycle.heading(after: .captureExifFinished, current: prior))
94
+ }
95
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/camera-preview",
3
- "version": "8.13.0",
3
+ "version": "8.13.1",
4
4
  "description": "Camera preview",
5
5
  "license": "MPL-2.0",
6
6
  "repository": {