@capgo/camera-preview 8.12.4 → 8.13.0

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.
Files changed (30) hide show
  1. package/README.md +56 -51
  2. package/android/src/main/java/app/capgo/capacitor/camera/preview/CameraPreview.java +13 -2
  3. package/android/src/main/java/app/capgo/capacitor/camera/preview/CameraXView.java +17 -4
  4. package/android/src/main/java/app/capgo/capacitor/camera/preview/CaptureOrientationResolver.java +28 -0
  5. package/android/src/test/java/app/capgo/capacitor/camera/preview/CaptureOrientationResolverTest.java +31 -0
  6. package/dist/docs.json +24 -4
  7. package/dist/esm/definitions.d.ts +28 -8
  8. package/dist/esm/definitions.js.map +1 -1
  9. package/dist/esm/web.js +6 -3
  10. package/dist/esm/web.js.map +1 -1
  11. package/dist/plugin.cjs.js +6 -3
  12. package/dist/plugin.cjs.js.map +1 -1
  13. package/dist/plugin.js +6 -3
  14. package/dist/plugin.js.map +1 -1
  15. package/ios/Sources/CapgoCameraPreviewPlugin/AspectRatioLayout.swift +40 -0
  16. package/ios/Sources/CapgoCameraPreviewPlugin/CameraController.swift +246 -57
  17. package/ios/Sources/CapgoCameraPreviewPlugin/CaptureImageExifEncoder.swift +88 -0
  18. package/ios/Sources/CapgoCameraPreviewPlugin/CaptureLifecycleCoordination.swift +37 -0
  19. package/ios/Sources/CapgoCameraPreviewPlugin/CaptureOrientationResolver.swift +23 -0
  20. package/ios/Sources/CapgoCameraPreviewPlugin/CapturePhotoProcessingQueue.swift +20 -0
  21. package/ios/Sources/CapgoCameraPreviewPlugin/CaptureResolutionSupport.swift +133 -0
  22. package/ios/Sources/CapgoCameraPreviewPlugin/ImageIOJpegEncoding.swift +28 -0
  23. package/ios/Sources/CapgoCameraPreviewPlugin/Plugin.swift +74 -142
  24. package/ios/Tests/CameraPreviewPluginTests/CameraPreviewPluginTests.swift +26 -0
  25. package/ios/Tests/CameraPreviewPluginTests/CaptureImageExifEncoderTests.swift +141 -0
  26. package/ios/Tests/CameraPreviewPluginTests/CaptureLifecycleCoordinationTests.swift +73 -0
  27. package/ios/Tests/CameraPreviewPluginTests/CaptureOrientationResolverTests.swift +30 -0
  28. package/ios/Tests/CameraPreviewPluginTests/CaptureResolutionSupportTests.swift +133 -0
  29. package/ios/Tests/CameraPreviewPluginTests/ImageIOJpegEncodingTests.swift +42 -0
  30. package/package.json +5 -3
