@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
@@ -0,0 +1,57 @@
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.useCameraPreview = void 0;
15
+ var monitoring_1 = require("@monkvision/monitoring");
16
+ var react_1 = require("react");
17
+ var common_1 = require("@monkvision/common");
18
+ var utils_1 = require("./utils");
19
+ var useUserMedia_1 = require("./useUserMedia");
20
+ /**
21
+ * Custom hook used to initialize and handle the camera preview. It initializes the camera stream based on the given
22
+ * configuration, and provides a handle to manage the camera such as the ref to the video element etc.
23
+ */
24
+ function useCameraPreview(config) {
25
+ var ref = (0, react_1.useRef)(null);
26
+ var windowDimensions = (0, common_1.useWindowDimensions)();
27
+ var handleError = (0, monitoring_1.useMonitoring)().handleError;
28
+ var userMediaResult = (0, useUserMedia_1.useUserMedia)((0, utils_1.getMediaConstraints)(config), ref);
29
+ var previewDimensions = (0, react_1.useMemo)(function () {
30
+ if (!windowDimensions || !userMediaResult.dimensions) {
31
+ return null;
32
+ }
33
+ var windowAspectRatio = windowDimensions.width / windowDimensions.height;
34
+ var streamAspectRatio = userMediaResult.dimensions.width / userMediaResult.dimensions.height;
35
+ return windowAspectRatio >= streamAspectRatio
36
+ ? {
37
+ width: windowDimensions.height * streamAspectRatio,
38
+ height: windowDimensions.height,
39
+ }
40
+ : {
41
+ width: windowDimensions.width,
42
+ height: windowDimensions.width / streamAspectRatio,
43
+ };
44
+ }, [windowDimensions, userMediaResult.dimensions]);
45
+ (0, react_1.useEffect)(function () {
46
+ if (userMediaResult.stream && ref.current) {
47
+ ref.current.srcObject = userMediaResult.stream;
48
+ ref.current.onloadedmetadata = function () {
49
+ var _a;
50
+ (_a = ref.current) === null || _a === void 0 ? void 0 : _a.play().catch(handleError);
51
+ };
52
+ }
53
+ }, [userMediaResult.stream]);
54
+ return (0, react_1.useMemo)(function () { return (__assign({ ref: ref, previewDimensions: previewDimensions }, userMediaResult)); }, [previewDimensions, userMediaResult]);
55
+ }
56
+ exports.useCameraPreview = useCameraPreview;
57
+ //# sourceMappingURL=useCameraPreview.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useCameraPreview.js","sourceRoot":"","sources":["../../../src/Camera/hooks/useCameraPreview.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,qDAAuD;AACvD,+BAA8D;AAE9D,6CAAyD;AACzD,iCAA4D;AAC5D,+CAA+D;AAgB/D;;;GAGG;AACH,SAAgB,gBAAgB,CAAC,MAAoB;IACnD,IAAM,GAAG,GAAG,IAAA,cAAM,EAAmB,IAAI,CAAC,CAAC;IAC3C,IAAM,gBAAgB,GAAG,IAAA,4BAAmB,GAAE,CAAC;IACvC,IAAA,WAAW,GAAK,IAAA,0BAAa,GAAE,YAApB,CAAqB;IACxC,IAAM,eAAe,GAAG,IAAA,2BAAY,EAAC,IAAA,2BAAmB,EAAC,MAAM,CAAC,EAAE,GAAG,CAAC,CAAC;IAEvE,IAAM,iBAAiB,GAAG,IAAA,eAAO,EAAC;QAChC,IAAI,CAAC,gBAAgB,IAAI,CAAC,eAAe,CAAC,UAAU,EAAE;YACpD,OAAO,IAAI,CAAC;SACb;QACD,IAAM,iBAAiB,GAAG,gBAAgB,CAAC,KAAK,GAAG,gBAAgB,CAAC,MAAM,CAAC;QAC3E,IAAM,iBAAiB,GAAG,eAAe,CAAC,UAAU,CAAC,KAAK,GAAG,eAAe,CAAC,UAAU,CAAC,MAAM,CAAC;QAE/F,OAAO,iBAAiB,IAAI,iBAAiB;YAC3C,CAAC,CAAC;gBACE,KAAK,EAAE,gBAAgB,CAAC,MAAM,GAAG,iBAAiB;gBAClD,MAAM,EAAE,gBAAgB,CAAC,MAAM;aAChC;YACH,CAAC,CAAC;gBACE,KAAK,EAAE,gBAAgB,CAAC,KAAK;gBAC7B,MAAM,EAAE,gBAAgB,CAAC,KAAK,GAAG,iBAAiB;aACnD,CAAC;IACR,CAAC,EAAE,CAAC,gBAAgB,EAAE,eAAe,CAAC,UAAU,CAAC,CAAC,CAAC;IAEnD,IAAA,iBAAS,EAAC;QACR,IAAI,eAAe,CAAC,MAAM,IAAI,GAAG,CAAC,OAAO,EAAE;YACzC,GAAG,CAAC,OAAO,CAAC,SAAS,GAAG,eAAe,CAAC,MAAM,CAAC;YAC/C,GAAG,CAAC,OAAO,CAAC,gBAAgB,GAAG;;gBAC7B,MAAA,GAAG,CAAC,OAAO,0CAAE,IAAI,GAAG,KAAK,CAAC,WAAW,CAAC,CAAC;YACzC,CAAC,CAAC;SACH;IACH,CAAC,EAAE,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC;IAE7B,OAAO,IAAA,eAAO,EACZ,cAAM,OAAA,YACJ,GAAG,KAAA,EACH,iBAAiB,mBAAA,IACd,eAAe,EAClB,EAJI,CAIJ,EACF,CAAC,iBAAiB,EAAE,eAAe,CAAC,CACrC,CAAC;AACJ,CAAC;AAzCD,4CAyCC"}
@@ -0,0 +1,31 @@
1
+ import { RefObject } from 'react';
2
+ import { PixelDimensions } from '@monkvision/types';
3
+ import { InternalCameraMonitoringConfig } from '../monitoring';
4
+ /**
5
+ * Configuration parameters for the `useCameraScreenshot` hook.
6
+ */
7
+ export interface CameraScreenshotConfig {
8
+ /**
9
+ * The ref to the video element to take screenshots of.
10
+ */
11
+ videoRef: RefObject<HTMLVideoElement>;
12
+ /**
13
+ * The ref to the canvas element used to draw the screenshots
14
+ */
15
+ canvasRef: RefObject<HTMLCanvasElement>;
16
+ /**
17
+ * The dimensions of the screenshot.
18
+ */
19
+ dimensions: PixelDimensions | null;
20
+ }
21
+ /**
22
+ * Callback used to take a screenshot.
23
+ *
24
+ * @return A ImageData object that contains the raw pixel's data.
25
+ */
26
+ export type TakeScreenshotFunction = (monitoring: InternalCameraMonitoringConfig) => ImageData;
27
+ /**
28
+ * Custom hook used to take screenshots of a video element. This hook is aimed to be used in pair with a `<canvas>`
29
+ * element referenced by the returned ref.
30
+ */
31
+ export declare function useCameraScreenshot({ videoRef, canvasRef, dimensions, }: CameraScreenshotConfig): TakeScreenshotFunction;
@@ -0,0 +1,66 @@
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.useCameraScreenshot = void 0;
15
+ var react_1 = require("react");
16
+ var monitoring_1 = require("@monkvision/monitoring");
17
+ var monitoring_2 = require("../monitoring");
18
+ var utils_1 = require("./utils");
19
+ function startScreenshotMeasurement(monitoring, dimensions) {
20
+ var _a;
21
+ var _b, _c;
22
+ (_b = monitoring.transaction) === null || _b === void 0 ? void 0 : _b.startMeasurement(monitoring_2.ScreenshotMeasurement.operation, {
23
+ data: monitoring.data,
24
+ tags: __assign((_a = {}, _a[monitoring_2.ScreenshotMeasurement.outputResolutionTagName] = dimensions
25
+ ? "".concat(dimensions.width, "x").concat(dimensions.height)
26
+ : 'null', _a), ((_c = monitoring.tags) !== null && _c !== void 0 ? _c : {})),
27
+ description: monitoring_2.ScreenshotMeasurement.description,
28
+ });
29
+ }
30
+ function stopScreenshotMeasurement(monitoringConfig, status) {
31
+ var _a;
32
+ (_a = monitoringConfig.transaction) === null || _a === void 0 ? void 0 : _a.stopMeasurement(monitoring_2.ScreenshotMeasurement.operation, status);
33
+ }
34
+ function setScreeshotSizeMeasurement(monitoring, image) {
35
+ var _a;
36
+ var imageSizeBytes = image.data.length;
37
+ (_a = monitoring.transaction) === null || _a === void 0 ? void 0 : _a.setMeasurement(monitoring_2.ScreenshotSizeMeasurement.name, imageSizeBytes, 'byte');
38
+ }
39
+ /**
40
+ * Custom hook used to take screenshots of a video element. This hook is aimed to be used in pair with a `<canvas>`
41
+ * element referenced by the returned ref.
42
+ */
43
+ function useCameraScreenshot(_a) {
44
+ var videoRef = _a.videoRef, canvasRef = _a.canvasRef, dimensions = _a.dimensions;
45
+ return (0, react_1.useCallback)(function (monitoring) {
46
+ startScreenshotMeasurement(monitoring, dimensions);
47
+ var context = (0, utils_1.getCanvasHandle)(canvasRef, function () {
48
+ return stopScreenshotMeasurement(monitoring, monitoring_1.TransactionStatus.UNKNOWN_ERROR);
49
+ }).context;
50
+ if (!dimensions) {
51
+ stopScreenshotMeasurement(monitoring, monitoring_1.TransactionStatus.UNKNOWN_ERROR);
52
+ throw new Error('Unable to take a picture because the video stream has no dimension.');
53
+ }
54
+ if (!videoRef.current) {
55
+ stopScreenshotMeasurement(monitoring, monitoring_1.TransactionStatus.UNKNOWN_ERROR);
56
+ throw new Error('Unable to take a picture because the video element is null.');
57
+ }
58
+ context.drawImage(videoRef.current, 0, 0, dimensions.width, dimensions.height);
59
+ var imageData = context.getImageData(0, 0, dimensions.width, dimensions.height);
60
+ setScreeshotSizeMeasurement(monitoring, imageData);
61
+ stopScreenshotMeasurement(monitoring, monitoring_1.TransactionStatus.OK);
62
+ return imageData;
63
+ }, [dimensions]);
64
+ }
65
+ exports.useCameraScreenshot = useCameraScreenshot;
66
+ //# sourceMappingURL=useCameraScreenshot.js.map
@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AAK,IAAA,uBAAe,EAAC,SAAS,EAAE;YAC7C,OAAA,yBAAyB,CAAC,UAAU,EAAE,8BAAiB,CAAC,aAAa,CAAC;QAAtE,CAAsE,CACvE,QAFc,CAEb;QACF,IAAI,CAAC,UAAU,EAAE;YACf,yBAAyB,CAAC,UAAU,EAAE,8BAAiB,CAAC,aAAa,CAAC,CAAC;YACvE,MAAM,IAAI,KAAK,CAAC,qEAAqE,CAAC,CAAC;SACxF;QACD,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE;YACrB,yBAAyB,CAAC,UAAU,EAAE,8BAAiB,CAAC,aAAa,CAAC,CAAC;YACvE,MAAM,IAAI,KAAK,CAAC,6DAA6D,CAAC,CAAC;SAChF;QACD,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC,EAAE,UAAU,CAAC,KAAK,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC;QAC/E,IAAM,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,UAAU,CAAC,KAAK,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC;QAClF,2BAA2B,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;QACnD,yBAAyB,CAAC,UAAU,EAAE,8BAAiB,CAAC,EAAE,CAAC,CAAC;QAC5D,OAAO,SAAS,CAAC;IACnB,CAAC,EACD,CAAC,UAAU,CAAC,CACb,CAAC;AACJ,CAAC;AA3BD,kDA2BC"}
@@ -0,0 +1,24 @@
1
+ import { CompressionOptions, MonkPicture } from '@monkvision/types';
2
+ import { RefObject } from 'react';
3
+ import { InternalCameraMonitoringConfig } from '../monitoring';
4
+ /**
5
+ * Params given to the useCompression hooks.
6
+ */
7
+ export interface UseCompressionParams {
8
+ /**
9
+ * The ref to the canvas element used to draw and compress the image.
10
+ */
11
+ canvasRef: RefObject<HTMLCanvasElement>;
12
+ /**
13
+ * The compression options.
14
+ */
15
+ options: CompressionOptions;
16
+ }
17
+ /**
18
+ * Function used to compress images and create DataURI objects.
19
+ */
20
+ export type CompressFunction = (image: ImageData, monitoring: InternalCameraMonitoringConfig) => Promise<MonkPicture>;
21
+ /**
22
+ * Custom hook used to manage the camera <canvas> element used to take video screenshots and encode images.
23
+ */
24
+ export declare function useCompression({ canvasRef, options }: UseCompressionParams): CompressFunction;
@@ -0,0 +1,126 @@
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
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.useCompression = void 0;
51
+ var monitoring_1 = require("@monkvision/monitoring");
52
+ var react_1 = require("react");
53
+ var monitoring_2 = require("../monitoring");
54
+ var utils_1 = require("./utils");
55
+ function startCompressionMeasurement(monitoring, options, image) {
56
+ var _a;
57
+ var _b, _c;
58
+ (_b = monitoring.transaction) === null || _b === void 0 ? void 0 : _b.startMeasurement(monitoring_2.CompressionMeasurement.operation, {
59
+ data: monitoring.data,
60
+ tags: __assign((_a = {}, _a[monitoring_2.CompressionMeasurement.formatTagName] = options.format, _a[monitoring_2.CompressionMeasurement.qualityTagName] = options.quality, _a[monitoring_2.CompressionMeasurement.dimensionsTagName] = "".concat(image.width, "x").concat(image.height), _a), ((_c = monitoring.tags) !== null && _c !== void 0 ? _c : {})),
61
+ description: monitoring_2.CompressionMeasurement.description,
62
+ });
63
+ }
64
+ function stopCompressionMeasurement(monitoring, status) {
65
+ var _a;
66
+ (_a = monitoring.transaction) === null || _a === void 0 ? void 0 : _a.stopMeasurement(monitoring_2.CompressionMeasurement.operation, status);
67
+ }
68
+ function setCustomMeasurements(monitoring, image, picture) {
69
+ var _a, _b;
70
+ var imageSizeBytes = image.data.length;
71
+ var pictureSizeBytes = picture.blob.size;
72
+ (_a = monitoring.transaction) === null || _a === void 0 ? void 0 : _a.setMeasurement(monitoring_2.CompressionSizeRatioMeasurement.name, pictureSizeBytes / imageSizeBytes, 'ratio');
73
+ (_b = monitoring.transaction) === null || _b === void 0 ? void 0 : _b.setMeasurement(monitoring_2.PictureSizeMeasurement.name, pictureSizeBytes, 'byte');
74
+ }
75
+ function compressUsingBrowser(image, canvasRef, options) {
76
+ var _a = (0, utils_1.getCanvasHandle)(canvasRef), canvas = _a.canvas, context = _a.context;
77
+ context.putImageData(image, 0, 0);
78
+ return new Promise(function (resolve, reject) {
79
+ canvas.toBlob(function (blob) {
80
+ if (!blob) {
81
+ reject(new Error('Unable to convert canvas to Blob, toBlob() returned null.'));
82
+ }
83
+ else {
84
+ resolve({
85
+ blob: blob,
86
+ uri: URL.createObjectURL(blob),
87
+ mimetype: options.format,
88
+ width: image.width,
89
+ height: image.height,
90
+ });
91
+ }
92
+ }, options.format, options.quality);
93
+ });
94
+ }
95
+ /**
96
+ * Custom hook used to manage the camera <canvas> element used to take video screenshots and encode images.
97
+ */
98
+ function useCompression(_a) {
99
+ var _this = this;
100
+ var canvasRef = _a.canvasRef, options = _a.options;
101
+ return (0, react_1.useCallback)(function (image, monitoring) { return __awaiter(_this, void 0, void 0, function () {
102
+ var picture, err_1;
103
+ return __generator(this, function (_a) {
104
+ switch (_a.label) {
105
+ case 0:
106
+ startCompressionMeasurement(monitoring, options, image);
107
+ _a.label = 1;
108
+ case 1:
109
+ _a.trys.push([1, 3, , 4]);
110
+ return [4 /*yield*/, compressUsingBrowser(image, canvasRef, options)];
111
+ case 2:
112
+ picture = _a.sent();
113
+ setCustomMeasurements(monitoring, image, picture);
114
+ stopCompressionMeasurement(monitoring, monitoring_1.TransactionStatus.OK);
115
+ return [2 /*return*/, picture];
116
+ case 3:
117
+ err_1 = _a.sent();
118
+ stopCompressionMeasurement(monitoring, monitoring_1.TransactionStatus.UNKNOWN_ERROR);
119
+ throw err_1;
120
+ case 4: return [2 /*return*/];
121
+ }
122
+ });
123
+ }); }, [options.format, options.quality]);
124
+ }
125
+ exports.useCompression = useCompression;
126
+ //# sourceMappingURL=useCompression.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useCompression.js","sourceRoot":"","sources":["../../../src/Camera/hooks/useCompression.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qDAA2D;AAE3D,+BAA+C;AAC/C,4CAKuB;AACvB,iCAA0C;AAwB1C,SAAS,2BAA2B,CAClC,UAA0C,EAC1C,OAA2B,EAC3B,KAAgB;;;IAEhB,MAAA,UAAU,CAAC,WAAW,0CAAE,gBAAgB,CAAC,mCAAsB,CAAC,SAAS,EAAE;QACzE,IAAI,EAAE,UAAU,CAAC,IAAI;QACrB,IAAI,wBACD,mCAAsB,CAAC,aAAa,IAAG,OAAO,CAAC,MAAM,KACrD,mCAAsB,CAAC,cAAc,IAAG,OAAO,CAAC,OAAO,KACvD,mCAAsB,CAAC,iBAAiB,IAAG,UAAG,KAAK,CAAC,KAAK,cAAI,KAAK,CAAC,MAAM,CAAE,OACzE,CAAC,MAAA,UAAU,CAAC,IAAI,mCAAI,EAAE,CAAC,CAC3B;QACD,WAAW,EAAE,mCAAsB,CAAC,WAAW;KAChD,CAAC,CAAC;AACL,CAAC;AAED,SAAS,0BAA0B,CACjC,UAA0C,EAC1C,MAAyB;;IAEzB,MAAA,UAAU,CAAC,WAAW,0CAAE,eAAe,CAAC,mCAAsB,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;AACpF,CAAC;AAED,SAAS,qBAAqB,CAC5B,UAA0C,EAC1C,KAAgB,EAChB,OAAoB;;IAEpB,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,mBAAW,EAChB,UAAO,KAAgB,EAAE,UAA0C;;;;;oBACjE,2BAA2B,CAAC,UAAU,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;;;;oBAEtC,qBAAM,oBAAoB,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,CAAC,EAAA;;oBAA/D,OAAO,GAAG,SAAqD;oBACrE,qBAAqB,CAAC,UAAU,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;oBAClD,0BAA0B,CAAC,UAAU,EAAE,8BAAiB,CAAC,EAAE,CAAC,CAAC;oBAC7D,sBAAO,OAAO,EAAC;;;oBAEf,0BAA0B,CAAC,UAAU,EAAE,8BAAiB,CAAC,aAAa,CAAC,CAAC;oBACxE,MAAM,KAAG,CAAC;;;;SAEb,EACD,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,CAClC,CAAC;AACJ,CAAC;AAhBD,wCAgBC"}
@@ -0,0 +1,32 @@
1
+ import { MonkPicture } from '@monkvision/types';
2
+ import { CameraMonitoringConfig, InternalCameraMonitoringConfig } from '../monitoring';
3
+ import { CompressFunction } from './useCompression';
4
+ import { UserMediaResult } from './useUserMedia';
5
+ /**
6
+ * Type definition for the parameters of the useTakePicture hook.
7
+ */
8
+ export interface UseTakePictureParams extends Pick<UserMediaResult, 'availableCameraDevices' | 'selectedCameraDeviceId'> {
9
+ /**
10
+ * Callback used to compress the screenshot taken by the takeScreenshot function.
11
+ */
12
+ compress: CompressFunction;
13
+ /**
14
+ * Callback used to take the screenshot of the camera video stream.
15
+ */
16
+ takeScreenshot: (monitoring: InternalCameraMonitoringConfig) => ImageData;
17
+ /**
18
+ * Event handler called when a picture has been taken.
19
+ */
20
+ onPictureTaken?: (picture: MonkPicture) => void;
21
+ /**
22
+ * The optional custom monitoring configuration that can be passed to the Camera component.
23
+ */
24
+ monitoring?: CameraMonitoringConfig;
25
+ }
26
+ /**
27
+ * Custom hook used by the Camera component to create the take picture function.
28
+ */
29
+ export declare function useTakePicture({ compress, takeScreenshot, onPictureTaken, monitoring, availableCameraDevices, selectedCameraDeviceId, }: UseTakePictureParams): {
30
+ takePicture: () => Promise<MonkPicture>;
31
+ isLoading: boolean;
32
+ };
@@ -0,0 +1,100 @@
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
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.useTakePicture = void 0;
51
+ var monitoring_1 = require("@monkvision/monitoring");
52
+ var common_1 = require("@monkvision/common");
53
+ var react_1 = require("react");
54
+ var monitoring_2 = require("../monitoring");
55
+ function createTakePictureTransactionData(_a) {
56
+ var monitoring = _a.monitoring, availableCameraDevices = _a.availableCameraDevices, selectedCameraDeviceId = _a.selectedCameraDeviceId;
57
+ return __assign(__assign({}, monitoring === null || monitoring === void 0 ? void 0 : monitoring.data), { availableCameras: availableCameraDevices.map(function (deviceInfo) { return "".concat(deviceInfo.label, " (").concat(deviceInfo.deviceId, ")"); }), selectedCameraId: selectedCameraDeviceId });
58
+ }
59
+ /**
60
+ * Custom hook used by the Camera component to create the take picture function.
61
+ */
62
+ function useTakePicture(_a) {
63
+ var _this = this;
64
+ var compress = _a.compress, takeScreenshot = _a.takeScreenshot, onPictureTaken = _a.onPictureTaken, monitoring = _a.monitoring, availableCameraDevices = _a.availableCameraDevices, selectedCameraDeviceId = _a.selectedCameraDeviceId;
65
+ var createTransaction = (0, monitoring_1.useMonitoring)().createTransaction;
66
+ var _b = (0, react_1.useState)(false), isLoading = _b[0], setIsLoading = _b[1];
67
+ var takePicture = (0, react_1.useCallback)(function () { return __awaiter(_this, void 0, void 0, function () {
68
+ var data, transaction, childMonitoring, screenshot, picture;
69
+ return __generator(this, function (_a) {
70
+ switch (_a.label) {
71
+ case 0:
72
+ setIsLoading(true);
73
+ data = createTakePictureTransactionData({
74
+ monitoring: monitoring,
75
+ availableCameraDevices: availableCameraDevices,
76
+ selectedCameraDeviceId: selectedCameraDeviceId,
77
+ });
78
+ transaction = createTransaction(__assign(__assign(__assign({}, monitoring_2.TakePictureTransaction), monitoring), { data: data }));
79
+ childMonitoring = {
80
+ transaction: transaction,
81
+ data: monitoring === null || monitoring === void 0 ? void 0 : monitoring.data,
82
+ tags: monitoring === null || monitoring === void 0 ? void 0 : monitoring.tags,
83
+ };
84
+ screenshot = takeScreenshot(childMonitoring);
85
+ return [4 /*yield*/, compress(screenshot, childMonitoring)];
86
+ case 1:
87
+ picture = _a.sent();
88
+ transaction.finish();
89
+ setIsLoading(false);
90
+ if (onPictureTaken) {
91
+ onPictureTaken(picture);
92
+ }
93
+ return [2 /*return*/, picture];
94
+ }
95
+ });
96
+ }); }, [createTransaction, monitoring, takeScreenshot, compress, onPictureTaken]);
97
+ return (0, common_1.useObjectMemo)({ takePicture: takePicture, isLoading: isLoading });
98
+ }
99
+ exports.useTakePicture = useTakePicture;
100
+ //# sourceMappingURL=useTakePicture.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTakePicture.js","sourceRoot":"","sources":["../../../src/Camera/hooks/useTakePicture.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qDAAuD;AACvD,6CAAmD;AAEnD,+BAA8C;AAC9C,4CAIuB;AA2BvB,SAAS,gCAAgC,CAAC,EAOzC;QANC,UAAU,gBAAA,EACV,sBAAsB,4BAAA,EACtB,sBAAsB,4BAAA;IAKtB,6BACK,UAAU,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AAC;oBACpB,IAAI,cAAc,EAAE;wBAClB,cAAc,CAAC,OAAO,CAAC,CAAC;qBACzB;oBACD,sBAAO,OAAO,EAAC;;;SAChB,EAAE,CAAC,iBAAiB,EAAE,UAAU,EAAE,cAAc,EAAE,QAAQ,EAAE,cAAc,CAAC,CAAC,CAAC;IAE9E,OAAO,IAAA,sBAAa,EAAC,EAAE,WAAW,aAAA,EAAE,SAAS,WAAA,EAAE,CAAC,CAAC;AACnD,CAAC;AAnCD,wCAmCC"}
@@ -0,0 +1,122 @@
1
+ import { RefObject } from 'react';
2
+ import { PixelDimensions } from '@monkvision/types';
3
+ /**
4
+ * Enumeration of the different Native error names that can happen when a stream is invalid.
5
+ */
6
+ export declare enum InvalidStreamErrorName {
7
+ /**
8
+ * The stream had no video track.
9
+ */
10
+ NO_VIDEO_TRACK = "NoVideoTrack",
11
+ /**
12
+ * The stream had too many video tracks (more than one).
13
+ */
14
+ TOO_MANY_VIDEO_TRACKS = "TooManyVideoTracks",
15
+ /**
16
+ * The stream's video track had no dimensions.
17
+ */
18
+ NO_DIMENSIONS = "NoDimensions"
19
+ }
20
+ /**
21
+ * The type of errors that the `useUserMedia` hook can return.
22
+ *
23
+ * @see useUserMedia
24
+ */
25
+ export declare enum UserMediaErrorType {
26
+ /**
27
+ * The camera stream couldn't be fetched because the web page does not have the permissions to access the camera.
28
+ */
29
+ NOT_ALLOWED = "not_allowed",
30
+ /**
31
+ * The camera stream was successfully fetched, but it could be processed. This error can happen for the following
32
+ * reasons :
33
+ * - The stream did not contain any video track, or contained multiple ones.
34
+ * - The stream's video track did not have any width or height property defined.
35
+ */
36
+ INVALID_STREAM = "invalid_stream",
37
+ /**
38
+ * The camera stream was successfully fetched, but was made inactive for some unknown reason. Some known reasons that
39
+ * could be causing this error are :
40
+ * - The user revoked the camera access rights of the webpage while the stream was running.
41
+ * - On Safari, making a call to `navigator.mediaDevices.getUserMedia` will close all other existing streams on the
42
+ * web page. When using the Monk Camera package, you need to make sure that your app does not call `getUserMedia`
43
+ * while the camera stream is running. You can take a look at
44
+ * [this page](https://webrtchacks.com/guide-to-safari-webrtc/) for more reading about this behaviour.
45
+ */
46
+ STREAM_INACTIVE = "stream_inactive",
47
+ /**
48
+ * Any other error.
49
+ */
50
+ OTHER = "other"
51
+ }
52
+ /**
53
+ * Type definition for the error wrapper that the `useUserMedia` hook can return in case of error. See the
54
+ * `UserMediaErrorType` enum for details about the type of errors that can be catched.
55
+ *
56
+ * @see UserMediaErrorType
57
+ */
58
+ export interface UserMediaError {
59
+ /**
60
+ * The type of error that occurred.
61
+ */
62
+ type: UserMediaErrorType;
63
+ /**
64
+ * The JavaScript `Error` object containing details about the error.
65
+ */
66
+ nativeError: Error | unknown;
67
+ }
68
+ /**
69
+ * Interface describing the result of the `useUserMedia` hook.
70
+ *
71
+ * @see useUserMedia
72
+ */
73
+ export interface UserMediaResult {
74
+ /**
75
+ * The resulting video stream. The stream can be null when not initialized or in case of an error.
76
+ */
77
+ stream: MediaStream | null;
78
+ /**
79
+ * The dimensions of the resulting camera stream. Note that these dimensions can differ from the ones given in the
80
+ * stream constraints if they are not supported or available on the current device.
81
+ */
82
+ dimensions: PixelDimensions | null;
83
+ /**
84
+ * The error details. If no error has occurred, this object will be null.
85
+ */
86
+ error: UserMediaError | null;
87
+ /**
88
+ * A loading indicator. This field is `true` while the hook is currently fetching the stream or applying new
89
+ * constraints.
90
+ */
91
+ isLoading: boolean;
92
+ /**
93
+ * A function used to retry in case of error. If there has been no error, or if the stream is loading, this function
94
+ * will do nothing. In case of an error, this function resets the state and tries to fetch a camera stream again.
95
+ */
96
+ retry: () => void;
97
+ /**
98
+ * The list of all available camera devices on the user's phone.
99
+ */
100
+ availableCameraDevices: MediaDeviceInfo[];
101
+ /**
102
+ * The ID of the selected camera device for picture taking.
103
+ */
104
+ selectedCameraDeviceId: string | null;
105
+ }
106
+ /**
107
+ * React hook that wraps the `navigator.mediaDevices.getUserMedia` browser function in order to add React logic layers
108
+ * and utility tools :
109
+ * - Creates an effect for `getUserMedia` that will be run everytime some state parameters are updated.
110
+ * - Will call `track.applyConstraints` when the video contstraints are updated in order to update the video stream.
111
+ * - Makes sure that the `getUserMedia` is only called when it needs to be using memoized state.
112
+ * - Provides various utilities such as error catching, loading information and a retry on failure feature.
113
+ *
114
+ * @param constraints The same media constraints you would pass to the `getUserMedia` function. Note that this hook has
115
+ * been designed for video only, so audio constraints could provoke unexpected behaviour.
116
+ * @param videoRef The ref to the video element displaying the camera preview stream.
117
+ * @return The result of this hook contains the resulting video stream, an error object if there has been an error, a
118
+ * loading indicator and a retry function that tries to get a camera stream again. See the `UserMediaResult` interface
119
+ * for more information.
120
+ * @see UserMediaResult
121
+ */
122
+ export declare function useUserMedia(constraints: MediaStreamConstraints, videoRef: RefObject<HTMLVideoElement>): UserMediaResult;