@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.
Files changed (29) hide show
  1. package/README.md +43 -39
  2. package/android/src/main/java/app/capgo/capacitor/camera/preview/CameraXView.java +26 -28
  3. package/android/src/main/java/app/capgo/capacitor/camera/preview/PhysicalOrientationResolver.java +72 -0
  4. package/android/src/test/java/app/capgo/capacitor/camera/preview/PhysicalOrientationResolverTest.java +90 -0
  5. package/dist/docs.json +8 -4
  6. package/dist/esm/definitions.d.ts +20 -8
  7. package/dist/esm/definitions.js.map +1 -1
  8. package/dist/esm/web.js +6 -3
  9. package/dist/esm/web.js.map +1 -1
  10. package/dist/plugin.cjs.js +6 -3
  11. package/dist/plugin.cjs.js.map +1 -1
  12. package/dist/plugin.js +6 -3
  13. package/dist/plugin.js.map +1 -1
  14. package/ios/Sources/CapgoCameraPreviewPlugin/ApplicationAudioSessionRestoreCoordinator.swift +136 -0
  15. package/ios/Sources/CapgoCameraPreviewPlugin/ApplicationAudioSessionSnapshot.swift +28 -0
  16. package/ios/Sources/CapgoCameraPreviewPlugin/AspectRatioLayout.swift +40 -0
  17. package/ios/Sources/CapgoCameraPreviewPlugin/CameraController.swift +204 -34
  18. package/ios/Sources/CapgoCameraPreviewPlugin/CapturePhotoProcessingQueue.swift +20 -0
  19. package/ios/Sources/CapgoCameraPreviewPlugin/CaptureResolutionSupport.swift +133 -0
  20. package/ios/Sources/CapgoCameraPreviewPlugin/ImageIOJpegEncoding.swift +28 -0
  21. package/ios/Sources/CapgoCameraPreviewPlugin/PhysicalOrientationResolver.swift +59 -0
  22. package/ios/Sources/CapgoCameraPreviewPlugin/Plugin.swift +59 -63
  23. package/ios/Tests/CameraPreviewPluginTests/ApplicationAudioSessionRestoreCoordinatorTests.swift +279 -0
  24. package/ios/Tests/CameraPreviewPluginTests/ApplicationAudioSessionSnapshotTests.swift +25 -0
  25. package/ios/Tests/CameraPreviewPluginTests/CameraPreviewPluginTests.swift +26 -0
  26. package/ios/Tests/CameraPreviewPluginTests/CaptureResolutionSupportTests.swift +133 -0
  27. package/ios/Tests/CameraPreviewPluginTests/ImageIOJpegEncodingTests.swift +42 -0
  28. package/ios/Tests/CameraPreviewPluginTests/PhysicalOrientationResolverTests.swift +89 -0
  29. package/package.json +5 -3
package/README.md CHANGED
@@ -1114,7 +1114,11 @@ On Android, if a physical-lens request falls back to a logical camera, this retu
1114
1114
  getPreviewSize() => Promise<{ x: number; y: number; width: number; height: number; }>
