@capgo/camera-preview 8.12.3 → 8.12.5
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 +43 -39
- package/android/src/main/java/app/capgo/capacitor/camera/preview/CameraXView.java +26 -28
- package/android/src/main/java/app/capgo/capacitor/camera/preview/PhysicalOrientationResolver.java +72 -0
- package/android/src/test/java/app/capgo/capacitor/camera/preview/PhysicalOrientationResolverTest.java +90 -0
- package/dist/docs.json +8 -4
- package/dist/esm/definitions.d.ts +20 -8
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/web.js +6 -3
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +6 -3
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +6 -3
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/CapgoCameraPreviewPlugin/ApplicationAudioSessionRestoreCoordinator.swift +136 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/ApplicationAudioSessionSnapshot.swift +28 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/AspectRatioLayout.swift +40 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/CameraController.swift +204 -34
- package/ios/Sources/CapgoCameraPreviewPlugin/CapturePhotoProcessingQueue.swift +20 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/CaptureResolutionSupport.swift +133 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/ImageIOJpegEncoding.swift +28 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/PhysicalOrientationResolver.swift +59 -0
- package/ios/Sources/CapgoCameraPreviewPlugin/Plugin.swift +59 -63
- package/ios/Tests/CameraPreviewPluginTests/ApplicationAudioSessionRestoreCoordinatorTests.swift +279 -0
- package/ios/Tests/CameraPreviewPluginTests/ApplicationAudioSessionSnapshotTests.swift +25 -0
- package/ios/Tests/CameraPreviewPluginTests/CameraPreviewPluginTests.swift +26 -0
- package/ios/Tests/CameraPreviewPluginTests/CaptureResolutionSupportTests.swift +133 -0
- package/ios/Tests/CameraPreviewPluginTests/ImageIOJpegEncodingTests.swift +42 -0
- package/ios/Tests/CameraPreviewPluginTests/PhysicalOrientationResolverTests.swift +89 -0
- package/package.json +5 -3
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import Foundation
|
|
3
|
+
|
|
4
|
+
struct CaptureResolutionParseResult {
|
|
5
|
+
let width: Int?
|
|
6
|
+
let height: Int?
|
|
7
|
+
let errorMessage: String?
|
|
8
|
+
|
|
9
|
+
var isError: Bool { errorMessage != nil }
|
|
10
|
+
|
|
11
|
+
static func success(width: Int?, height: Int?) -> CaptureResolutionParseResult {
|
|
12
|
+
CaptureResolutionParseResult(width: width, height: height, errorMessage: nil)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static func error(_ message: String) -> CaptureResolutionParseResult {
|
|
16
|
+
CaptureResolutionParseResult(width: nil, height: nil, errorMessage: message)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
enum CaptureResolutionSupport {
|
|
21
|
+
static func parseStartCaptureResolution(_ captureResolutionObject: Any?) -> CaptureResolutionParseResult {
|
|
22
|
+
guard let captureResolutionObject else {
|
|
23
|
+
return .success(width: nil, height: nil)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
guard let captureResolution = captureResolutionObject as? [String: Any] else {
|
|
27
|
+
return .error("captureResolution must be an object with width and height")
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
guard captureResolution["width"] != nil, captureResolution["height"] != nil else {
|
|
31
|
+
return .error("captureResolution requires both width and height")
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
guard let captureWidth = parsePositivePixelDimension(captureResolution["width"]),
|
|
35
|
+
let captureHeight = parsePositivePixelDimension(captureResolution["height"]) else {
|
|
36
|
+
return .error("captureResolution width and height must be positive")
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return .success(width: captureWidth, height: captureHeight)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static func parsePositivePixelDimension(_ value: Any?) -> Int? {
|
|
43
|
+
guard let value else { return nil }
|
|
44
|
+
|
|
45
|
+
if let number = value as? NSNumber {
|
|
46
|
+
return parsePositivePixelDimensionFromDouble(number.doubleValue)
|
|
47
|
+
}
|
|
48
|
+
if let string = value as? String {
|
|
49
|
+
let trimmed = string.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
50
|
+
if trimmed.isEmpty { return nil }
|
|
51
|
+
guard let numeric = Double(trimmed) else { return nil }
|
|
52
|
+
return parsePositivePixelDimensionFromDouble(numeric)
|
|
53
|
+
}
|
|
54
|
+
return nil
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private static func parsePositivePixelDimensionFromDouble(_ numeric: Double) -> Int? {
|
|
58
|
+
guard !numeric.isNaN, !numeric.isInfinite, numeric > 0 else { return nil }
|
|
59
|
+
guard numeric == floor(numeric), numeric <= Double(Int.max) else { return nil }
|
|
60
|
+
guard numeric <= Double(Int32.max) else { return nil }
|
|
61
|
+
return Int(numeric)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/// Matches Android `normalizedCaptureTargetResolution`: landscape-major width and height.
|
|
65
|
+
static func normalizedCaptureTargetResolution(configuredWidth: Int, configuredHeight: Int) -> (width: Int, height: Int) {
|
|
66
|
+
(max(configuredWidth, configuredHeight), min(configuredWidth, configuredHeight))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static func longEdge(_ dimensions: CMVideoDimensions) -> Int32 {
|
|
70
|
+
max(dimensions.width, dimensions.height)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static func shortEdge(_ dimensions: CMVideoDimensions) -> Int32 {
|
|
74
|
+
min(dimensions.width, dimensions.height)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static func pixelArea(_ dimensions: CMVideoDimensions) -> Int64 {
|
|
78
|
+
Int64(dimensions.width) * Int64(dimensions.height)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/// Picks a supported `maxPhotoDimensions` value at or below the configured cap.
|
|
82
|
+
static func photoMaxDimensions(
|
|
83
|
+
configuredWidth: Int,
|
|
84
|
+
configuredHeight: Int,
|
|
85
|
+
supportedDimensions: [CMVideoDimensions],
|
|
86
|
+
deviceMax: CMVideoDimensions
|
|
87
|
+
) -> CMVideoDimensions {
|
|
88
|
+
let normalized = normalizedCaptureTargetResolution(configuredWidth: configuredWidth, configuredHeight: configuredHeight)
|
|
89
|
+
let targetLong = Int32(normalized.width)
|
|
90
|
+
let targetShort = Int32(normalized.height)
|
|
91
|
+
|
|
92
|
+
let fittingSupported = supportedDimensions.filter { dimension in
|
|
93
|
+
longEdge(dimension) <= targetLong && shortEdge(dimension) <= targetShort
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if let bestFit = fittingSupported.max(by: { pixelArea($0) < pixelArea($1) }) {
|
|
97
|
+
return bestFit
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return clampPhotoDimensions(
|
|
101
|
+
width: targetLong,
|
|
102
|
+
height: targetShort,
|
|
103
|
+
deviceMax: deviceMax
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
static func clampPhotoDimensions(width: Int32, height: Int32, deviceMax: CMVideoDimensions) -> CMVideoDimensions {
|
|
108
|
+
var clampedWidth = width
|
|
109
|
+
var clampedHeight = height
|
|
110
|
+
let deviceWidth = deviceMax.width
|
|
111
|
+
let deviceHeight = deviceMax.height
|
|
112
|
+
if clampedWidth > deviceWidth || clampedHeight > deviceHeight {
|
|
113
|
+
let scale = min(Double(deviceWidth) / Double(clampedWidth), Double(deviceHeight) / Double(clampedHeight))
|
|
114
|
+
clampedWidth = max(1, Int32((Double(clampedWidth) * scale).rounded(.down)))
|
|
115
|
+
clampedHeight = max(1, Int32((Double(clampedHeight) * scale).rounded(.down)))
|
|
116
|
+
}
|
|
117
|
+
return CMVideoDimensions(width: clampedWidth, height: clampedHeight)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
static func shouldEnableHighResolutionPhoto(
|
|
121
|
+
captureResolutionWidth: Int?,
|
|
122
|
+
captureResolutionHeight: Int?,
|
|
123
|
+
captureMaxWidth: Int?,
|
|
124
|
+
captureMaxHeight: Int?
|
|
125
|
+
) -> Bool {
|
|
126
|
+
if captureResolutionWidth != nil, captureResolutionHeight != nil {
|
|
127
|
+
return false
|
|
128
|
+
}
|
|
129
|
+
let widthExceedsDefault = captureMaxWidth.map { $0 > 1920 } ?? false
|
|
130
|
+
let heightExceedsDefault = captureMaxHeight.map { $0 > 1920 } ?? false
|
|
131
|
+
return widthExceedsDefault || heightExceedsDefault
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import ImageIO
|
|
3
|
+
import MobileCoreServices
|
|
4
|
+
|
|
5
|
+
enum ImageIOJpegEncoding {
|
|
6
|
+
static func compressionQuality(forPercent qualityPercent: Int) -> CGFloat {
|
|
7
|
+
CGFloat(Double(qualityPercent) / 100.0)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static func applyingCompressionQuality(_ qualityPercent: Int, to properties: [String: Any]) -> [String: Any] {
|
|
11
|
+
var result = properties
|
|
12
|
+
result[kCGImageDestinationLossyCompressionQuality as String] = compressionQuality(forPercent: qualityPercent)
|
|
13
|
+
return result
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
static func encode(cgImage: CGImage, properties: [String: Any], qualityPercent: Int) -> Data? {
|
|
17
|
+
let finalProperties = applyingCompressionQuality(qualityPercent, to: properties)
|
|
18
|
+
let mutableData = NSMutableData()
|
|
19
|
+
guard let destination = CGImageDestinationCreateWithData(mutableData, kUTTypeJPEG, 1, nil) else {
|
|
20
|
+
return nil
|
|
21
|
+
}
|
|
22
|
+
CGImageDestinationAddImage(destination, cgImage, finalProperties as CFDictionary)
|
|
23
|
+
guard CGImageDestinationFinalize(destination) else {
|
|
24
|
+
return nil
|
|
25
|
+
}
|
|
26
|
+
return mutableData as Data
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import Foundation
|
|
3
|
+
|
|
4
|
+
/// Resolves device physical orientation from gravity (accelerometer) for photo and video capture.
|
|
5
|
+
/// When the device is near flat, x/y gravity is noisy; keep the last reliable orientation or UI fallback.
|
|
6
|
+
enum PhysicalOrientationResolver {
|
|
7
|
+
/// |z| above this means gravity mostly points through the screen (camera toward table or ceiling).
|
|
8
|
+
static let flatGravityZThreshold: Double = 0.75
|
|
9
|
+
/// Below this, horizontal gravity is too weak to infer portrait vs landscape reliably.
|
|
10
|
+
static let minHorizontalGravity: Double = 0.35
|
|
11
|
+
/// When |x| and |y| are within this margin, do not flip between landscape and portrait.
|
|
12
|
+
static let axisDecisionMargin: Double = 0.12
|
|
13
|
+
|
|
14
|
+
struct Acceleration {
|
|
15
|
+
let x: Double
|
|
16
|
+
let y: Double
|
|
17
|
+
let z: Double
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
struct Result {
|
|
21
|
+
let orientation: AVCaptureVideoOrientation
|
|
22
|
+
/// True when orientation came from a strong gravity signal (safe to remember for hysteresis).
|
|
23
|
+
let isReliable: Bool
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static func resolve(
|
|
27
|
+
acceleration: Acceleration,
|
|
28
|
+
lastReliableOrientation: AVCaptureVideoOrientation?,
|
|
29
|
+
fallbackOrientation: AVCaptureVideoOrientation
|
|
30
|
+
) -> Result {
|
|
31
|
+
let x = acceleration.x
|
|
32
|
+
let y = acceleration.y
|
|
33
|
+
let z = acceleration.z
|
|
34
|
+
let absX = abs(x)
|
|
35
|
+
let absY = abs(y)
|
|
36
|
+
let absZ = abs(z)
|
|
37
|
+
let horizontalMax = max(absX, absY)
|
|
38
|
+
|
|
39
|
+
let isNearFlat = absZ >= flatGravityZThreshold || horizontalMax < minHorizontalGravity
|
|
40
|
+
if isNearFlat {
|
|
41
|
+
let orientation = lastReliableOrientation ?? fallbackOrientation
|
|
42
|
+
return Result(orientation: orientation, isReliable: false)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let axisMargin = axisDecisionMargin
|
|
46
|
+
if abs(absX - absY) < axisMargin {
|
|
47
|
+
let orientation = lastReliableOrientation ?? fallbackOrientation
|
|
48
|
+
return Result(orientation: orientation, isReliable: false)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let orientation: AVCaptureVideoOrientation
|
|
52
|
+
if absX > absY {
|
|
53
|
+
orientation = x > 0 ? .landscapeLeft : .landscapeRight
|
|
54
|
+
} else {
|
|
55
|
+
orientation = y > 0 ? .portraitUpsideDown : .portrait
|
|
56
|
+
}
|
|
57
|
+
return Result(orientation: orientation, isReliable: true)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -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.12.
|
|
38
|
+
private let pluginVersion: String = "8.12.5"
|
|
39
39
|
public let identifier = "CameraPreviewPlugin"
|
|
40
40
|
public let jsName = "CameraPreview"
|
|
41
41
|
public let pluginMethods: [CAPPluginMethod] = [
|
|
@@ -491,12 +491,16 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
491
491
|
|
|
492
492
|
self.updateCameraFrame()
|
|
493
493
|
|
|
494
|
-
|
|
494
|
+
return self.visiblePreviewSizeJSObject()
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
private func visiblePreviewSizeJSObject() -> JSObject {
|
|
498
|
+
let rect = self.cameraController.visiblePreviewRect(in: self.previewView)
|
|
495
499
|
var result = JSObject()
|
|
496
|
-
result["x"] = Double(
|
|
497
|
-
result["y"] = Double(
|
|
498
|
-
result["width"] = Double(
|
|
499
|
-
result["height"] = Double(
|
|
500
|
+
result["x"] = Double(rect.origin.x)
|
|
501
|
+
result["y"] = Double(rect.origin.y)
|
|
502
|
+
result["width"] = Double(rect.width)
|
|
503
|
+
result["height"] = Double(rect.height)
|
|
500
504
|
return result
|
|
501
505
|
}
|
|
502
506
|
|
|
@@ -663,6 +667,7 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
663
667
|
print(" - force: \(call.getBool("force") ?? false)")
|
|
664
668
|
print(" - videoQuality: \(call.getString("videoQuality") ?? "high")")
|
|
665
669
|
print(" - videoStabilizationMode: \(call.getString("videoStabilizationMode") ?? "off")")
|
|
670
|
+
print(" - allowHapticsAndSystemSoundsDuringRecording: \(call.getBool("allowHapticsAndSystemSoundsDuringRecording") ?? false)")
|
|
666
671
|
|
|
667
672
|
let force = call.getBool("force") ?? false
|
|
668
673
|
|
|
@@ -718,12 +723,37 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
718
723
|
}
|
|
719
724
|
}
|
|
720
725
|
|
|
726
|
+
let hasAspectRatio = call.getString("aspectRatio") != nil
|
|
727
|
+
let hasWidth = call.getInt("width") != nil
|
|
728
|
+
let hasHeight = call.getInt("height") != nil
|
|
729
|
+
if hasAspectRatio && (hasWidth || hasHeight) {
|
|
730
|
+
call.reject("Cannot set both aspectRatio and size (width/height). Use setPreviewSize after start.")
|
|
731
|
+
return
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
let captureResolutionParseResult = CaptureResolutionSupport.parseStartCaptureResolution(call.getObject("captureResolution"))
|
|
735
|
+
if captureResolutionParseResult.isError {
|
|
736
|
+
call.reject(captureResolutionParseResult.errorMessage ?? "Invalid captureResolution")
|
|
737
|
+
return
|
|
738
|
+
}
|
|
739
|
+
if captureResolutionParseResult.width != nil, captureResolutionParseResult.height != nil {
|
|
740
|
+
if #unavailable(iOS 16.0) {
|
|
741
|
+
call.reject("captureResolution requires iOS 16 or later")
|
|
742
|
+
return
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
|
|
721
746
|
self.isInitializing = true
|
|
722
747
|
self.hasResolvedStartCall = false
|
|
723
748
|
self.pendingStartCall = call
|
|
724
749
|
self.startGeneration += 1
|
|
725
750
|
let startToken = self.startGeneration
|
|
726
751
|
|
|
752
|
+
self.cameraController.setConfiguredCaptureResolution(
|
|
753
|
+
width: captureResolutionParseResult.width,
|
|
754
|
+
height: captureResolutionParseResult.height
|
|
755
|
+
)
|
|
756
|
+
|
|
727
757
|
self.cameraPosition = call.getString("position") ?? "rear"
|
|
728
758
|
let deviceId = call.getString("deviceId")
|
|
729
759
|
let cameraMode = call.getBool("enableVideoMode") ?? call.getBool("cameraMode") ?? false
|
|
@@ -781,17 +811,8 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
781
811
|
self.pendingStartBarcodeScannerOptions = self.barcodeScannerStartOptions(from: call)
|
|
782
812
|
|
|
783
813
|
let initialZoomLevel = call.getFloat("initialZoomLevel")
|
|
814
|
+
let allowHapticsAndSystemSoundsDuringRecording = call.getBool("allowHapticsAndSystemSoundsDuringRecording") ?? false
|
|
784
815
|
|
|
785
|
-
// Check for conflict between aspectRatio and size (width/height)
|
|
786
|
-
let hasAspectRatio = call.getString("aspectRatio") != nil
|
|
787
|
-
let hasWidth = call.getInt("width") != nil
|
|
788
|
-
let hasHeight = call.getInt("height") != nil
|
|
789
|
-
|
|
790
|
-
if hasAspectRatio && (hasWidth || hasHeight) {
|
|
791
|
-
self.pendingStartBarcodeScannerOptions = nil
|
|
792
|
-
call.reject("Cannot set both aspectRatio and size (width/height). Use setPreviewSize after start.")
|
|
793
|
-
return
|
|
794
|
-
}
|
|
795
816
|
let beginStart: () -> Void = {
|
|
796
817
|
if (self.cameraController.captureSession?.isRunning ?? false) && !force {
|
|
797
818
|
DispatchQueue.main.async {
|
|
@@ -830,7 +851,7 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
830
851
|
}
|
|
831
852
|
}
|
|
832
853
|
|
|
833
|
-
self.cameraController.prepare(cameraPosition: self.cameraPosition, deviceId: deviceId, disableAudio: self.disableAudio, cameraMode: cameraMode, aspectRatio: self.aspectRatio, aspectMode: self.aspectMode, initialZoomLevel: initialZoomLevel, disableFocusIndicator: self.disableFocusIndicator, videoQuality: videoQuality, startToken: startToken) { error in
|
|
854
|
+
self.cameraController.prepare(cameraPosition: self.cameraPosition, deviceId: deviceId, disableAudio: self.disableAudio, cameraMode: cameraMode, aspectRatio: self.aspectRatio, aspectMode: self.aspectMode, initialZoomLevel: initialZoomLevel, disableFocusIndicator: self.disableFocusIndicator, videoQuality: videoQuality, allowHapticsAndSystemSoundsDuringRecording: allowHapticsAndSystemSoundsDuringRecording, startToken: startToken) { error in
|
|
834
855
|
if let error = error {
|
|
835
856
|
print(error)
|
|
836
857
|
DispatchQueue.main.async {
|
|
@@ -994,12 +1015,7 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
994
1015
|
|
|
995
1016
|
DispatchQueue.main.async {
|
|
996
1017
|
guard startToken == self.startGeneration else { return }
|
|
997
|
-
|
|
998
|
-
returnedObject["width"] = self.previewView.frame.width as any JSValue
|
|
999
|
-
returnedObject["height"] = self.previewView.frame.height as any JSValue
|
|
1000
|
-
returnedObject["x"] = self.previewView.frame.origin.x as any JSValue
|
|
1001
|
-
returnedObject["y"] = self.previewView.frame.origin.y as any JSValue
|
|
1002
|
-
self.resolveStartCall(call, returnedObject: returnedObject)
|
|
1018
|
+
self.resolveStartCall(call, returnedObject: self.visiblePreviewSizeJSObject())
|
|
1003
1019
|
}
|
|
1004
1020
|
}
|
|
1005
1021
|
|
|
@@ -1009,12 +1025,7 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
1009
1025
|
if self.cameraController.hasReceivedFirstFrame {
|
|
1010
1026
|
DispatchQueue.main.async {
|
|
1011
1027
|
guard startToken == self.startGeneration else { return }
|
|
1012
|
-
|
|
1013
|
-
returnedObject["width"] = self.previewView.frame.width as any JSValue
|
|
1014
|
-
returnedObject["height"] = self.previewView.frame.height as any JSValue
|
|
1015
|
-
returnedObject["x"] = self.previewView.frame.origin.x as any JSValue
|
|
1016
|
-
returnedObject["y"] = self.previewView.frame.origin.y as any JSValue
|
|
1017
|
-
self.resolveStartCall(call, returnedObject: returnedObject)
|
|
1028
|
+
self.resolveStartCall(call, returnedObject: self.visiblePreviewSizeJSObject())
|
|
1018
1029
|
}
|
|
1019
1030
|
}
|
|
1020
1031
|
}
|
|
@@ -1378,15 +1389,19 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
1378
1389
|
|
|
1379
1390
|
let capturedFromFrontCamera = self.cameraController.currentCameraPosition == .front
|
|
1380
1391
|
let gpsForThisCapture = (withExifLocation ?? false) ? self.currentLocation : nil
|
|
1392
|
+
let locationForExif = (withExifLocation ?? false) ? self.currentLocation : nil
|
|
1393
|
+
let headingForExif = (withExifLocation ?? false) ? self.currentHeading : nil
|
|
1394
|
+
let storeToFile = self.storeToFile
|
|
1381
1395
|
self.cameraController.captureImage(width: width, height: height, quality: quality, gpsLocation: gpsForThisCapture, embedTimestamp: embedTimestamp, embedLocation: effectiveEmbedLocation, photoQualityPrioritization: photoQualityPrioritization) { (image, originalPhotoData, _, error) in
|
|
1382
1396
|
print("[CameraPreview] captureImage callback received")
|
|
1383
|
-
|
|
1384
|
-
print("[CameraPreview] Processing capture on
|
|
1385
|
-
// Ensure heading updates are stopped on all exit paths (error, guard failure, or success)
|
|
1397
|
+
CapturePhotoProcessingQueue.async {
|
|
1398
|
+
print("[CameraPreview] Processing capture on background queue (isMain=\(Thread.isMainThread))")
|
|
1386
1399
|
defer {
|
|
1387
1400
|
if withExifLocation ?? false {
|
|
1388
|
-
|
|
1389
|
-
|
|
1401
|
+
DispatchQueue.main.async {
|
|
1402
|
+
self.locationManager?.stopUpdatingHeading()
|
|
1403
|
+
self.currentHeading = nil
|
|
1404
|
+
}
|
|
1390
1405
|
}
|
|
1391
1406
|
}
|
|
1392
1407
|
if let error = error {
|
|
@@ -1411,8 +1426,8 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
1411
1426
|
guard let imageDataWithExif = self.createImageDataWithExif(
|
|
1412
1427
|
from: imageToEncode,
|
|
1413
1428
|
quality: Int(quality),
|
|
1414
|
-
location:
|
|
1415
|
-
heading:
|
|
1429
|
+
location: locationForExif,
|
|
1430
|
+
heading: headingForExif,
|
|
1416
1431
|
originalPhotoData: originalPhotoData
|
|
1417
1432
|
)
|
|
1418
1433
|
else {
|
|
@@ -1423,13 +1438,12 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
1423
1438
|
|
|
1424
1439
|
print("[CameraPreview] Image data created, size: \(imageDataWithExif.count) bytes")
|
|
1425
1440
|
|
|
1426
|
-
// Prepare the result first
|
|
1427
1441
|
let exifData = self.getExifData(from: imageDataWithExif)
|
|
1428
1442
|
|
|
1429
1443
|
var result = JSObject()
|
|
1430
1444
|
result["exif"] = exifData
|
|
1431
1445
|
|
|
1432
|
-
if
|
|
1446
|
+
if storeToFile == false {
|
|
1433
1447
|
let base64Image = imageDataWithExif.base64EncodedString()
|
|
1434
1448
|
result["value"] = base64Image
|
|
1435
1449
|
} else {
|
|
@@ -1443,7 +1457,6 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
1443
1457
|
}
|
|
1444
1458
|
}
|
|
1445
1459
|
|
|
1446
|
-
// Save to gallery asynchronously if requested
|
|
1447
1460
|
if saveToGallery {
|
|
1448
1461
|
print("[CameraPreview] Saving to gallery asynchronously...")
|
|
1449
1462
|
DispatchQueue.global(qos: .utility).async {
|
|
@@ -1453,7 +1466,7 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
1453
1466
|
}
|
|
1454
1467
|
}
|
|
1455
1468
|
|
|
1456
|
-
print("[CameraPreview] Resolving capture call
|
|
1469
|
+
print("[CameraPreview] Resolving capture call from background queue")
|
|
1457
1470
|
call.resolve(result)
|
|
1458
1471
|
}
|
|
1459
1472
|
}
|
|
@@ -1562,11 +1575,6 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
1562
1575
|
return jpegDataAtQuality
|
|
1563
1576
|
}
|
|
1564
1577
|
|
|
1565
|
-
let mutableData = NSMutableData()
|
|
1566
|
-
guard let destination = CGImageDestinationCreateWithData(mutableData, kUTTypeJPEG, 1, nil) else {
|
|
1567
|
-
return jpegDataAtQuality
|
|
1568
|
-
}
|
|
1569
|
-
|
|
1570
1578
|
var finalProperties = imageProperties
|
|
1571
1579
|
|
|
1572
1580
|
// Ensure orientation reflects the pixel data (we pass an orientation-fixed UIImage)
|
|
@@ -1613,10 +1621,8 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
1613
1621
|
tiffDict[kCGImagePropertyTIFFOrientation as String] = 1
|
|
1614
1622
|
finalProperties[kCGImagePropertyTIFFDictionary as String] = tiffDict
|
|
1615
1623
|
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
if CGImageDestinationFinalize(destination) {
|
|
1619
|
-
return mutableData as Data
|
|
1624
|
+
if let encoded = ImageIOJpegEncoding.encode(cgImage: cgImage, properties: finalProperties, qualityPercent: quality) {
|
|
1625
|
+
return encoded
|
|
1620
1626
|
}
|
|
1621
1627
|
|
|
1622
1628
|
return jpegDataAtQuality
|
|
@@ -1821,7 +1827,8 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
1821
1827
|
if let frameRate = call.getInt("frameRate") {
|
|
1822
1828
|
try self.cameraController.setVideoFrameRate(frameRate)
|
|
1823
1829
|
}
|
|
1824
|
-
let allowHapticsAndSystemSoundsDuringRecording = call.getBool("allowHapticsAndSystemSoundsDuringRecording")
|
|
1830
|
+
let allowHapticsAndSystemSoundsDuringRecording = call.getBool("allowHapticsAndSystemSoundsDuringRecording")
|
|
1831
|
+
?? self.cameraController.allowHapticsAndSystemSoundsDuringRecording
|
|
1825
1832
|
let mirrorFrontCamera = call.getBool("mirrorFrontCamera") ?? false
|
|
1826
1833
|
try self.cameraController.captureVideo(
|
|
1827
1834
|
maxDuration: maxDuration,
|
|
@@ -2438,12 +2445,7 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
2438
2445
|
}
|
|
2439
2446
|
|
|
2440
2447
|
DispatchQueue.main.async {
|
|
2441
|
-
|
|
2442
|
-
result["x"] = Double(self.previewView.frame.origin.x)
|
|
2443
|
-
result["y"] = Double(self.previewView.frame.origin.y)
|
|
2444
|
-
result["width"] = Double(self.previewView.frame.width)
|
|
2445
|
-
result["height"] = Double(self.previewView.frame.height)
|
|
2446
|
-
call.resolve(result)
|
|
2448
|
+
call.resolve(self.visiblePreviewSizeJSObject())
|
|
2447
2449
|
}
|
|
2448
2450
|
}
|
|
2449
2451
|
|
|
@@ -2480,13 +2482,7 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
|
|
|
2480
2482
|
self.updateCameraFrame()
|
|
2481
2483
|
self.makeWebViewTransparent()
|
|
2482
2484
|
|
|
2483
|
-
|
|
2484
|
-
var result = JSObject()
|
|
2485
|
-
result["x"] = Double(self.previewView.frame.origin.x)
|
|
2486
|
-
result["y"] = Double(self.previewView.frame.origin.y)
|
|
2487
|
-
result["width"] = Double(self.previewView.frame.width)
|
|
2488
|
-
result["height"] = Double(self.previewView.frame.height)
|
|
2489
|
-
call.resolve(result)
|
|
2485
|
+
call.resolve(self.visiblePreviewSizeJSObject())
|
|
2490
2486
|
}
|
|
2491
2487
|
}
|
|
2492
2488
|
|