@monkvision/camera-web 4.0.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 (76) hide show
  1. package/LICENSE +32 -0
  2. package/README.md +182 -0
  3. package/lib/Camera/Camera.d.ts +51 -0
  4. package/lib/Camera/Camera.js +69 -0
  5. package/lib/Camera/Camera.js.map +1 -0
  6. package/lib/Camera/Camera.styles.d.ts +2 -0
  7. package/lib/Camera/Camera.styles.js +21 -0
  8. package/lib/Camera/Camera.styles.js.map +1 -0
  9. package/lib/Camera/CameraHUD.types.d.ts +62 -0
  10. package/lib/Camera/CameraHUD.types.js +3 -0
  11. package/lib/Camera/CameraHUD.types.js.map +1 -0
  12. package/lib/Camera/hooks/index.d.ts +7 -0
  13. package/lib/Camera/hooks/index.js +26 -0
  14. package/lib/Camera/hooks/index.js.map +1 -0
  15. package/lib/Camera/hooks/useCameraCanvas.d.ts +37 -0
  16. package/lib/Camera/hooks/useCameraCanvas.js +58 -0
  17. package/lib/Camera/hooks/useCameraCanvas.js.map +1 -0
  18. package/lib/Camera/hooks/useCameraPreview.d.ts +22 -0
  19. package/lib/Camera/hooks/useCameraPreview.js +57 -0
  20. package/lib/Camera/hooks/useCameraPreview.js.map +1 -0
  21. package/lib/Camera/hooks/useCameraScreenshot.d.ts +31 -0
  22. package/lib/Camera/hooks/useCameraScreenshot.js +66 -0
  23. package/lib/Camera/hooks/useCameraScreenshot.js.map +1 -0
  24. package/lib/Camera/hooks/useCompression.d.ts +24 -0
  25. package/lib/Camera/hooks/useCompression.js +126 -0
  26. package/lib/Camera/hooks/useCompression.js.map +1 -0
  27. package/lib/Camera/hooks/useTakePicture.d.ts +32 -0
  28. package/lib/Camera/hooks/useTakePicture.js +100 -0
  29. package/lib/Camera/hooks/useTakePicture.js.map +1 -0
  30. package/lib/Camera/hooks/useUserMedia.d.ts +122 -0
  31. package/lib/Camera/hooks/useUserMedia.js +294 -0
  32. package/lib/Camera/hooks/useUserMedia.js.map +1 -0
  33. package/lib/Camera/hooks/utils/analyzeCameraDevices.d.ts +17 -0
  34. package/lib/Camera/hooks/utils/analyzeCameraDevices.js +75 -0
  35. package/lib/Camera/hooks/utils/analyzeCameraDevices.js.map +1 -0
  36. package/lib/Camera/hooks/utils/getCanvasHandle.d.ts +18 -0
  37. package/lib/Camera/hooks/utils/getCanvasHandle.js +29 -0
  38. package/lib/Camera/hooks/utils/getCanvasHandle.js.map +1 -0
  39. package/lib/Camera/hooks/utils/getMediaContraints.d.ts +46 -0
  40. package/lib/Camera/hooks/utils/getMediaContraints.js +62 -0
  41. package/lib/Camera/hooks/utils/getMediaContraints.js.map +1 -0
  42. package/lib/Camera/hooks/utils/index.d.ts +3 -0
  43. package/lib/Camera/hooks/utils/index.js +20 -0
  44. package/lib/Camera/hooks/utils/index.js.map +1 -0
  45. package/lib/Camera/index.d.ts +4 -0
  46. package/lib/Camera/index.js +9 -0
  47. package/lib/Camera/index.js.map +1 -0
  48. package/lib/Camera/monitoring.d.ts +34 -0
  49. package/lib/Camera/monitoring.js +30 -0
  50. package/lib/Camera/monitoring.js.map +1 -0
  51. package/lib/SimpleCameraHUD/SimpleCameraHUD.d.ts +24 -0
  52. package/lib/SimpleCameraHUD/SimpleCameraHUD.js +37 -0
  53. package/lib/SimpleCameraHUD/SimpleCameraHUD.js.map +1 -0
  54. package/lib/SimpleCameraHUD/SimpleCameraHUD.styles.d.ts +2 -0
  55. package/lib/SimpleCameraHUD/SimpleCameraHUD.styles.js +62 -0
  56. package/lib/SimpleCameraHUD/SimpleCameraHUD.styles.js.map +1 -0
  57. package/lib/SimpleCameraHUD/index.d.ts +1 -0
  58. package/lib/SimpleCameraHUD/index.js +6 -0
  59. package/lib/SimpleCameraHUD/index.js.map +1 -0
  60. package/lib/i18n.d.ts +5 -0
  61. package/lib/i18n.js +24 -0
  62. package/lib/i18n.js.map +1 -0
  63. package/lib/index.d.ts +4 -0
  64. package/lib/index.js +21 -0
  65. package/lib/index.js.map +1 -0
  66. package/lib/translations/de.json +3 -0
  67. package/lib/translations/en.json +3 -0
  68. package/lib/translations/fr.json +3 -0
  69. package/lib/translations/nl.json +3 -0
  70. package/lib/utils/errors.utils.d.ts +7 -0
  71. package/lib/utils/errors.utils.js +42 -0
  72. package/lib/utils/errors.utils.js.map +1 -0
  73. package/lib/utils/index.d.ts +1 -0
  74. package/lib/utils/index.js +18 -0
  75. package/lib/utils/index.js.map +1 -0
  76. package/package.json +90 -0
