You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|[visioncamera.hooks.harness.tsx](visioncamera.hooks.harness.tsx)| React hook reactivity for `useCameraDevice(...)` position and physical-device filter changes |
31
+
|[visioncamera.utils.harness.ts](visioncamera.utils.harness.ts)| Pure public utilities such as `getUIRotation(...)` across every output/interface orientation pair |
A Camera has a fixed sensor orientation in which Frames are streamed in.
9
9
If you rotate your phone, the Camera doesn't physically rotate alongside with it, so rotation has to be applied to the Frames dynamically - and each [`CameraOutput`](/api/react-native-vision-camera/hybrid-objects/CameraOutput) applies orientation differently.
10
10
11
-
### Automatically set Orientation
11
+
### Set Orientation
12
+
13
+
#### Automatically set Orientation
12
14
13
15
In most cases, your [`Orientation`](/api/react-native-vision-camera/type-aliases/CameraOrientation) should be automatically set to either the App's Interface Orientation, or your Phone's Device Orientation:
14
16
15
-
- App Interface Orientation ([`'interface'`](/api/react-native-vision-camera/type-aliases/OrientationSource)): Changes output orientation only when the UI rotates. If the UI is locked to `portrait` and the phone is held sideways, the output orientation will still stick to `portrait` ([`'up'`](/api/react-native-vision-camera/type-aliases/CameraOrientation)). This is how apps like Snapchat or Instagram work.
17
+
- App Interface Orientation ([`'interface'`](/api/react-native-vision-camera/type-aliases/OrientationSource)): Changes output orientation only when the UI rotates. If the UI is locked to `portrait` and the phone is held sideways, the output orientation will still stick to `portrait` ([`'up'`](/api/react-native-vision-camera/type-aliases/CameraOrientation)). This is how apps like Snapchat or Instagram work - they are effectively locked to `portrait`.
16
18
- Phone Device Orientation ([`'device'`](/api/react-native-vision-camera/type-aliases/OrientationSource)): Changes output orientation when the phone physically rotates. If the UI is locked to `portrait` and the phone is held sideways, the output orientation will change to be sideways - even if the UI doesn't rotate to `landscape`. This is how most photography apps (including the stock iOS Camera app) work.
17
19
18
20
VisionCamera exposes two [`OrientationManager`](/api/react-native-vision-camera/hybrid-objects/OrientationManager)s - one for monitoring [`'interface'`](/api/react-native-vision-camera/type-aliases/OrientationSource) orientation, and one for monitoring [`'device'`](/api/react-native-vision-camera/type-aliases/OrientationSource) orientation:
For full manual control over orientation, set [`orientationSource`](/api/react-native-vision-camera/interfaces/CameraProps#orientationsource) to [`'custom'`](/api/react-native-vision-camera/type-aliases/OrientationSource) (if you are using `<Camera />` or `useCamera(...)`), and set a custom [`CameraOutput.outputOrientation`](/api/react-native-vision-camera/hybrid-objects/CameraOutput#outputorientation) for your outputs.
67
69
70
+
#### Rotate UI Elements based on Camera Orientation
71
+
72
+
If your Camera's [`orientationSource`](/api/react-native-vision-camera/interfaces/CameraProps#orientationsource) is set to [`'device'`](/api/react-native-vision-camera/type-aliases/OrientationSource) (or [`'custom'`](/api/react-native-vision-camera/type-aliases/OrientationSource)), your UI will not rotate alongside with the Camera pipeline.
73
+
To then visually rotate individual Camera controls (such as the Flip Camera button, a Flash button, or other controls), listen to the [`onUIRotationChanged`](/api/react-native-vision-camera/interfaces/CameraProps#onuirotationchanged) callback and rotate accordingly:
> Use [react-native-reanimated](https://docs.swmansion.com/react-native-reanimated/) for more control over animations.
113
+
68
114
### How Orientation is handled
69
115
70
116
Since Camera sensors have fixed orientations, rotation has to be applied to Frames dynamically. The Camera pipeline does not physically rotate buffers, as this is computationally expensive and would introduce latency.
* Called when the Camera Output orientation (driven
93
+
* by {@linkcode orientationSource}) or the interface
94
+
* orientation changes with a {@linkcode rotation} value
95
+
* that specifies the degrees needed to rotate UI elements
96
+
* such as Camera controls (flash button, Camera flip button)
97
+
* so they appear upright.
98
+
* @param rotation The degrees that UI elements need to be rotated by to appear up-right.
99
+
*/
100
+
onUIRotationChanged?: (rotation: number)=>void
89
101
/**
90
102
* Sets whether the {@linkcode CameraOutput}s are mirrored along
91
103
* the vertical axis. {@linkcode MirrorMode | 'auto'} mirrors
@@ -252,6 +264,7 @@ export function useCamera({
252
264
onInterruptionStarted,
253
265
onInterruptionEnded,
254
266
onSubjectAreaChanged,
267
+
onUIRotationChanged,
255
268
enableDistortionCorrection,
256
269
enableLowLightBoost,
257
270
enableSmoothAutoFocus,
@@ -265,6 +278,12 @@ export function useCamera({
265
278
onError: onError,
266
279
})
267
280
281
+
// TODO: Refactor our orientation logic here because it is problematic for multiple reasons;
282
+
// 1. Avoid going through re-renders/React state to change orientation (2x useOrientation(..)) (slow)
283
+
// 2. Avoid going through multiple setter calls here in a useEffect to set output orientation (possible race condition)
284
+
// 3. Avoid having a static useOrientation(...) hook - instead, have a UI element (`<NativePreviewView />`) fire interface orientation listeners (multi-display support)
285
+
// 4. orientationSource="custom" currently resorts back to 'up', which is not true - not sure if we just skip the callback or ignore instead?
286
+
// Instead, have orientation source be native/declarative so we can use `AVCaptureDevice.RotationCoordinator` and drive orientation from a preview without re-renders.
0 commit comments