1115
1115
  ```
1116
1116
 
1117
- Gets the current preview size and position.
1117
+ Gets the current visible camera preview bounds in web-view coordinates.
1118
+
1119
+ With `aspectMode: 'contain'`, this is the letterboxed camera image rect inside the
1120
+ preview container (not the full container when bars are shown). With `aspectMode: 'cover'`,
1121
+ this matches the preview container bounds on both platforms.
1118
1122
 
1119
1123
  **Returns:** <code>Promise&lt;{ x: number; y: number; width: number; height: number; }&gt;</code>
1120
1124
 
@@ -1559,44 +1563,44 @@ Get the native Capacitor plugin version
1559
1563
 
1560
1564
  Defines the configuration options for starting the camera preview.
1561
1565
 
1562
- | Prop | Type | Description | Default | Since |
1563
- | ------------------------------------------------ | ---------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- | ------ |
1564
- | **`parent`** | <code>string</code> | The parent element to attach the video preview to. | | |
1565
- | **`className`** | <code>string</code> | A CSS class name to add to the preview element. | | |
1566
- | **`width`** | <code>number</code> | The width of the preview in pixels. Defaults to the screen width. | | |
1567
- | **`height`** | <code>number</code> | The height of the preview in pixels. Defaults to the screen height. | | |
1568
- | **`x`** | <code>number</code> | The horizontal origin of the preview, in pixels. | | |
1569
- | **`y`** | <code>number</code> | The vertical origin of the preview, in pixels. | | |
1570
- | **`aspectRatio`** | <code><a href="#camerapreviewaspectratio">CameraPreviewAspectRatio</a></code> | The aspect ratio of the camera preview, '4:3' or '16:9' or 'fill'. Cannot be set if width or height is provided, otherwise the call will be rejected. Use setPreviewSize to adjust size after starting. | | 2.0.0 |
1571
- | **`aspectMode`** | <code>'cover' \| 'contain'</code> | Controls how the camera preview fills the available space. - 'contain': Fits the entire preview within the space, may show letterboxing (default). - 'cover': Fills the entire space, may crop edges of the preview. | <code>"contain"</code> | |
1572
- | **`gridMode`** | <code><a href="#gridmode">GridMode</a></code> | The grid overlay to display on the camera preview. | <code>"none"</code> | 2.1.0 |
1573
- | **`includeSafeAreaInsets`** | <code>boolean</code> | Adjusts the y-position to account for safe areas (e.g., notches). | <code>false</code> | |
1574
- | **`toBack`** | <code>boolean</code> | If true, places the preview behind the webview. | <code>true</code> | |
1575
- | **`paddingBottom`** | <code>number</code> | Bottom padding for the preview, in pixels. | | |
1576
- | **`rotateWhenOrientationChanged`** | <code>boolean</code> | Whether to rotate the preview when the device orientation changes. | <code>true</code> | |
1577
- | **`position`** | <code>string</code> | The camera to use. | <code>"rear"</code> | |
1578
- | **`storeToFile`** | <code>boolean</code> | If true, saves the captured image to a file and returns the file path. If false, returns a base64 encoded string. | <code>false</code> | |
1579
- | **`disableExifHeaderStripping`** | <code>boolean</code> | If true, prevents the plugin from rotating the image based on EXIF data. | <code>false</code> | |
1580
- | **`disableAudio`** | <code>boolean</code> | If true, disables the audio stream, preventing audio permission requests. | <code>true</code> | |
1581
- | **`lockAndroidOrientation`** | <code>boolean</code> | If true, locks the device orientation while the camera is active. | <code>false</code> | |
1582
- | **`enableOpacity`** | <code>boolean</code> | If true, allows the camera preview's opacity to be changed. | <code>false</code> | |
1583
- | **`disableFocusIndicator`** | <code>boolean</code> | If true, disables the visual focus indicator when tapping to focus. | <code>false</code> | |
1584
- | **`deviceId`** | <code>string</code> | The `deviceId` of the camera to use. If provided, `position` is ignored. | | |
1585
- | **`enablePhysicalDeviceSelection`** | <code>boolean</code> | On Android, attempts to bind a physical camera directly when `deviceId` refers to a physical lens. Disabled by default because OEM support is inconsistent; when false, Android keeps the current logical-camera fallback behavior. | <code>false</code> | |
1586
- | **`initialZoomLevel`** | <code>number</code> | The initial zoom level when starting the camera preview. If the requested zoom level is not available, the native plugin will reject. | <code>1.0</code> | 2.2.0 |
1587
- | **`positioning`** | <code><a href="#camerapositioning">CameraPositioning</a></code> | The vertical positioning of the camera preview. | <code>"center"</code> | 2.3.0 |
1588
- | **`enableVideoMode`** | <code>boolean</code> | If true, enables video capture capabilities when the camera starts. | <code>false</code> | 7.11.0 |
1589
- | **`captureResolution`** | <code><a href="#cameracaptureresolution">CameraCaptureResolution</a></code> | Sets the Android still-capture resolution for the `ImageCapture` use case at session bind time. Preview resolution is unchanged. The shared ViewPort crop from the preview still applies to captures. When omitted, `ImageCapture` uses the highest resolution supported by the active camera (same as versions before 8.11.10). Set this to cap capture size for faster shots on high-megapixel sensors, for example `{ width: 1920, height: 1080 }` or `{ width: 1600, height: 1200 }`. | | 8.12.0 |
1590
- | **`force`** | <code>boolean</code> | If true, forces the camera to start/restart even if it's already running or busy. This will kill the current camera session and start a new one, ignoring all state checks. | <code>false</code> | |
1591
- | **`videoQuality`** | <code><a href="#videoquality">VideoQuality</a></code> | Sets the quality of video for recording. Options: 'low', 'medium', 'high', '2160p', '1080p', '720p', '480p', '4:3' | <code>"high"</code> | |
1592
- | **`maxDuration`** | <code>number</code> | Maximum recording duration in seconds. Recording stops automatically when reached. | | |
1593
- | **`maxFileSize`** | <code>number</code> | Maximum recording file size in bytes. Recording stops automatically when reached. | | |
1594
- | **`videoCodec`** | <code><a href="#videocodec">VideoCodec</a></code> | Preferred video codec for recording. | <code>"avc1"</code> | |
1595
- | **`frameRate`** | <code>number</code> | Target video frame rate in frames per second. Applied when recording starts. Use `getSupportedVideoFrameRates()` to list supported values. | | 8.6.0 |
1596
- | **`videoStabilizationMode`** | <code><a href="#videostabilizationmode">VideoStabilizationMode</a></code> | Preferred video stabilization mode for recording. | <code>"off"</code> | |
1597
- | **`allowHapticsAndSystemSoundsDuringRecording`** | <code>boolean</code> | When `true`, allows haptic feedback and system sounds while recording video with audio. iOS suppresses haptics during audio input recording unless this is enabled. Requires `disableAudio: false`. | <code>false</code> | 8.8.0 |
1598
- | **`mirrorFrontCamera`** | <code>boolean</code> | When `true` and the front camera is active, mirrors the recorded video file so it matches the selfie-style preview (text and gestures are not reversed). Does not affect the live preview. Defaults to `false` to preserve existing behavior. | <code>false</code> | 8.10.0 |
1599
- | **`barcodeScanner`** | <code>boolean \| <a href="#barcodescanneroptions">BarcodeScannerOptions</a></code> | Starts barcode scanning together with the camera preview. Set to `true` or pass options to scan all supported formats. Omit this option to keep barcode scanning disabled at startup. | | 8.8.0 |
1566
+ | Prop | Type | Description | Default | Since |
1567
+ | ------------------------------------------------ | ---------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------- | ------ |
1568
+ | **`parent`** | <code>string</code> | The parent element to attach the video preview to. | | |
1569
+ | **`className`** | <code>string</code> | A CSS class name to add to the preview element. | | |
1570
+ | **`width`** | <code>number</code> | The width of the preview in pixels. Defaults to the screen width. | | |
1571
+ | **`height`** | <code>number</code> | The height of the preview in pixels. Defaults to the screen height. | | |
1572
+ | **`x`** | <code>number</code> | The horizontal origin of the preview, in pixels. | | |
1573
+ | **`y`** | <code>number</code> | The vertical origin of the preview, in pixels. | | |
1574
+ | **`aspectRatio`** | <code><a href="#camerapreviewaspectratio">CameraPreviewAspectRatio</a></code> | The aspect ratio of the camera preview, '4:3' or '16:9' or 'fill'. Cannot be set if width or height is provided, otherwise the call will be rejected. Use setPreviewSize to adjust size after starting. | | 2.0.0 |
1575
+ | **`aspectMode`** | <code>'cover' \| 'contain'</code> | Controls how the camera preview fills the available space. - 'contain': Fits the entire preview within the space, may show letterboxing (default). - 'cover': Fills the entire space, may crop edges of the preview. | <code>"contain"</code> | |
1576
+ | **`gridMode`** | <code><a href="#gridmode">GridMode</a></code> | The grid overlay to display on the camera preview. | <code>"none"</code> | 2.1.0 |
1577
+ | **`includeSafeAreaInsets`** | <code>boolean</code> | Adjusts the y-position to account for safe areas (e.g., notches). | <code>false</code> | |
1578
+ | **`toBack`** | <code>boolean</code> | If true, places the preview behind the webview. | <code>true</code> | |
1579
+ | **`paddingBottom`** | <code>number</code> | Bottom padding for the preview, in pixels. | | |
1580
+ | **`rotateWhenOrientationChanged`** | <code>boolean</code> | Whether to rotate the preview when the device orientation changes. | <code>true</code> | |
1581
+ | **`position`** | <code>string</code> | The camera to use. | <code>"rear"</code> | |
1582
+ | **`storeToFile`** | <code>boolean</code> | If true, saves the captured image to a file and returns the file path. If false, returns a base64 encoded string. | <code>false</code> | |
1583
+ | **`disableExifHeaderStripping`** | <code>boolean</code> | If true, prevents the plugin from rotating the image based on EXIF data. | <code>false</code> | |
1584
+ | **`disableAudio`** | <code>boolean</code> | If true, disables the audio stream, preventing audio permission requests. | <code>true</code> | |
1585
+ | **`lockAndroidOrientation`** | <code>boolean</code> | If true, locks the device orientation while the camera is active. | <code>false</code> | |
1586
+ | **`enableOpacity`** | <code>boolean</code> | If true, allows the camera preview's opacity to be changed. | <code>false</code> | |
1587
+ | **`disableFocusIndicator`** | <code>boolean</code> | If true, disables the visual focus indicator when tapping to focus. | <code>false</code> | |
1588
+ | **`deviceId`** | <code>string</code> | The `deviceId` of the camera to use. If provided, `position` is ignored. | | |
1589
+ | **`enablePhysicalDeviceSelection`** | <code>boolean</code> | On Android, attempts to bind a physical camera directly when `deviceId` refers to a physical lens. Disabled by default because OEM support is inconsistent; when false, Android keeps the current logical-camera fallback behavior. | <code>false</code> | |
1590
+ | **`initialZoomLevel`** | <code>number</code> | The initial zoom level when starting the camera preview. If the requested zoom level is not available, the native plugin will reject. | <code>1.0</code> | 2.2.0 |
1591
+ | **`positioning`** | <code><a href="#camerapositioning">CameraPositioning</a></code> | The vertical positioning of the camera preview. | <code>"center"</code> | 2.3.0 |
1592
+ | **`enableVideoMode`** | <code>boolean</code> | If true, enables video capture capabilities when the camera starts. | <code>false</code> | 7.11.0 |
1593
+ | **`captureResolution`** | <code><a href="#cameracaptureresolution">CameraCaptureResolution</a></code> | Sets the still-capture sensor resolution cap at session start. Preview resolution is unchanged. The shared ViewPort crop from the preview still applies to captures. On Android this binds the `ImageCapture` use case to the requested size. On iOS 16+ this sets `AVCapturePhotoSettings.maxPhotoDimensions` (and disables high-resolution photo capture when capped) so the sensor delivers fewer pixels instead of downscaling a full-sensor frame in software. On iOS 15, `start()` rejects this option. When omitted, capture uses the highest resolution supported by the active camera (same as versions before 8.11.10 on Android, and the format default on iOS). Set this to cap capture size for faster shots on high-megapixel sensors, for example `{ width: 1920, height: 1080 }` or `{ width: 1600, height: 1200 }`. | | 8.12.0 |
1594
+ | **`force`** | <code>boolean</code> | If true, forces the camera to start/restart even if it's already running or busy. This will kill the current camera session and start a new one, ignoring all state checks. | <code>false</code> | |
1595
+ | **`videoQuality`** | <code><a href="#videoquality">VideoQuality</a></code> | Sets the quality of video for recording. Options: 'low', 'medium', 'high', '2160p', '1080p', '720p', '480p', '4:3' | <code>"high"</code> | |
1596
+ | **`maxDuration`** | <code>number</code> | Maximum recording duration in seconds. Recording stops automatically when reached. | | |
1597
+ | **`maxFileSize`** | <code>number</code> | Maximum recording file size in bytes. Recording stops automatically when reached. | | |
1598
+ | **`videoCodec`** | <code><a href="#videocodec">VideoCodec</a></code> | Preferred video codec for recording. | <code>"avc1"</code> | |
1599
+ | **`frameRate`** | <code>number</code> | Target video frame rate in frames per second. Applied when recording starts. Use `getSupportedVideoFrameRates()` to list supported values. | | 8.6.0 |
1600
+ | **`videoStabilizationMode`** | <code><a href="#videostabilizationmode">VideoStabilizationMode</a></code> | Preferred video stabilization mode for recording. | <code>"off"</code> | |
1601
+ | **`allowHapticsAndSystemSoundsDuringRecording`** | <code>boolean</code> | When `true`, allows haptic feedback and system sounds while recording video with audio. iOS suppresses haptics during audio input recording unless this is enabled. Requires `disableAudio: false`. | <code>false</code> | 8.8.0 |
1602
+ | **`mirrorFrontCamera`** | <code>boolean</code> | When `true` and the front camera is active, mirrors the recorded video file so it matches the selfie-style preview (text and gestures are not reversed). Does not affect the live preview. Defaults to `false` to preserve existing behavior. | <code>false</code> | 8.10.0 |
1603
+ | **`barcodeScanner`** | <code>boolean \| <a href="#barcodescanneroptions">BarcodeScannerOptions</a></code> | Starts barcode scanning together with the camera preview. Set to `true` or pass options to scan all supported formats. Omit this option to keep barcode scanning disabled at startup. | | 8.8.0 |
1600
1604
 
1601
1605
 
1602
1606
  #### CameraCaptureResolution
@@ -223,6 +223,7 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
223
223
  private final float[] lastAccelerometerValues = new float[3]; // x,y, and z
224
224
  private final Object accelerometerLock = new Object();
225
225
  private volatile int lastCaptureRotation = -1; // -1 unknown
226
+ private volatile int lastReliableCaptureRotation = -1;
226
227
 
227
228
  // Compass heading (degrees, 0-360, true north); -1 means not yet available
228
229
  private Sensor rotationVectorSensor;
@@ -565,6 +566,7 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
565
566
  lastAccelerometerValues[2] = 0f;
566
567
  }
567
568
  lastCaptureRotation = -1;
569
+ lastReliableCaptureRotation = -1;
568
570
 
569
571
  // Start accelerometer for orientation detection regardless of lock
570
572
  if (sensorManager == null) {
@@ -2207,43 +2209,38 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
2207
2209
  * This works even when portrait lock is enabled.
2208
2210
  * Falls back to display rotation if accelerometer data is not available.
2209
2211
  */
2212
+ private int getDisplayRotationFallback() {
2213
+ if (previewView != null && previewView.getDisplay() != null) {
2214
+ return previewView.getDisplay().getRotation();
2215
+ }
2216
+ return android.view.Surface.ROTATION_0;
2217
+ }
2218
+
2210
2219
  private int getRotationFromAccelerometer() {
2211
- float x, y;
2220
+ float x, y, z;
2212
2221
  synchronized (accelerometerLock) {
2213
2222
  x = lastAccelerometerValues[0];
2214
2223
  y = lastAccelerometerValues[1];
2224
+ z = lastAccelerometerValues[2];
2215
2225
  }
2216
2226
 
2217
- // If no accelerometer data yet, fall back to display rotation
2218
- final float epsilon = 1.0f;
2219
- if (Math.abs(x) < epsilon && Math.abs(y) < epsilon) {
2220
- if (previewView != null && previewView.getDisplay() != null) {
2221
- return previewView.getDisplay().getRotation();
2222
- }
2223
- return android.view.Surface.ROTATION_0;
2227
+ final float gravityEarth = SensorManager.GRAVITY_EARTH;
2228
+ final float epsilonG = 0.1f;
2229
+ if (Math.abs(x) < gravityEarth * epsilonG && Math.abs(y) < gravityEarth * epsilonG && Math.abs(z) < gravityEarth * epsilonG) {
2230
+ return getDisplayRotationFallback();
2224
2231
  }
2225
2232
 
2226
- // Android accelerometer: +X is right, +Y is up, +Z is toward user
2227
- // Determine orientation based on which axis has the strongest gravity component
2228
- if (Math.abs(x) > Math.abs(y)) {
2229
- // Landscape orientation
2230
- if (x > 0) {
2231
- // Device tilted to the left (top of device points left)
2232
- return android.view.Surface.ROTATION_90;
2233
- } else {
2234
- // Device tilted to the right (top of device points right)
2235
- return android.view.Surface.ROTATION_270;
2236
- }
2237
- } else {
2238
- // Portrait orientation
2239
- if (y > 0) {
2240
- // Normal portrait (top of device points up)
2241
- return android.view.Surface.ROTATION_0;
2242
- } else {
2243
- // Upside down portrait
2244
- return android.view.Surface.ROTATION_180;
2245
- }
2233
+ PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(
2234
+ x / gravityEarth,
2235
+ y / gravityEarth,
2236
+ z / gravityEarth,
2237
+ lastReliableCaptureRotation,
2238
+ getDisplayRotationFallback()
2239
+ );
2240
+ if (result.reliable) {
2241
+ lastReliableCaptureRotation = result.rotation;
2246
2242
  }
2243
+ return result.rotation;
2247
2244
  }
2248
2245
 
2249
2246
  public void capturePhoto(
@@ -5410,6 +5407,7 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
5410
5407
 
5411
5408
  // Update video capture rotation from accelerometer for device orientation
5412
5409
  int rotation = getRotationFromAccelerometer();
5410
+ lastCaptureRotation = rotation;
5413
5411
  videoCapture.setTargetRotation(rotation);
5414
5412
  Log.d(TAG, "startRecordVideo: Using rotation " + rotation + " from accelerometer");
5415
5413
 
@@ -0,0 +1,72 @@
1
+ package app.capgo.capacitor.camera.preview;
2
+
3
+ import android.view.Surface;
4
+
5
+ /**
6
+ * Resolves device physical rotation from gravity (accelerometer) for photo and video capture.
7
+ * When the device is near flat, x/y gravity is noisy; keep the last reliable rotation or UI fallback.
8
+ */
9
+ final class PhysicalOrientationResolver {
10
+
11
+ /** |z| above this means gravity mostly points through the screen (camera toward table or ceiling). */
12
+ static final float FLAT_GRAVITY_Z_THRESHOLD = 0.75f;
13
+ /** Below this, horizontal gravity is too weak to infer portrait vs landscape reliably. */
14
+ static final float MIN_HORIZONTAL_GRAVITY = 0.35f;
15
+ /** When |x| and |y| are within this margin, do not flip between landscape and portrait. */
16
+ static final float AXIS_DECISION_MARGIN = 0.12f;
17
+
18
+ static final class Result {
19
+
20
+ final int rotation;
21
+ /** True when rotation came from a strong gravity signal (safe to remember for hysteresis). */
22
+ final boolean reliable;
23
+
24
+ Result(int rotation, boolean reliable) {
25
+ this.rotation = rotation;
26
+ this.reliable = reliable;
27
+ }
28
+ }
29
+
30
+ private PhysicalOrientationResolver() {}
31
+
32
+ /**
33
+ * @param x gravity component on x in g (divide raw m/s^2 by {@link android.hardware.SensorManager#GRAVITY_EARTH})
34
+ * @param y gravity component on y in g
35
+ * @param z gravity component on z in g
36
+ * @param lastReliableRotation {@link Surface#ROTATION_0} etc., or -1 if unknown
37
+ * @param fallbackRotation display rotation when gravity is ambiguous
38
+ */
39
+ static Result resolve(float x, float y, float z, int lastReliableRotation, int fallbackRotation) {
40
+ float absX = Math.abs(x);
41
+ float absY = Math.abs(y);
42
+ float absZ = Math.abs(z);
43
+ float horizontalMax = Math.max(absX, absY);
44
+
45
+ boolean isNearFlat = absZ >= FLAT_GRAVITY_Z_THRESHOLD || horizontalMax < MIN_HORIZONTAL_GRAVITY;
46
+ if (isNearFlat) {
47
+ int rotation = lastReliableRotation != -1 ? lastReliableRotation : fallbackRotation;
48
+ return new Result(rotation, false);
49
+ }
50
+
51
+ if (Math.abs(absX - absY) < AXIS_DECISION_MARGIN) {
52
+ int rotation = lastReliableRotation != -1 ? lastReliableRotation : fallbackRotation;
53
+ return new Result(rotation, false);
54
+ }
55
+
56
+ int rotation;
57
+ if (absX > absY) {
58
+ if (x > 0) {
59
+ rotation = Surface.ROTATION_90;
60
+ } else {
61
+ rotation = Surface.ROTATION_270;
62
+ }
63
+ } else {
64
+ if (y > 0) {
65
+ rotation = Surface.ROTATION_0;
66
+ } else {
67
+ rotation = Surface.ROTATION_180;
68
+ }
69
+ }
70
+ return new Result(rotation, true);
71
+ }
72
+ }
@@ -0,0 +1,90 @@
1
+ package app.capgo.capacitor.camera.preview;
2
+
3
+ import static org.junit.Assert.assertEquals;
4
+ import static org.junit.Assert.assertFalse;
5
+ import static org.junit.Assert.assertTrue;
6
+
7
+ import android.view.Surface;
8
+ import org.junit.Test;
9
+ import org.junit.runner.RunWith;
10
+ import org.robolectric.RobolectricTestRunner;
11
+ import org.robolectric.annotation.Config;
12
+
13
+ @RunWith(RobolectricTestRunner.class)
14
+ @Config(manifest = Config.NONE, sdk = 28)
15
+ public class PhysicalOrientationResolverTest {
16
+
17
+ @Test
18
+ public void nearFlatUsesFallbackWhenNoLastReliable() {
19
+ PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(0.04f, -0.03f, -0.99f, -1, Surface.ROTATION_0);
20
+ assertEquals(Surface.ROTATION_0, result.rotation);
21
+ assertFalse(result.reliable);
22
+ }
23
+
24
+ @Test
25
+ public void nearFlatKeepsLastReliableRotation() {
26
+ PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(
27
+ 0.05f,
28
+ 0.02f,
29
+ 0.92f,
30
+ Surface.ROTATION_90,
31
+ Surface.ROTATION_0
32
+ );
33
+ assertEquals(Surface.ROTATION_90, result.rotation);
34
+ assertFalse(result.reliable);
35
+ }
36
+
37
+ @Test
38
+ public void clearPortrait() {
39
+ PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(0.08f, 0.95f, 0.15f, -1, Surface.ROTATION_0);
40
+ assertEquals(Surface.ROTATION_0, result.rotation);
41
+ assertTrue(result.reliable);
42
+ }
43
+
44
+ @Test
45
+ public void clearLandscapeLeft() {
46
+ PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(0.92f, 0.08f, 0.05f, -1, Surface.ROTATION_0);
47
+ assertEquals(Surface.ROTATION_90, result.rotation);
48
+ assertTrue(result.reliable);
49
+ }
50
+
51
+ @Test
52
+ public void clearLandscapeRight() {
53
+ PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(-0.9f, 0.1f, 0.05f, -1, Surface.ROTATION_0);
54
+ assertEquals(Surface.ROTATION_270, result.rotation);
55
+ assertTrue(result.reliable);
56
+ }
57
+
58
+ @Test
59
+ public void upsideDownPortrait() {
60
+ PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(0.05f, -0.94f, 0.1f, -1, Surface.ROTATION_0);
61
+ assertEquals(Surface.ROTATION_180, result.rotation);
62
+ assertTrue(result.reliable);
63
+ }
64
+
65
+ @Test
66
+ public void ambiguousAxisUsesLastReliable() {
67
+ PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(
68
+ 0.52f,
69
+ -0.48f,
70
+ 0.2f,
71
+ Surface.ROTATION_0,
72
+ Surface.ROTATION_90
73
+ );
74
+ assertEquals(Surface.ROTATION_0, result.rotation);
75
+ assertFalse(result.reliable);
76
+ }
77
+
78
+ @Test
79
+ public void weakHorizontalGravityTreatedAsFlat() {
80
+ PhysicalOrientationResolver.Result result = PhysicalOrientationResolver.resolve(
81
+ 0.12f,
82
+ -0.18f,
83
+ 0.55f,
84
+ Surface.ROTATION_270,
85
+ Surface.ROTATION_0
86
+ );
87
+ assertEquals(Surface.ROTATION_270, result.rotation);
88
+ assertFalse(result.reliable);
89
+ }
90
+ }
package/dist/docs.json CHANGED
@@ -1066,7 +1066,7 @@
1066
1066
  "text": "android, ios"
1067
1067
  }
1068
1068
  ],
1069
- "docs": "Gets the current preview size and position.",
1069
+ "docs": "Gets the current visible camera preview bounds in web-view coordinates.\n\nWith `aspectMode: 'contain'`, this is the letterboxed camera image rect inside the\npreview container (not the full container when bars are shown). With `aspectMode: 'cover'`,\nthis matches the preview container bounds on both platforms.",
1070
1070
  "complexTypes": [],
1071
1071
  "slug": "getpreviewsize"
1072
1072
  },
@@ -1088,7 +1088,7 @@
1088
1088
  },
1089
1089
  {
1090
1090
  "name": "returns",
1091
- "text": "A promise that resolves with the actual preview dimensions and position."
1091
+ "text": "A promise that resolves with the visible camera preview bounds (same contract as {@link CameraPreviewPlugin.getPreviewSize})."
1092
1092
  },
1093
1093
  {
1094
1094
  "name": "since",
@@ -2086,15 +2086,19 @@
2086
2086
  "name": "captureResolution",
2087
2087
  "tags": [
2088
2088
  {
2089
- "text": "android",
2089
+ "text": "android, ios",
2090
2090
  "name": "platform"
2091
2091
  },
2092
2092
  {
2093
2093
  "text": "8.12.0",
2094
2094
  "name": "since"
2095
+ },
2096
+ {
2097
+ "text": "8.13.0 (iOS)",
2098
+ "name": "since"
2095
2099
  }
2096
2100
  ],
2097
- "docs": "Sets the Android still-capture resolution for the `ImageCapture` use case at session bind time.\nPreview resolution is unchanged. The shared ViewPort crop from the preview still applies to captures.\n\nWhen omitted, `ImageCapture` uses the highest resolution supported by the active camera\n(same as versions before 8.11.10). Set this to cap capture size for faster shots on high-megapixel\nsensors, for example `{ width: 1920, height: 1080 }` or `{ width: 1600, height: 1200 }`.",
2101
+ "docs": "Sets the still-capture sensor resolution cap at session start. Preview resolution is unchanged.\nThe shared ViewPort crop from the preview still applies to captures.\n\nOn Android this binds the `ImageCapture` use case to the requested size. On iOS 16+ this sets\n`AVCapturePhotoSettings.maxPhotoDimensions` (and disables high-resolution photo capture when capped)\nso the sensor delivers fewer pixels instead of downscaling a full-sensor frame in software.\nOn iOS 15, `start()` rejects this option.\n\nWhen omitted, capture uses the highest resolution supported by the active camera\n(same as versions before 8.11.10 on Android, and the format default on iOS). Set this to cap\ncapture size for faster shots on high-megapixel sensors, for example `{ width: 1920, height: 1080 }`\nor `{ width: 1600, height: 1200 }`.",
2098
2102
  "complexTypes": [
2099
2103
  "CameraCaptureResolution"
2100
2104
  ],
@@ -314,15 +314,22 @@ export interface CameraPreviewOptions {
314
314
  */
315
315
  enableVideoMode?: boolean;
316
316
  /**
317
- * Sets the Android still-capture resolution for the `ImageCapture` use case at session bind time.
318
- * Preview resolution is unchanged. The shared ViewPort crop from the preview still applies to captures.
317
+ * Sets the still-capture sensor resolution cap at session start. Preview resolution is unchanged.
318
+ * The shared ViewPort crop from the preview still applies to captures.
319
319
  *
320
- * When omitted, `ImageCapture` uses the highest resolution supported by the active camera
321
- * (same as versions before 8.11.10). Set this to cap capture size for faster shots on high-megapixel
322
- * sensors, for example `{ width: 1920, height: 1080 }` or `{ width: 1600, height: 1200 }`.
320
+ * On Android this binds the `ImageCapture` use case to the requested size. On iOS 16+ this sets
321
+ * `AVCapturePhotoSettings.maxPhotoDimensions` (and disables high-resolution photo capture when capped)
322
+ * so the sensor delivers fewer pixels instead of downscaling a full-sensor frame in software.
323
+ * On iOS 15, `start()` rejects this option.
323
324
  *
324
- * @platform android
325
+ * When omitted, capture uses the highest resolution supported by the active camera
326
+ * (same as versions before 8.11.10 on Android, and the format default on iOS). Set this to cap
327
+ * capture size for faster shots on high-megapixel sensors, for example `{ width: 1920, height: 1080 }`
328
+ * or `{ width: 1600, height: 1200 }`.
329
+ *
330
+ * @platform android, ios
325
331
  * @since 8.12.0
332
+ * @since 8.13.0 (iOS)
326
333
  */
327
334
  captureResolution?: CameraCaptureResolution;
328
335
  /**
@@ -966,7 +973,12 @@ export interface CameraPreviewPlugin {
966
973
  deviceId: string;
967
974
  }>;
968
975
  /**
969
- * Gets the current preview size and position.
976
+ * Gets the current visible camera preview bounds in web-view coordinates.
977
+ *
978
+ * With `aspectMode: 'contain'`, this is the letterboxed camera image rect inside the
979
+ * preview container (not the full container when bars are shown). With `aspectMode: 'cover'`,
980
+ * this matches the preview container bounds on both platforms.
981
+ *
970
982
  * @returns {Promise<{x: number, y: number, width: number, height: number}>}
971
983
  * @since 7.5.0
972
984
  * @platform android, ios
@@ -980,7 +992,7 @@ export interface CameraPreviewPlugin {
980
992
  /**
981
993
  * Sets the preview size and position.
982
994
  * @param options The new position and dimensions.
983
- * @returns {Promise<{ width: number; height: number; x: number; y: number }>} A promise that resolves with the actual preview dimensions and position.
995
+ * @returns {Promise<{ width: number; height: number; x: number; y: number }>} A promise that resolves with the visible camera preview bounds (same contract as {@link CameraPreviewPlugin.getPreviewSize}).
984
996
  * @since 7.5.0
985
997
  * @platform android, ios
986
998
  */