package/LICENSE ADDED
@@ -0,0 +1,32 @@
1
+ The Clear BSD License
2
+
3
+ Copyright (c) [2022] [Monk](http://monk.ai)
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted (subject to the limitations in the disclaimer
8
+ below) provided that the following conditions are met:
9
+
10
+ * Redistributions of source code must retain the above copyright notice,
11
+ this list of conditions and the following disclaimer.
12
+
13
+ * Redistributions in binary form must reproduce the above copyright
14
+ notice, this list of conditions and the following disclaimer in the
15
+ documentation and/or other materials provided with the distribution.
16
+
17
+ * Neither the name of the copyright holder nor the names of its
18
+ contributors may be used to endorse or promote products derived from this
19
+ software without specific prior written permission.
20
+
21
+ NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
22
+ THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23
+ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25
+ PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
26
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29
+ BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
30
+ IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,182 @@
1
+ # @monkvision/camera-web
2
+ This package provides tools used to handle the Camera in MonkJs web projects in React. It exports a component called
3
+ Camera, that lets you display a camera preview on your app as well as an HUD on top of it, and provides methods
4
+ to take pictures, compress images, etc.
5
+
6
+ # Installing
7
+ To install the package, you can run the following command :
8
+
9
+ ```shell
10
+ yarn add @monkvision/camera-web
11
+ ```
12
+
13
+ If you are using TypeScript, this package comes with its type definitions integrated, so you don't need to install
14
+ anything else!
15
+
16
+ # Camera component
17
+ ## Camera Preview
18
+ The main component exported by this package is called `Camera`. When placed in a screen this component displays a
19
+ preview of the user's camera video stream. This component is a smart React wrapper for core JavaScript API
20
+ functionnalities that let you access the user camera :
21
+ - Automatic handling of Camera permissions : if you place this component in your app, the user will automatically be
22
+ asked permission for camera access
23
+ - Error handling and retry-on-error utilities
24
+ - React memoization of elements
25
+ - Render optimizations
26
+
27
+ ```tsx
28
+ // This super simple bit of code will let you display a camera preview in your app.
29
+ import { Camera } from '@monkvision/camera-web';
30
+
31
+ function MyCameraPreview() {
32
+ return <Camera />;
33
+ }
34
+ ```
35
+
36
+ ## Camera resolution
37
+ The resolution quality of the pictures taken by the Camera component is configurable by passing it as a prop to the
38
+ Camera component :
39
+
40
+ ```tsx
41
+ import { CameraResolution } from '@monkvision/types';
42
+ import { Camera } from '@monkvision/camera-web';
43
+
44
+ function MyCameraPreview() {
45
+ return <Camera resolution={CameraResolution.HD_720P} />;
46
+ }
47
+ ```
48
+
49
+ Notes :
50
+ - This option does not affect the resolution of the Camera preview : the preview will always use the highest
51
+ resolution available on the current device.
52
+ - If the specified resolution is not equal to the one used by the device's native camera, the pictures taken will be
53
+ scaled to fit the requirements.
54
+ - The resolutions available in the `CameraResolution` enum are all in the 16:9 format.
55
+ - If the aspect ratio of the specified resolution differs from the one of the device's camera, pictures taken will
56
+ always have the same aspect ratio as the native camera one, and will be scaled in a way to make sure that neither the
57
+ width, nor the height of the output picture will exceed the dimensions of the specified resolution.
58
+
59
+ ## Compression options
60
+ When pictures are taken by the camera, they are compressed and encoded. The compression format and quality can be
61
+ configured using props on the camera component in the following ways :
62
+
63
+ ```tsx
64
+ import { Camera } from '@monkvision/camera-web';
65
+ import { CompressionFormat } from '@monkvision/types';
66
+
67
+ function MyCameraPreview() {
68
+ return <Camera format={CompressionFormat.JPEG} quality={0.4} />;
69
+ }
70
+ ```
71
+
72
+ For more details on the compression options, see the *API* section below.
73
+
74
+ ## Camera HUD
75
+ In order to control the Camera (take pictures etc.), an HUD component can be passed as a prop to the Camera component.
76
+ An HUD component is a component that takes a camera handle (an object used to control the camera) as well as
77
+ a camera preview element as props and that will display the preview with some additional head-up display on top of it.
78
+ When a HUD component is passed to the Camera component, the Camera will place it in the tree, and will pass him down the
79
+ camera handle and preview already configured.
80
+
81
+ It is definitely possible to write your own custom Camera HUD component, but if you just need a very basic Camera HUD,
82
+ this package exports one already : the `SimpleCameraHUD` component. It will display a button to take the pictures, error
83
+ messages with proper labels in case there are errors when fetching the camera video stream, and a retry button to try
84
+ to fetch the camera stream again if there was an error.
85
+
86
+ To use this HUD, simply pass the component to the `HUDComponent` prop of the Camera component :
87
+
88
+ ```tsx
89
+ import { Camera, SimpleCameraHUD } from '@monkvision/camera-web';
90
+
91
+ function MyCameraPreviewWithHUD() {
92
+ return <Camera HUDComponent={SimpleCameraHUD} />;
93
+ }
94
+ ```
95
+
96
+ The text displayed by this component (error messages, retry button label...) is by default in english. If you want to
97
+ customize the display language, you can specify the fixed language you want to use by using the `lang` prop of the
98
+ component like this :
99
+
100
+ ```tsx
101
+ import { Camera, SimpleCameraHUD } from '@monkvision/camera-web';
102
+
103
+ function MyCameraPreviewWithGermanHUD() {
104
+ return <Camera HUDComponent={SimpleCameraHUD} hudProps={{ lang: 'fr' }} />;
105
+ }
106
+ ```
107
+
108
+ The currently supported languages are :
109
+ - English : `'en'` (default)
110
+ - French : `'fr'`
111
+ - German : `'de'`
112
+ - Dutch : `'nl'`
113
+
114
+ ## Creating a Custom HUD
115
+ In order to implement custom Camera HUD, you simply need to create a component that will take a camera handle and a
116
+ camera preview as props. Additional props can be passed to this HUD component using the `hudProps` prop of the Camera
117
+ component :
118
+
119
+ ```tsx
120
+ import { Camera, CameraHUDProps } from '@monkvision/camera-web';
121
+
122
+ interface MyCustomCameraHUD extends CameraHUDProps {
123
+ message: string;
124
+ }
125
+
126
+ function MyCustomCameraHUD({ handle, cameraPreview, message }: MyCustomCameraHUD) {
127
+ return (
128
+ <div>
129
+ {cameraPreview}
130
+ <button onClick={handle.takePicture}>Take Picture</button>
131
+ <div>{message}</div>
132
+ </div>
133
+ );
134
+ }
135
+
136
+ function MyCameraPreviewWithCustomHUD() {
137
+ return (
138
+ <Camera
139
+ HUDComponent={MyCustomCameraHUD}
140
+ hudProps={{ message: 'Hello World!' }}
141
+ />
142
+ );
143
+ }
144
+ ```
145
+
146
+ The complete specification of the camera handle is available in the *API* section below.
147
+
148
+ ## Camera Events
149
+ In order to add side effects when some things happen in the Camera, you can specify event handlers callbacks in the
150
+ Camera component props. For now the supported events available are :
151
+
152
+ - `onPictureTaken: (picture: MonkPicture) => void` : Callback called when the user takes a picture
153
+
154
+ # API
155
+ ## Camera component
156
+ ### Description
157
+ Main component exported by this package, displays a Camera preview and the given HUD on top of it.
158
+
159
+ ### Props
160
+ | Prop | Type | Description | Required | Default Value |
161
+ |----------------|--------------------------------|---------------------------------------------------------------------------------------------------------------------------------|----------|---------------------------|
162
+ | resolution | CameraResolution | Resolution of the pictures taken by the camera. This does not affect the resolution of the Camera preview. | | `CameraResolution.UHD_4K` |
163
+ | format | CompressionFormat | The compression format used to compress images taken by the camera. | | `CompressionFormat.JPEG` |
164
+ | quality | number | The image quality when using a compression format that supports lossy compression. From 0 (lowest quality) to 1 (best quality). | | `0.6` |
165
+ | HUDComponent | CameraHUDComponent<T> | The camera HUD component to display on top of the camera preview. | | |
166
+ | hudProps | T | Additional props passed down to the Camera HUD component. | | |
167
+ | onPictureTaken | (picture: MonkPicture) => void | Callback called when a picture has been taken by the user. | | |
168
+ | monitoring | CameraMonitoringConfig | Extra options that can be passed to configure how the monitoring is handled in the component. | | |
169
+
170
+ ## Camera Handle interface
171
+ ### Description
172
+ Object passed to Camera HUD components that is used to control the camera
173
+
174
+ ### Properties
175
+ | Prop | Type | Description |
176
+ |-------------------|-----------------------------|----------------------------------------------------------------------------------------------------------|
177
+ | takePicture | () => Promise<MonkPicture> | A function that you can call to ask the camera to take a picture. |
178
+ | error | UserMediaError &#124; null | The error details if there has been an error when fetching the camera stream. |
179
+ | isLoading | boolean | Boolean indicating if the camera preview is loading. |
180
+ | retry | () => void | A function to retry the camera stream fetching in case of error. |
181
+ | dimensions | PixelDimensions &#124; null | The Camera stream dimensions (`null` if there is no stream). |
182
+ | previewDimensions | PixelDimensions &#124; null | The effective video dimensions of the Camera stream on the client screen (`null` if there is no stream). |
@@ -0,0 +1,51 @@
1
+ import { AllOrNone, CameraConfig, RequiredKeys } from '@monkvision/types';
2
+ import { CameraMonitoringConfig } from './monitoring';
3
+ import { CameraEventHandlers, CameraHUDComponent } from './CameraHUD.types';
4
+ /**
5
+ * Type definition for the HUD component and its props passed to the Camera component. Monk uses this custom type in
6
+ * order to enforce typing on the following configurations :
7
+ *
8
+ * - If the HUD component does not have any required props, the Camera component will allow developers to pass it either
9
+ * the HUDComponent or both the component and its props.
10
+ * - If the HUD component does indeed have required props, the Camera component will only accept either BOTH the
11
+ * HUDComponent and its props, or none of them.
12
+ *
13
+ * This is done in order to ensure that developers do not pass HUD components that need specific props to be rendered to
14
+ * the Camera without actually passing those props as well.
15
+ */
16
+ export type HUDConfigProps<T extends object> = RequiredKeys<T> extends never ? {
17
+ /**
18
+ * HUD component to display above the camera preview.
19
+ *
20
+ * Note: If this component needs custom props to be rendered, don't forget to pass them to the Camera in the
21
+ * `hudProps` props.
22
+ */
23
+ HUDComponent?: CameraHUDComponent<T>;
24
+ /**
25
+ * Additional props passed to the HUD component when it will be rendered.
26
+ */
27
+ hudProps?: T;
28
+ } : AllOrNone<{
29
+ HUDComponent: CameraHUDComponent<T>;
30
+ hudProps: T;
31
+ }>;
32
+ /**
33
+ * Props given to the Camera component. The generic T type corresponds to the prop types of the HUD.
34
+ */
35
+ export type CameraProps<T extends object> = CameraConfig & CameraEventHandlers & HUDConfigProps<T> & {
36
+ /**
37
+ * Additional monitoring config that can be provided to the Camera component.
38
+ */
39
+ monitoring?: CameraMonitoringConfig;
40
+ };
41
+ /**
42
+ * Component used in MonkJs project used to :
43
+ * - Display a camera preview on the screen
44
+ * - Display a given camera HUD (buttons) on top of the camera preview
45
+ * - Provide tools to control the camera (take picture, compress images etc...)
46
+ *
47
+ * Please refer to the official documentation of the @monkvision/camera-web package (available
48
+ * [here](https://github.com/monkvision/monkjs/blob/main/packages/camera-web/README.md)) for more details on how this
49
+ * component works.
50
+ */
51
+ export declare function Camera<T extends object>({ resolution, format, quality, allowImageUpscaling, HUDComponent, hudProps, monitoring, onPictureTaken, }: CameraProps<T>): JSX.Element;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.Camera = void 0;
15
+ var jsx_runtime_1 = require("react/jsx-runtime");
16
+ var react_1 = require("react");
17
+ var types_1 = require("@monkvision/types");
18
+ var common_1 = require("@monkvision/common");
19
+ var hooks_1 = require("./hooks");
20
+ var Camera_styles_1 = require("./Camera.styles");
21
+ /**
22
+ * Component used in MonkJs project used to :
23
+ * - Display a camera preview on the screen
24
+ * - Display a given camera HUD (buttons) on top of the camera preview
25
+ * - Provide tools to control the camera (take picture, compress images etc...)
26
+ *
27
+ * Please refer to the official documentation of the @monkvision/camera-web package (available
28
+ * [here](https://github.com/monkvision/monkjs/blob/main/packages/camera-web/README.md)) for more details on how this
29
+ * component works.
30
+ */
31
+ function Camera(_a) {
32
+ var _b = _a.resolution, resolution = _b === void 0 ? types_1.CameraResolution.UHD_4K : _b, _c = _a.format, format = _c === void 0 ? types_1.CompressionFormat.JPEG : _c, _d = _a.quality, quality = _d === void 0 ? 0.6 : _d, _e = _a.allowImageUpscaling, allowImageUpscaling = _e === void 0 ? false : _e, HUDComponent = _a.HUDComponent, hudProps = _a.hudProps, monitoring = _a.monitoring, onPictureTaken = _a.onPictureTaken;
33
+ var previewResolution = (0, react_1.useMemo)(function () { return ((0, common_1.isMobileDevice)() ? types_1.CameraResolution.UHD_4K : types_1.CameraResolution.FHD_1080P); }, []);
34
+ var _f = (0, hooks_1.useCameraPreview)({
35
+ resolution: previewResolution,
36
+ facingMode: hooks_1.CameraFacingMode.ENVIRONMENT,
37
+ }), videoRef = _f.ref, streamDimensions = _f.dimensions, previewDimensions = _f.previewDimensions, error = _f.error, retry = _f.retry, isPreviewLoading = _f.isLoading, availableCameraDevices = _f.availableCameraDevices, selectedCameraDeviceId = _f.selectedCameraDeviceId;
38
+ var _g = (0, hooks_1.useCameraCanvas)({
39
+ resolution: resolution,
40
+ streamDimensions: streamDimensions,
41
+ allowImageUpscaling: allowImageUpscaling,
42
+ }), canvasRef = _g.ref, canvasDimensions = _g.dimensions;
43
+ var takeScreenshot = (0, hooks_1.useCameraScreenshot)({
44
+ videoRef: videoRef,
45
+ canvasRef: canvasRef,
46
+ dimensions: canvasDimensions,
47
+ });
48
+ var compress = (0, hooks_1.useCompression)({ canvasRef: canvasRef, options: { format: format, quality: quality } });
49
+ var _h = (0, hooks_1.useTakePicture)({
50
+ compress: compress,
51
+ takeScreenshot: takeScreenshot,
52
+ onPictureTaken: onPictureTaken,
53
+ monitoring: monitoring,
54
+ availableCameraDevices: availableCameraDevices,
55
+ selectedCameraDeviceId: selectedCameraDeviceId,
56
+ }), takePicture = _h.takePicture, isTakePictureLoading = _h.isLoading;
57
+ var isLoading = isPreviewLoading || isTakePictureLoading;
58
+ var cameraPreview = (0, react_1.useMemo)(function () { return ((0, jsx_runtime_1.jsxs)("div", __assign({ style: Camera_styles_1.styles['container'] }, { children: [(0, jsx_runtime_1.jsx)("video", { style: Camera_styles_1.styles['cameraPreview'], ref: videoRef, autoPlay: true, playsInline: true, controls: false, muted: true, "data-testid": 'camera-video-preview' }), (0, jsx_runtime_1.jsx)("canvas", { ref: canvasRef, style: Camera_styles_1.styles['cameraCanvas'], "data-testid": 'camera-canvas' })] }))); }, []);
59
+ return HUDComponent ? ((0, jsx_runtime_1.jsx)(HUDComponent, __assign({ handle: {
60
+ takePicture: takePicture,
61
+ error: error,
62
+ retry: retry,
63
+ isLoading: isLoading,
64
+ dimensions: streamDimensions,
65
+ previewDimensions: previewDimensions,
66
+ }, cameraPreview: cameraPreview }, (hudProps !== null && hudProps !== void 0 ? hudProps : {})))) : ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: cameraPreview }));
67
+ }
68
+ exports.Camera = Camera;
69
+ //# sourceMappingURL=Camera.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Camera.js","sourceRoot":"","sources":["../../src/Camera/Camera.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+BAAuC;AACvC,2CAM2B;AAC3B,6CAAoD;AACpD,iCAOiB;AAEjB,iDAAyC;AA2CzC;;;;;;;;;GASG;AACH,SAAgB,MAAM,CAAmB,EASxB;QARf,kBAAoC,EAApC,UAAU,mBAAG,wBAAgB,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,cAAc,gBAAA;QACd,UAAU,YAAA;QACV,sBAAsB,wBAAA;QACtB,sBAAsB,wBAAA;KACvB,CAAC,EAPM,WAAW,iBAAA,EAAa,oBAAoB,eAOlD,CAAC;IACH,IAAM,SAAS,GAAG,gBAAgB,IAAI,oBAAoB,CAAC;IAC3D,IAAM,aAAa,GAAG,IAAA,eAAO,EAC3B,cAAM,OAAA,CACJ,0CAAK,KAAK,EAAE,sBAAM,CAAC,WAAW,CAAC,iBAC7B,kCACE,KAAK,EAAE,sBAAM,CAAC,eAAe,CAAC,EAC9B,GAAG,EAAE,QAAQ,EACb,QAAQ,QACR,WAAW,EAAE,IAAI,EACjB,QAAQ,EAAE,KAAK,EACf,KAAK,EAAE,IAAI,iBACC,sBAAsB,GAClC,EACF,mCAAQ,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,sBAAM,CAAC,cAAc,CAAC,iBAAc,eAAe,GAAG,KACjF,CACP,EAbK,CAaL,EACD,EAAE,CACH,CAAC;IAEF,OAAO,YAAY,CAAC,CAAC,CAAC,CACpB,uBAAC,YAAY,aACX,MAAM,EAAE;YACN,WAAW,aAAA;YACX,KAAK,OAAA;YACL,KAAK,OAAA;YACL,SAAS,WAAA;YACT,UAAU,EAAE,gBAAgB;YAC5B,iBAAiB,mBAAA;SAClB,EACD,aAAa,EAAE,aAAa,IACvB,CAAC,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,EAAE,CAAO,EAC3B,CACH,CAAC,CAAC,CAAC,CACF,2DAAG,aAAa,GAAI,CACrB,CAAC;AACJ,CAAC;AAjFD,wBAiFC"}
@@ -0,0 +1,2 @@
1
+ import { Styles } from '@monkvision/types';
2
+ export declare const styles: Styles;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.styles = void 0;
4
+ exports.styles = {
5
+ container: {
6
+ width: '100%',
7
+ height: '100%',
8
+ display: 'flex',
9
+ alignItems: 'center',
10
+ justifyContent: 'center',
11
+ backgroundColor: '#000000',
12
+ },
13
+ cameraPreview: {
14
+ width: '100%',
15
+ height: '100%',
16
+ },
17
+ cameraCanvas: {
18
+ display: 'none',
19
+ },
20
+ };
21
+ //# sourceMappingURL=Camera.styles.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Camera.styles.js","sourceRoot":"","sources":["../../src/Camera/Camera.styles.ts"],"names":[],"mappings":";;;AAEa,QAAA,MAAM,GAAW;IAC5B,SAAS,EAAE;QACT,KAAK,EAAE,MAAM;QACb,MAAM,EAAE,MAAM;QACd,OAAO,EAAE,MAAM;QACf,UAAU,EAAE,QAAQ;QACpB,cAAc,EAAE,QAAQ;QACxB,eAAe,EAAE,SAAS;KAC3B;IACD,aAAa,EAAE;QACb,KAAK,EAAE,MAAM;QACb,MAAM,EAAE,MAAM;KACf;IACD,YAAY,EAAE;QACZ,OAAO,EAAE,MAAM;KAChB;CACF,CAAC"}
@@ -0,0 +1,62 @@
1
+ import { ComponentType, ReactElement } from 'react';
2
+ import { PixelDimensions, MonkPicture } from '@monkvision/types';
3
+ import { UserMediaError } from './hooks';
4
+ /**
5
+ * A set of properties used to handle a Camera preview.
6
+ */
7
+ export interface CameraHandle {
8
+ /**
9
+ * A function that you can call to ask the camera to take a picture.
10
+ */
11
+ takePicture: () => Promise<MonkPicture>;
12
+ /**
13
+ * The error details if there has been an error when fetching the camera stream.
14
+ */
15
+ error: UserMediaError | null;
16
+ /**
17
+ * Boolean indicating if the camera preview is loading.
18
+ */
19
+ isLoading: boolean;
20
+ /**
21
+ * A function to retry the camera stream fetching in case of error.
22
+ */
23
+ retry: () => void;
24
+ /**
25
+ * The dimensions of the resulting camera stream. Note that these dimensions can differ from the ones given in the
26
+ * stream constraints if they are not supported or available on the current device.
27
+ */
28
+ dimensions: PixelDimensions | null;
29
+ /**
30
+ * The effective pixel dimensions of the video stream on the client screen.
31
+ */
32
+ previewDimensions: PixelDimensions | null;
33
+ }
34
+ /**
35
+ * Interface describing the different event handlers for events dispatched by the Camera component.
36
+ */
37
+ export interface CameraEventHandlers {
38
+ /**
39
+ * Callback called when a picture has been taken by the Camera.
40
+ *
41
+ * @param picture The picture that has been taken.
42
+ */
43
+ onPictureTaken?: (picture: MonkPicture) => void;
44
+ }
45
+ /**
46
+ * Props accepted by a CameraHUD component.
47
+ */
48
+ export interface CameraHUDProps {
49
+ /**
50
+ * The camera preview element. The HUD component is expected to take this element as a prop and display it however it
51
+ * wants to.
52
+ */
53
+ cameraPreview: ReactElement;
54
+ /**
55
+ * The handle used to control the camera.
56
+ */
57
+ handle: CameraHandle;
58
+ }
59
+ /**
60
+ * Component type definition for a Camera HUD component.
61
+ */
62
+ export type CameraHUDComponent<T extends object = Record<never, never>> = ComponentType<CameraHUDProps & T>;
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=CameraHUD.types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CameraHUD.types.js","sourceRoot":"","sources":["../../src/Camera/CameraHUD.types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ export * from './useCameraPreview';
2
+ export * from './useCameraScreenshot';
3
+ export * from './useUserMedia';
4
+ export * from './useCameraCanvas';
5
+ export * from './useCompression';
6
+ export * from './useTakePicture';
7
+ export { type CanvasHandle, CameraFacingMode, type CameraConfig } from './utils';
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.CameraFacingMode = void 0;
18
+ __exportStar(require("./useCameraPreview"), exports);
19
+ __exportStar(require("./useCameraScreenshot"), exports);
20
+ __exportStar(require("./useUserMedia"), exports);
21
+ __exportStar(require("./useCameraCanvas"), exports);
22
+ __exportStar(require("./useCompression"), exports);
23
+ __exportStar(require("./useTakePicture"), exports);
24
+ var utils_1 = require("./utils");
25
+ Object.defineProperty(exports, "CameraFacingMode", { enumerable: true, get: function () { return utils_1.CameraFacingMode; } });
26
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/Camera/hooks/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,qDAAmC;AACnC,wDAAsC;AACtC,iDAA+B;AAC/B,oDAAkC;AAClC,mDAAiC;AACjC,mDAAiC;AACjC,iCAAiF;AAArD,yGAAA,gBAAgB,OAAA"}
@@ -0,0 +1,37 @@
1
+ import { RefObject } from 'react';
2
+ import { CameraResolution, PixelDimensions } from '@monkvision/types';
3
+ /**
4
+ * Object used to configure the camera canvas.
5
+ */
6
+ export interface CameraCanvasConfig {
7
+ /**
8
+ * The resolution of the pictures taken asked by the user of the Camera component.
9
+ */
10
+ resolution: CameraResolution;
11
+ /**
12
+ * Boolean indicating if the Camera component should allow image upscaling when the asked resolution is bigger than
13
+ * the one of the device Camera.
14
+ */
15
+ allowImageUpscaling: boolean;
16
+ /**
17
+ * The dimensions of the video stream.
18
+ */
19
+ streamDimensions: PixelDimensions | null;
20
+ }
21
+ /**
22
+ * Handle used to manage the camera canvas.
23
+ */
24
+ export interface CameraCanvasHandle {
25
+ /**
26
+ * The ref to the canvas element. Forward this ref to the <canvas> tag to set it up.
27
+ */
28
+ ref: RefObject<HTMLCanvasElement>;
29
+ /**
30
+ * The dimensions of the canvas.
31
+ */
32
+ dimensions: PixelDimensions | null;
33
+ }
34
+ /**
35
+ * Custom hook used to manage the camera <canvas> element used to take video screenshots and encode images.
36
+ */
37
+ export declare function useCameraCanvas({ resolution, streamDimensions, allowImageUpscaling, }: CameraCanvasConfig): CameraCanvasHandle;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useCameraCanvas = void 0;
4
+ var react_1 = require("react");
5
+ var utils_1 = require("./utils");
6
+ /**
7
+ * This function is used to calculate the dimensions of the canvas that will be used to draw the image, thus also
8
+ * calculating the output dimensions of the image itself, respecting the following logic :
9
+ * - If the aspect ratio of the stream and constraints are the same, we simply scale the stream to make it fit the
10
+ * constraints. Note that if `allowImageUpscaling` is `false`, and the stream is smaller than the constraints, we don't
11
+ * scale "up" the stream image, and we simply return the stream dimensions.
12
+ * - If the aspect ratio of the stream is different from the one specified in the constraints, the logic is the same,
13
+ * but the output aspect ratio will be the same one as the stream. The stream dimensions will simply be scaled
14
+ * following the same logic as the previous point, making sure that neither the width nor the height of the canvas will
15
+ * exceed the ones described by the constraints.
16
+ */
17
+ function getCanvasDimensions(_a) {
18
+ var resolution = _a.resolution, streamDimensions = _a.streamDimensions, allowImageUpscaling = _a.allowImageUpscaling;
19
+ if (!streamDimensions) {
20
+ return null;
21
+ }
22
+ var isPortrait = streamDimensions.width < streamDimensions.height;
23
+ var constraintsDimensions = (0, utils_1.getResolutionDimensions)(resolution, isPortrait);
24
+ var streamRatio = streamDimensions.width / streamDimensions.height;
25
+ if (constraintsDimensions.width > streamDimensions.width &&
26
+ constraintsDimensions.height > streamDimensions.height &&
27
+ !allowImageUpscaling) {
28
+ return {
29
+ width: streamDimensions.width,
30
+ height: streamDimensions.height,
31
+ };
32
+ }
33
+ var fitToHeight = constraintsDimensions.width / streamRatio > constraintsDimensions.height;
34
+ return {
35
+ width: fitToHeight ? constraintsDimensions.height * streamRatio : constraintsDimensions.width,
36
+ height: fitToHeight ? constraintsDimensions.height : constraintsDimensions.width / streamRatio,
37
+ };
38
+ }
39
+ /**
40
+ * Custom hook used to manage the camera <canvas> element used to take video screenshots and encode images.
41
+ */
42
+ function useCameraCanvas(_a) {
43
+ var resolution = _a.resolution, streamDimensions = _a.streamDimensions, allowImageUpscaling = _a.allowImageUpscaling;
44
+ var ref = (0, react_1.useRef)(null);
45
+ var handle = (0, react_1.useMemo)(function () { return ({
46
+ ref: ref,
47
+ dimensions: getCanvasDimensions({ resolution: resolution, streamDimensions: streamDimensions, allowImageUpscaling: allowImageUpscaling }),
48
+ }); }, [resolution, streamDimensions]);
49
+ (0, react_1.useEffect)(function () {
50
+ if (handle.dimensions && ref.current) {
51
+ ref.current.width = handle.dimensions.width;
52
+ ref.current.height = handle.dimensions.height;
53
+ }
54
+ }, [handle.dimensions]);
55
+ return handle;
56
+ }
57
+ exports.useCameraCanvas = useCameraCanvas;
58
+ //# sourceMappingURL=useCameraCanvas.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useCameraCanvas.js","sourceRoot":"","sources":["../../../src/Camera/hooks/useCameraCanvas.ts"],"names":[],"mappings":";;;AAAA,+BAA8D;AAE9D,iCAAkD;AAmClD;;;;;;;;;;GAUG;AACH,SAAS,mBAAmB,CAAC,EAIR;QAHnB,UAAU,gBAAA,EACV,gBAAgB,sBAAA,EAChB,mBAAmB,yBAAA;IAEnB,IAAI,CAAC,gBAAgB,EAAE;QACrB,OAAO,IAAI,CAAC;KACb;IACD,IAAM,UAAU,GAAG,gBAAgB,CAAC,KAAK,GAAG,gBAAgB,CAAC,MAAM,CAAC;IACpE,IAAM,qBAAqB,GAAG,IAAA,+BAAuB,EAAC,UAAU,EAAE,UAAU,CAAC,CAAC;IAC9E,IAAM,WAAW,GAAG,gBAAgB,CAAC,KAAK,GAAG,gBAAgB,CAAC,MAAM,CAAC;IAErE,IACE,qBAAqB,CAAC,KAAK,GAAG,gBAAgB,CAAC,KAAK;QACpD,qBAAqB,CAAC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,CAAC,CAAC,CAAC;IAExB,OAAO,MAAM,CAAC;AAChB,CAAC;AAtBD,0CAsBC"}
@@ -0,0 +1,22 @@
1
+ import { RefObject } from 'react';
2
+ import { PixelDimensions } from '@monkvision/types';
3
+ import { CameraConfig } from './utils';
4
+ import { UserMediaResult } from './useUserMedia';
5
+ /**
6
+ * An object containing properties used to handle the camera preview.
7
+ */
8
+ export interface CameraPreviewHandle extends UserMediaResult {
9
+ /**
10
+ * The effective pixel dimensions of the video stream on the client screen.
11
+ */
12
+ previewDimensions: PixelDimensions | null;
13
+ /**
14
+ * React MutableRefObject referencing the video element displaying the camera preview.
15
+ */
16
+ ref: RefObject<HTMLVideoElement>;
17
+ }
18
+ /**
19
+ * Custom hook used to initialize and handle the camera preview. It initializes the camera stream based on the given
20
+ * configuration, and provides a handle to manage the camera such as the ref to the video element etc.
21
+ */
22
+ export declare function useCameraPreview(config: CameraConfig): CameraPreviewHandle;