@@ -0,0 +1,141 @@
1
+ import CoreLocation
2
+ import ImageIO
3
+ import UIKit
4
+ import XCTest
5
+ @testable import CapgoCameraPreview
6
+
7
+ final class CaptureImageExifEncoderTests: XCTestCase {
8
+ func testSkipsJpegEncodeWhenOriginalPhotoDataProvided() {
9
+ let image = makeTestImage()
10
+ guard let originalPhotoData = image.jpegData(compressionQuality: 0.9) else {
11
+ XCTFail("Failed to build fixture JPEG")
12
+ return
13
+ }
14
+
15
+ var encodeCallCount = 0
16
+ let result = CaptureImageExifEncoder.createImageData(
17
+ from: image,
18
+ quality: 85,
19
+ location: nil,
20
+ heading: nil,
21
+ originalPhotoData: originalPhotoData,
22
+ jpegEncoder: { _, _ in
23
+ encodeCallCount += 1
24
+ return nil
25
+ }
26
+ )
27
+
28
+ XCTAssertNotNil(result)
29
+ XCTAssertEqual(encodeCallCount, 0)
30
+ }
31
+
32
+ func testProducesJpegWithTiffMetadata() {
33
+ let image = makeTestImage()
34
+ guard let originalPhotoData = image.jpegData(compressionQuality: 0.9) else {
35
+ XCTFail("Failed to build fixture JPEG")
36
+ return
37
+ }
38
+
39
+ let result = CaptureImageExifEncoder.createImageData(
40
+ from: image,
41
+ quality: 85,
42
+ location: nil,
43
+ heading: nil,
44
+ originalPhotoData: originalPhotoData
45
+ )
46
+
47
+ guard let result,
48
+ let source = CGImageSourceCreateWithData(result as CFData, nil),
49
+ let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [String: Any],
50
+ let tiff = properties[kCGImagePropertyTIFFDictionary as String] as? [String: Any] else {
51
+ XCTFail("Expected JPEG output with TIFF metadata")
52
+ return
53
+ }
54
+
55
+ XCTAssertEqual(tiff[kCGImagePropertyTIFFMake as String] as? String, "Apple")
56
+ }
57
+
58
+ func testHonorsRequestedQualityWhenOriginalPhotoDataProvided() {
59
+ let size = CGSize(width: 400, height: 300)
60
+ let renderer = UIGraphicsImageRenderer(size: size)
61
+ let image = renderer.image { context in
62
+ UIColor.red.setFill()
63
+ context.fill(CGRect(origin: .zero, size: size))
64
+ UIColor.blue.setFill()
65
+ context.fill(CGRect(x: 50, y: 50, width: 300, height: 200))
66
+ }
67
+ guard let originalPhotoData = image.jpegData(compressionQuality: 1.0) else {
68
+ XCTFail("Failed to build fixture JPEG")
69
+ return
70
+ }
71
+
72
+ guard let lowQuality = CaptureImageExifEncoder.createImageData(
73
+ from: image,
74
+ quality: 20,
75
+ location: nil,
76
+ heading: nil,
77
+ originalPhotoData: originalPhotoData
78
+ ),
79
+ let highQuality = CaptureImageExifEncoder.createImageData(
80
+ from: image,
81
+ quality: 95,
82
+ location: nil,
83
+ heading: nil,
84
+ originalPhotoData: originalPhotoData
85
+ ) else {
86
+ XCTFail("Expected JPEG output at both quality levels")
87
+ return
88
+ }
89
+
90
+ XCTAssertLessThan(lowQuality.count, highQuality.count)
91
+ }
92
+
93
+ func testFallsBackToJpegEncoderWhenMetadataReadFails() {
94
+ let image = makeTestImage()
95
+ let invalidOriginalData = Data([0x00, 0x01, 0x02, 0x03])
96
+
97
+ var encodeCallCount = 0
98
+ let result = CaptureImageExifEncoder.createImageData(
99
+ from: image,
100
+ quality: 85,
101
+ location: nil,
102
+ heading: nil,
103
+ originalPhotoData: invalidOriginalData,
104
+ jpegEncoder: { _, _ in
105
+ encodeCallCount += 1
106
+ return image.jpegData(compressionQuality: 0.85)
107
+ }
108
+ )
109
+
110
+ XCTAssertNotNil(result)
111
+ XCTAssertEqual(encodeCallCount, 1)
112
+ }
113
+
114
+ func testEncodesWhenOriginalPhotoDataMissing() {
115
+ let image = makeTestImage()
116
+
117
+ var encodeCallCount = 0
118
+ let result = CaptureImageExifEncoder.createImageData(
119
+ from: image,
120
+ quality: 85,
121
+ location: nil,
122
+ heading: nil,
123
+ originalPhotoData: nil,
124
+ jpegEncoder: { _, _ in
125
+ encodeCallCount += 1
126
+ return image.jpegData(compressionQuality: 0.85)
127
+ }
128
+ )
129
+
130
+ XCTAssertNotNil(result)
131
+ XCTAssertGreaterThanOrEqual(encodeCallCount, 1)
132
+ }
133
+
134
+ private func makeTestImage() -> UIImage {
135
+ let renderer = UIGraphicsImageRenderer(size: CGSize(width: 32, height: 32))
136
+ return renderer.image { context in
137
+ UIColor.red.setFill()
138
+ context.fill(CGRect(x: 0, y: 0, width: 32, height: 32))
139
+ }
140
+ }
141
+ }
@@ -0,0 +1,73 @@
1
+ import XCTest
2
+ @testable import CapgoCameraPreview
3
+
4
+ final class CaptureLifecycleCoordinationTests: XCTestCase {
5
+ func testDeferredStopRunsCleanupWhenCaptureFinishes() {
6
+ var state = CaptureLifecycleState()
7
+ state.isCapturingPhoto = true
8
+ var cleanupCount = 0
9
+
10
+ CaptureLifecycleCoordination.handleStop(state: &state, force: false, cleanup: { cleanupCount += 1 })
11
+
12
+ XCTAssertTrue(state.stopRequestedAfterCapture)
13
+ XCTAssertTrue(state.isCapturingPhoto)
14
+ XCTAssertEqual(cleanupCount, 0)
15
+
16
+ CaptureLifecycleCoordination.finishCapture(state: &state, cleanup: { cleanupCount += 1 })
17
+
18
+ XCTAssertFalse(state.isCapturingPhoto)
19
+ XCTAssertFalse(state.stopRequestedAfterCapture)
20
+ XCTAssertEqual(cleanupCount, 1)
21
+ }
22
+
23
+ func testForceStopDuringCaptureCleansUpImmediately() {
24
+ var state = CaptureLifecycleState()
25
+ state.isCapturingPhoto = true
26
+ var cleanupCount = 0
27
+
28
+ CaptureLifecycleCoordination.handleStop(state: &state, force: true, cleanup: { cleanupCount += 1 })
29
+
30
+ XCTAssertFalse(state.stopRequestedAfterCapture)
31
+ XCTAssertEqual(cleanupCount, 1)
32
+ }
33
+
34
+ func testFinishCaptureWithoutDeferredStopDoesNotCleanup() {
35
+ var state = CaptureLifecycleState()
36
+ state.isCapturingPhoto = true
37
+ var cleanupCount = 0
38
+
39
+ CaptureLifecycleCoordination.finishCapture(state: &state, cleanup: { cleanupCount += 1 })
40
+
41
+ XCTAssertFalse(state.isCapturingPhoto)
42
+ XCTAssertFalse(state.stopRequestedAfterCapture)
43
+ XCTAssertEqual(cleanupCount, 0)
44
+ }
45
+
46
+ func testStopAfterCaptureAlreadyFinishedCleansUpImmediately() {
47
+ var state = CaptureLifecycleState()
48
+ state.isCapturingPhoto = false
49
+ var cleanupCount = 0
50
+
51
+ CaptureLifecycleCoordination.handleStop(state: &state, force: false, cleanup: { cleanupCount += 1 })
52
+
53
+ XCTAssertFalse(state.stopRequestedAfterCapture)
54
+ XCTAssertEqual(cleanupCount, 1)
55
+ }
56
+
57
+ func testCoordinatorDeferredStopThenFinishCleansUpOnce() {
58
+ var state = CaptureLifecycleState()
59
+ state.isCapturingPhoto = true
60
+ var cleanupCount = 0
61
+ let finishExpectation = expectation(description: "finish on main")
62
+
63
+ DispatchQueue.main.async {
64
+ CaptureLifecycleCoordination.handleStop(state: &state, force: false, cleanup: { cleanupCount += 1 })
65
+ CaptureLifecycleCoordination.finishCapture(state: &state, cleanup: { cleanupCount += 1 })
66
+ finishExpectation.fulfill()
67
+ }
68
+
69
+ waitForExpectations(timeout: 5)
70
+ XCTAssertEqual(cleanupCount, 1)
71
+ XCTAssertFalse(state.stopRequestedAfterCapture)
72
+ }
73
+ }
@@ -0,0 +1,30 @@
1
+ import AVFoundation
2
+ import XCTest
3
+ @testable import CapgoCameraPreview
4
+
5
+ final class CaptureOrientationResolverTests: XCTestCase {
6
+ func testDefaultsToDeviceOrientation() {
7
+ XCTAssertEqual(
8
+ CaptureOrientationResolver.resolveOrientation(
9
+ mode: nil,
10
+ previewOrientation: .portrait,
11
+ deviceOrientation: .landscapeRight
12
+ ),
13
+ .landscapeRight
14
+ )
15
+ XCTAssertTrue(CaptureOrientationResolver.usesDeviceOrientation(mode: nil))
16
+ XCTAssertTrue(CaptureOrientationResolver.usesDeviceOrientation(mode: "device"))
17
+ }
18
+
19
+ func testPreviewModeUsesPreviewOrientation() {
20
+ XCTAssertEqual(
21
+ CaptureOrientationResolver.resolveOrientation(
22
+ mode: "preview",
23
+ previewOrientation: .landscapeLeft,
24
+ deviceOrientation: .portrait
25
+ ),
26
+ .landscapeLeft
27
+ )
28
+ XCTAssertFalse(CaptureOrientationResolver.usesDeviceOrientation(mode: "preview"))
29
+ }
30
+ }
@@ -0,0 +1,133 @@
1
+ import AVFoundation
2
+ import XCTest
3
+ @testable import CapgoCameraPreview
4
+
5
+ final class CaptureResolutionSupportTests: XCTestCase {
6
+ func testParseCaptureResolutionNullObject() {
7
+ let result = CaptureResolutionSupport.parseStartCaptureResolution(nil)
8
+ XCTAssertFalse(result.isError)
9
+ XCTAssertNil(result.width)
10
+ XCTAssertNil(result.height)
11
+ }
12
+
13
+ func testParseCaptureResolutionMissingWidthOrHeight() {
14
+ let missingWidth = CaptureResolutionSupport.parseStartCaptureResolution(["height": 1080])
15
+ XCTAssertTrue(missingWidth.isError)
16
+ XCTAssertEqual(missingWidth.errorMessage, "captureResolution requires both width and height")
17
+
18
+ let missingHeight = CaptureResolutionSupport.parseStartCaptureResolution(["width": 1920])
19
+ XCTAssertTrue(missingHeight.isError)
20
+ }
21
+
22
+ func testParseCaptureResolutionInvalidTypesAndValues() {
23
+ XCTAssertTrue(CaptureResolutionSupport.parseStartCaptureResolution("not-an-object").isError)
24
+
25
+ let zero = CaptureResolutionSupport.parseStartCaptureResolution(["width": 0, "height": 1080])
26
+ XCTAssertTrue(zero.isError)
27
+
28
+ let negative = CaptureResolutionSupport.parseStartCaptureResolution(["width": -1, "height": 1080])
29
+ XCTAssertTrue(negative.isError)
30
+
31
+ let nanWidth = CaptureResolutionSupport.parseStartCaptureResolution(["width": "NaN", "height": 1080])
32
+ XCTAssertTrue(nanWidth.isError)
33
+ }
34
+
35
+ func testParseCaptureResolutionStringNumbers() {
36
+ let result = CaptureResolutionSupport.parseStartCaptureResolution(["width": "1920", "height": "1080"])
37
+ XCTAssertFalse(result.isError)
38
+ XCTAssertEqual(result.width, 1920)
39
+ XCTAssertEqual(result.height, 1080)
40
+ }
41
+
42
+ func testNormalizedCaptureTargetResolutionSwapsToLandscapeMajor() {
43
+ let normalized = CaptureResolutionSupport.normalizedCaptureTargetResolution(configuredWidth: 1080, configuredHeight: 1920)
44
+ XCTAssertEqual(normalized.width, 1920)
45
+ XCTAssertEqual(normalized.height, 1080)
46
+ }
47
+
48
+ func testPhotoMaxDimensionsSelectsSupportedValueWithinCap() {
49
+ let deviceMax = CMVideoDimensions(width: 4032, height: 3024)
50
+ let supported = [
51
+ CMVideoDimensions(width: 4032, height: 3024),
52
+ CMVideoDimensions(width: 1920, height: 1080),
53
+ CMVideoDimensions(width: 1280, height: 720)
54
+ ]
55
+ let dimensions = CaptureResolutionSupport.photoMaxDimensions(
56
+ configuredWidth: 1080,
57
+ configuredHeight: 1920,
58
+ supportedDimensions: supported,
59
+ deviceMax: deviceMax
60
+ )
61
+ XCTAssertEqual(dimensions.width, 1920)
62
+ XCTAssertEqual(dimensions.height, 1080)
63
+ }
64
+
65
+ func testPhotoMaxDimensionsClampsToConfiguredCapWhenNoSupportedSizeFits() {
66
+ let deviceMax = CMVideoDimensions(width: 4032, height: 3024)
67
+ let supported = [
68
+ CMVideoDimensions(width: 4032, height: 3024),
69
+ CMVideoDimensions(width: 1920, height: 1080)
70
+ ]
71
+ let dimensions = CaptureResolutionSupport.photoMaxDimensions(
72
+ configuredWidth: 640,
73
+ configuredHeight: 480,
74
+ supportedDimensions: supported,
75
+ deviceMax: deviceMax
76
+ )
77
+ XCTAssertEqual(dimensions.width, 640)
78
+ XCTAssertEqual(dimensions.height, 480)
79
+ }
80
+
81
+ func testParseCaptureResolutionRejectsInt32Overflow() {
82
+ let overflow = CaptureResolutionSupport.parseStartCaptureResolution([
83
+ "width": Int64(Int32.max) + 1,
84
+ "height": 1080
85
+ ])
86
+ XCTAssertTrue(overflow.isError)
87
+ }
88
+
89
+ func testShouldEnableHighResolutionPhotoWhenCaptureResolutionSet() {
90
+ XCTAssertFalse(
91
+ CaptureResolutionSupport.shouldEnableHighResolutionPhoto(
92
+ captureResolutionWidth: 1920,
93
+ captureResolutionHeight: 1080,
94
+ captureMaxWidth: nil,
95
+ captureMaxHeight: nil
96
+ )
97
+ )
98
+ }
99
+
100
+ func testShouldEnableHighResolutionPhotoWhenCaptureMaxDimensionsLarge() {
101
+ XCTAssertTrue(
102
+ CaptureResolutionSupport.shouldEnableHighResolutionPhoto(
103
+ captureResolutionWidth: nil,
104
+ captureResolutionHeight: nil,
105
+ captureMaxWidth: 2048,
106
+ captureMaxHeight: nil
107
+ )
108
+ )
109
+ }
110
+ }
111
+
112
+ final class CapturePhotoProcessingQueueTests: XCTestCase {
113
+ func testProcessingQueueIsNotMainThread() {
114
+ let expectation = expectation(description: "background processing")
115
+ var ranOnMain = true
116
+ CapturePhotoProcessingQueue.async {
117
+ ranOnMain = Thread.isMainThread
118
+ expectation.fulfill()
119
+ }
120
+ waitForExpectations(timeout: 5)
121
+ XCTAssertFalse(ranOnMain)
122
+ }
123
+
124
+ func testIsCurrentQueueOnlyOnProcessingQueue() {
125
+ XCTAssertFalse(CapturePhotoProcessingQueue.isCurrentQueue)
126
+ let expectation = expectation(description: "queue marker")
127
+ CapturePhotoProcessingQueue.async {
128
+ XCTAssertTrue(CapturePhotoProcessingQueue.isCurrentQueue)
129
+ expectation.fulfill()
130
+ }
131
+ waitForExpectations(timeout: 5)
132
+ }
133
+ }
@@ -0,0 +1,42 @@
1
+ import XCTest
2
+ @testable import CapgoCameraPreview
3
+
4
+ final class ImageIOJpegEncodingTests: XCTestCase {
5
+ func testApplyingCompressionQualitySetsImageIODestinationKey() {
6
+ let properties = ImageIOJpegEncoding.applyingCompressionQuality(42, to: [:])
7
+ let quality = properties[kCGImageDestinationLossyCompressionQuality as String] as? CGFloat
8
+ XCTAssertEqual(quality ?? -1, 0.42, accuracy: 0.0001)
9
+ }
10
+
11
+ func testEncodeProducesSmallerOutputAtLowerQuality() throws {
12
+ let size = CGSize(width: 800, height: 600)
13
+ let renderer = UIGraphicsImageRenderer(size: size)
14
+ let image = renderer.image { context in
15
+ UIColor.red.setFill()
16
+ context.fill(CGRect(origin: .zero, size: size))
17
+ UIColor.blue.setFill()
18
+ context.fill(CGRect(x: 100, y: 100, width: 600, height: 400))
19
+ }
20
+ guard let cgImage = image.cgImage else {
21
+ XCTFail("Failed to create test CGImage")
22
+ return
23
+ }
24
+
25
+ let sourceJPEG = image.jpegData(compressionQuality: 1.0)
26
+ XCTAssertNotNil(sourceJPEG)
27
+ guard let sourceJPEG,
28
+ let imageSource = CGImageSourceCreateWithData(sourceJPEG as CFData, nil),
29
+ let metadata = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [String: Any] else {
30
+ XCTFail("Failed to read JPEG metadata for test fixture")
31
+ return
32
+ }
33
+
34
+ guard let lowQualityData = ImageIOJpegEncoding.encode(cgImage: cgImage, properties: metadata, qualityPercent: 20),
35
+ let highQualityData = ImageIOJpegEncoding.encode(cgImage: cgImage, properties: metadata, qualityPercent: 95) else {
36
+ XCTFail("ImageIO JPEG encode failed")
37
+ return
38
+ }
39
+
40
+ XCTAssertLessThan(lowQualityData.count, highQualityData.count)
41
+ }
42
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/camera-preview",
3
- "version": "8.12.4",
3
+ "version": "8.13.0",
4
4
  "description": "Camera preview",
5
5
  "license": "MPL-2.0",
6
6
  "repository": {
@@ -44,7 +44,8 @@
44
44
  "verify:ios": "xcodebuild -scheme CapgoCameraPreview -destination generic/platform=iOS && bun run test:ios",
45
45
  "test:ios": "bash ./scripts/test-ios.sh",
46
46
  "verify:android": "cd android && ./gradlew clean build test && cd ..",
47
- "verify:web": "bun run build",
47
+ "verify:web": "bun run build && bun run test:web",
48
+ "test:web": "bun test src/web.stop.test.ts",
48
49
  "lint": "bun run eslint && bun run prettier -- --check && bun run swiftlint -- lint",
49
50
  "fmt": "bun run eslint -- --fix && bun run prettier -- --write && bun run swiftlint -- --fix --format",
50
51
  "eslint": "eslint .",
@@ -82,7 +83,8 @@
82
83
  "rollup": "^4.53.2",
83
84
  "swiftlint": "^2.0.0",
84
85
  "typescript": "^6.0.3",
85
- "prettier-pretty-check": "^0.2.0"
86
+ "prettier-pretty-check": "^0.2.0",
87
+ "happy-dom": "^20.0.0"
86
88
  },
87
89
  "peerDependencies": {
88
90
  "@capacitor/core": ">=8.0.0"