@chipmobilesdk/rn-camera 0.2.0 → 0.2.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.
@@ -1,178 +1,177 @@
1
- /**
2
- * The live VisionCamera surface rendered inside `CameraPreview`.
3
- *
4
- * Split into its own module so `CameraPreview` can require it lazily: consumers
5
- * that only use the pure utilities or the test adapter never load the native
6
- * module, while the hooks this component needs (`useCameraDevice`,
7
- * `usePhotoOutput`) can still be called normally.
8
- *
9
- * It also owns capture. In VisionCamera v5 the photo output belongs to the
10
- * session the `<Camera>` view configures, so the output only exists while this
11
- * surface is mounted; it is published to the engine through `visionSurface`.
12
- */
13
-
14
- import React, { useEffect, useMemo, useState } from 'react';
15
- import { StyleSheet } from 'react-native';
16
- import {
17
- Camera,
18
- useCameraDevice,
19
- usePhotoOutput,
20
- type CameraPhotoOutput,
21
- } from 'react-native-vision-camera';
22
-
23
- import {
24
- known,
25
- unavailable,
26
- type CameraPosition,
27
- type CapturedImage,
28
- type CaptureRequest,
29
- type ResizeMode,
30
- } from '../config/types';
31
- import { DEFAULT_QUALITY } from '../core/captureController';
32
- import { CameraError, isCameraError } from '../diagnostics/errors';
33
- import { registerVisionSurface } from '../native/visionSurface';
34
-
35
- export interface VisionCameraFeedProps {
36
- readonly cameraPosition: CameraPosition;
37
- readonly resizeMode: ResizeMode;
38
- readonly torchEnabled: boolean;
39
- }
40
-
41
- const styles = StyleSheet.create({
42
- feed: { flex: 1 },
43
- });
44
-
45
- const MIME_TYPES: Record<string, string> = {
46
- jpeg: 'image/jpeg',
47
- heic: 'image/heic',
48
- dng: 'image/x-adobe-dng',
49
- tiff: 'image/tiff',
50
- };
51
-
52
- /** VisionCamera reports orientation as a compass value; the contract wants degrees. */
53
- const ORIENTATION_DEGREES: Record<string, number> = {
54
- up: 0,
55
- right: 90,
56
- down: 180,
57
- left: 270,
58
- };
59
-
60
- function toFileUri(path: string): string {
61
- return path.startsWith('file://') ? path : `file://${path}`;
62
- }
63
-
64
- function toFilePath(uri: string): string {
65
- return uri.startsWith('file://') ? uri.slice('file://'.length) : uri;
66
- }
67
-
68
- /** Native failures carry no code and may name a file path; never surface them raw. */
69
- function asCameraError(error: unknown, code: 'captureFailed' | 'storageFailure'): CameraError {
70
- return isCameraError(error) ? error : new CameraError(code);
71
- }
72
-
73
- async function capturePhoto(
74
- photoOutput: CameraPhotoOutput,
75
- request: CaptureRequest,
76
- cameraPosition: CameraPosition,
77
- ): Promise<CapturedImage> {
78
- let photo: Awaited<ReturnType<CameraPhotoOutput['capturePhoto']>>;
79
- try {
80
- photo = await photoOutput.capturePhoto({ flashMode: request.flashMode ?? 'off' }, {});
81
- } catch (error) {
82
- throw asCameraError(error, 'captureFailed');
83
- }
84
- try {
85
- // A destination makes the artifact app-owned, so it is written straight
86
- // there; otherwise it goes to a temporary file (FR-026a).
87
- let path: string;
88
- if (request.destination) {
89
- try {
90
- await photo.saveToFileAsync(toFilePath(request.destination));
91
- } catch {
92
- throw new CameraError(
93
- 'storageFailure',
94
- 'Could not write the capture to the requested destination',
95
- );
96
- }
97
- path = request.destination;
98
- } else {
99
- // VisionCamera's own temporary directory. The controller moves the file
100
- // into the artifact store, when it has one, so cleanup can reach it.
101
- try {
102
- path = await photo.saveToTemporaryFileAsync();
103
- } catch (error) {
104
- throw asCameraError(error, 'storageFailure');
105
- }
106
- }
107
-
108
- return {
109
- uri: toFileUri(path),
110
- mimeType: MIME_TYPES[photo.containerFormat] ?? 'application/octet-stream',
111
- width: photo.width,
112
- height: photo.height,
113
- timestamp: Date.now(),
114
- cameraPosition: request.cameraPosition ?? cameraPosition,
115
- // Ownership is decided by the controller from the original request;
116
- // this value is overwritten there and is only a safe default.
117
- ownership: request.destination ? 'appOwned' : 'packageTemporary',
118
- // Quality is fixed on the photo output, not per capture, and nothing here
119
- // resizes. Report what was actually applied rather than echo the hints.
120
- effectiveQuality: DEFAULT_QUALITY,
121
- effectiveMaxDimension: unavailable(),
122
- // The native layer reports neither back after a save, and guessing them
123
- // would be worse than saying so (FR-022).
124
- fileSize: unavailable(),
125
- orientation:
126
- photo.orientation in ORIENTATION_DEGREES
127
- ? known(ORIENTATION_DEGREES[photo.orientation])
128
- : unavailable(),
129
- };
130
- } finally {
131
- // Large native buffers must not wait for the JS GC.
132
- photo.dispose();
133
- }
134
- }
135
-
136
- export const VisionCameraFeed: React.FC<VisionCameraFeedProps> = ({
137
- cameraPosition,
138
- resizeMode,
139
- torchEnabled,
140
- }) => {
141
- const device = useCameraDevice(cameraPosition);
142
- const photoOutput = usePhotoOutput({ quality: DEFAULT_QUALITY });
143
-
144
- const surface = useMemo(
145
- () => ({
146
- capture: (request: CaptureRequest) =>
147
- capturePhoto(photoOutput, request, cameraPosition),
148
- }),
149
- [photoOutput, cameraPosition],
150
- );
151
-
152
- useEffect(() => registerVisionSurface(surface), [surface]);
153
-
154
- // VisionCamera applies `torchMode` on mount, before the camera is active, and
155
- // leaves the native "Camera is not active" rejection unhandled — one red
156
- // error per session start. Leave the prop unset until the torch is first
157
- // turned on; after that 'off' must be sent explicitly to turn it back off.
158
- const [torchTouched, setTorchTouched] = useState(false);
159
- if (torchEnabled && !torchTouched) {
160
- setTorchTouched(true);
161
- }
162
- const torchMode = torchEnabled ? 'on' : torchTouched ? 'off' : undefined;
163
-
164
- if (!device) {
165
- return null;
166
- }
167
-
168
- return (
169
- <Camera
170
- style={styles.feed}
171
- device={device}
172
- outputs={[photoOutput]}
173
- isActive={true}
174
- resizeMode={resizeMode}
175
- torchMode={torchMode}
176
- />
177
- );
178
- };
1
+ /**
2
+ * The live VisionCamera surface rendered inside `CameraPreview`.
3
+ *
4
+ * Split into its own module so `CameraPreview` can require it lazily: consumers
5
+ * that only use the pure utilities or the test adapter never load the native
6
+ * module, while the hooks this component needs (`useCameraDevice`,
7
+ * `usePhotoOutput`) can still be called normally.
8
+ *
9
+ * It also owns capture. In VisionCamera v5 the photo output belongs to the
10
+ * session the `<Camera>` view configures, so the output only exists while this
11
+ * surface is mounted; it is published to the engine through `visionSurface`.
12
+ */
13
+
14
+ import React, { useEffect, useMemo, useState } from 'react';
15
+ import { StyleSheet } from 'react-native';
16
+ import {
17
+ Camera,
18
+ useCameraDevice,
19
+ usePhotoOutput,
20
+ type CameraPhotoOutput,
21
+ } from 'react-native-vision-camera';
22
+
23
+ import {
24
+ known,
25
+ unavailable,
26
+ type CameraPosition,
27
+ type CapturedImage,
28
+ type CaptureRequest,
29
+ type ResizeMode,
30
+ } from '../config/types';
31
+ import { DEFAULT_QUALITY } from '../core/captureController';
32
+ import { CameraError, isCameraError } from '../diagnostics/errors';
33
+ import { registerVisionSurface } from '../native/visionSurface';
34
+
35
+ export interface VisionCameraFeedProps {
36
+ readonly cameraPosition: CameraPosition;
37
+ readonly resizeMode: ResizeMode;
38
+ readonly torchEnabled: boolean;
39
+ }
40
+
41
+ const styles = StyleSheet.create({
42
+ feed: { flex: 1 },
43
+ });
44
+
45
+ const MIME_TYPES: Record<string, string> = {
46
+ jpeg: 'image/jpeg',
47
+ heic: 'image/heic',
48
+ dng: 'image/x-adobe-dng',
49
+ tiff: 'image/tiff',
50
+ };
51
+
52
+ /** VisionCamera reports orientation as a compass value; the contract wants degrees. */
53
+ const ORIENTATION_DEGREES: Record<string, number> = {
54
+ up: 0,
55
+ right: 90,
56
+ down: 180,
57
+ left: 270,
58
+ };
59
+
60
+ function toFileUri(path: string): string {
61
+ return path.startsWith('file://') ? path : `file://${path}`;
62
+ }
63
+
64
+ function toFilePath(uri: string): string {
65
+ return uri.startsWith('file://') ? uri.slice('file://'.length) : uri;
66
+ }
67
+
68
+ /** Native failures carry no code and may name a file path; never surface them raw. */
69
+ function asCameraError(error: unknown, code: 'captureFailed' | 'storageFailure'): CameraError {
70
+ return isCameraError(error) ? error : new CameraError(code);
71
+ }
72
+
73
+ async function capturePhoto(
74
+ photoOutput: CameraPhotoOutput,
75
+ request: CaptureRequest,
76
+ cameraPosition: CameraPosition,
77
+ ): Promise<CapturedImage> {
78
+ let photo: Awaited<ReturnType<CameraPhotoOutput['capturePhoto']>>;
79
+ try {
80
+ photo = await photoOutput.capturePhoto({ flashMode: request.flashMode ?? 'off' }, {});
81
+ } catch (error) {
82
+ throw asCameraError(error, 'captureFailed');
83
+ }
84
+ try {
85
+ // A destination makes the artifact app-owned, so it is written straight
86
+ // there; otherwise it goes to a temporary file (FR-026a).
87
+ let path: string;
88
+ if (request.destination) {
89
+ try {
90
+ await photo.saveToFileAsync(toFilePath(request.destination));
91
+ } catch {
92
+ throw new CameraError(
93
+ 'storageFailure',
94
+ 'Could not write the capture to the requested destination',
95
+ );
96
+ }
97
+ path = request.destination;
98
+ } else {
99
+ // VisionCamera's own temporary directory. The controller moves the file
100
+ // into the artifact store, when it has one, so cleanup can reach it.
101
+ try {
102
+ path = await photo.saveToTemporaryFileAsync();
103
+ } catch (error) {
104
+ throw asCameraError(error, 'storageFailure');
105
+ }
106
+ }
107
+
108
+ return {
109
+ uri: toFileUri(path),
110
+ mimeType: MIME_TYPES[photo.containerFormat] ?? 'application/octet-stream',
111
+ width: photo.width,
112
+ height: photo.height,
113
+ timestamp: Date.now(),
114
+ cameraPosition: request.cameraPosition ?? cameraPosition,
115
+ // Ownership is decided by the controller from the original request;
116
+ // this value is overwritten there and is only a safe default.
117
+ ownership: request.destination ? 'appOwned' : 'packageTemporary',
118
+ // Quality is fixed on the photo output, not per capture, and nothing here
119
+ // resizes. Report what was actually applied rather than echo the hints.
120
+ effectiveQuality: DEFAULT_QUALITY,
121
+ effectiveMaxDimension: unavailable(),
122
+ // The native layer reports neither back after a save, and guessing them
123
+ // would be worse than saying so (FR-022).
124
+ fileSize: unavailable(),
125
+ orientation:
126
+ photo.orientation in ORIENTATION_DEGREES
127
+ ? known(ORIENTATION_DEGREES[photo.orientation])
128
+ : unavailable(),
129
+ };
130
+ } finally {
131
+ // Large native buffers must not wait for the JS GC.
132
+ photo.dispose();
133
+ }
134
+ }
135
+
136
+ export const VisionCameraFeed: React.FC<VisionCameraFeedProps> = ({
137
+ cameraPosition,
138
+ resizeMode,
139
+ torchEnabled,
140
+ }) => {
141
+ const device = useCameraDevice(cameraPosition);
142
+ const photoOutput = usePhotoOutput({ quality: DEFAULT_QUALITY });
143
+
144
+ const surface = useMemo(
145
+ () => ({
146
+ capture: (request: CaptureRequest) => capturePhoto(photoOutput, request, cameraPosition),
147
+ }),
148
+ [photoOutput, cameraPosition],
149
+ );
150
+
151
+ useEffect(() => registerVisionSurface(surface), [surface]);
152
+
153
+ // VisionCamera applies `torchMode` on mount, before the camera is active, and
154
+ // leaves the native "Camera is not active" rejection unhandled — one red
155
+ // error per session start. Leave the prop unset until the torch is first
156
+ // turned on; after that 'off' must be sent explicitly to turn it back off.
157
+ const [torchTouched, setTorchTouched] = useState(false);
158
+ if (torchEnabled && !torchTouched) {
159
+ setTorchTouched(true);
160
+ }
161
+ const torchMode = torchEnabled ? 'on' : torchTouched ? 'off' : undefined;
162
+
163
+ if (!device) {
164
+ return null;
165
+ }
166
+
167
+ return (
168
+ <Camera
169
+ style={styles.feed}
170
+ device={device}
171
+ outputs={[photoOutput]}
172
+ isActive={true}
173
+ resizeMode={resizeMode}
174
+ torchMode={torchMode}
175
+ />
176
+ );
177
+ };
@@ -1,58 +1,56 @@
1
- /**
2
- * React bindings for the camera controller.
3
- *
4
- * `useCameraController` owns the controller lifetime and disposes it on
5
- * unmount, which is what guarantees the camera is released when a screen goes
6
- * away (FR-016).
7
- */
8
-
9
- import { useEffect, useRef, useState, useSyncExternalStore } from 'react';
10
-
11
- import type { CameraState } from '../config/types';
12
- import {
13
- createCameraController,
14
- type CameraController,
15
- type CameraControllerOptions,
16
- } from '../core/controller';
17
-
18
- export function useCameraController(
19
- options: CameraControllerOptions = {},
20
- ): CameraController {
21
- // Options usually arrive as a fresh object literal each render; capturing
22
- // them in a ref keeps the controller stable for the component's lifetime.
23
- const optionsRef = useRef(options);
24
- // Construction has no side effects (the controller subscribes on first use),
25
- // so a controller React builds and discards here costs nothing.
26
- const [controller, setController] = useState(() => createCameraController(optionsRef.current));
27
- const disposedRef = useRef(new WeakSet<CameraController>());
28
-
29
- useEffect(() => {
30
- // StrictMode, and React's planned offscreen reuse, run the cleanup and then
31
- // this effect again on the same component. Disposal is final, so the old
32
- // controller cannot be revived; replace it. Before this, the screen kept a
33
- // dead controller after the remount and the camera never started.
34
- const disposed = disposedRef.current;
35
- if (disposed.has(controller)) {
36
- setController(createCameraController(optionsRef.current));
37
- return undefined;
38
- }
39
- return () => {
40
- disposed.add(controller);
41
- controller.dispose().catch(() => undefined);
42
- };
43
- }, [controller]);
44
-
45
- return controller;
46
- }
47
-
48
- export function useCameraState(controller: CameraController): CameraState {
49
- // `subscribe` is a property of the controller object, so it is stable for the
50
- // controller's lifetime, which is what useSyncExternalStore requires. The
51
- // store read is tear-free, unlike the previous useState copy, which could
52
- // miss a change made between render and effect.
53
- return useSyncExternalStore(
54
- controller.subscribe,
55
- () => controller.state,
56
- () => controller.state,
57
- );
58
- }
1
+ /**
2
+ * React bindings for the camera controller.
3
+ *
4
+ * `useCameraController` owns the controller lifetime and disposes it on
5
+ * unmount, which is what guarantees the camera is released when a screen goes
6
+ * away (FR-016).
7
+ */
8
+
9
+ import { useEffect, useRef, useState, useSyncExternalStore } from 'react';
10
+
11
+ import type { CameraState } from '../config/types';
12
+ import {
13
+ createCameraController,
14
+ type CameraController,
15
+ type CameraControllerOptions,
16
+ } from '../core/controller';
17
+
18
+ export function useCameraController(options: CameraControllerOptions = {}): CameraController {
19
+ // Options usually arrive as a fresh object literal each render; capturing
20
+ // them in a ref keeps the controller stable for the component's lifetime.
21
+ const optionsRef = useRef(options);
22
+ // Construction has no side effects (the controller subscribes on first use),
23
+ // so a controller React builds and discards here costs nothing.
24
+ const [controller, setController] = useState(() => createCameraController(optionsRef.current));
25
+ const disposedRef = useRef(new WeakSet<CameraController>());
26
+
27
+ useEffect(() => {
28
+ // StrictMode, and React's planned offscreen reuse, run the cleanup and then
29
+ // this effect again on the same component. Disposal is final, so the old
30
+ // controller cannot be revived; replace it. Before this, the screen kept a
31
+ // dead controller after the remount and the camera never started.
32
+ const disposed = disposedRef.current;
33
+ if (disposed.has(controller)) {
34
+ setController(createCameraController(optionsRef.current));
35
+ return undefined;
36
+ }
37
+ return () => {
38
+ disposed.add(controller);
39
+ controller.dispose().catch(() => undefined);
40
+ };
41
+ }, [controller]);
42
+
43
+ return controller;
44
+ }
45
+
46
+ export function useCameraState(controller: CameraController): CameraState {
47
+ // `subscribe` is a property of the controller object, so it is stable for the
48
+ // controller's lifetime, which is what useSyncExternalStore requires. The
49
+ // store read is tear-free, unlike the previous useState copy, which could
50
+ // miss a change made between render and effect.
51
+ return useSyncExternalStore(
52
+ controller.subscribe,
53
+ () => controller.state,
54
+ () => controller.state,
55
+ );
56
+ }