@capgo/camera-preview 7.3.11 → 7.4.0-alpha.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 (47) hide show
  1. package/CapgoCameraPreview.podspec +16 -13
  2. package/README.md +492 -73
  3. package/android/build.gradle +11 -0
  4. package/android/gradle/wrapper/gradle-wrapper.properties +1 -1
  5. package/android/src/main/AndroidManifest.xml +5 -3
  6. package/android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java +968 -505
  7. package/android/src/main/java/com/ahm/capacitor/camera/preview/CameraXView.java +3017 -0
  8. package/android/src/main/java/com/ahm/capacitor/camera/preview/GridOverlayView.java +119 -0
  9. package/android/src/main/java/com/ahm/capacitor/camera/preview/model/CameraDevice.java +63 -0
  10. package/android/src/main/java/com/ahm/capacitor/camera/preview/model/CameraLens.java +79 -0
  11. package/android/src/main/java/com/ahm/capacitor/camera/preview/model/CameraSessionConfiguration.java +167 -0
  12. package/android/src/main/java/com/ahm/capacitor/camera/preview/model/LensInfo.java +40 -0
  13. package/android/src/main/java/com/ahm/capacitor/camera/preview/model/ZoomFactors.java +35 -0
  14. package/dist/docs.json +1041 -161
  15. package/dist/esm/definitions.d.ts +484 -84
  16. package/dist/esm/definitions.js +10 -1
  17. package/dist/esm/definitions.js.map +1 -1
  18. package/dist/esm/web.d.ts +78 -3
  19. package/dist/esm/web.js +813 -68
  20. package/dist/esm/web.js.map +1 -1
  21. package/dist/plugin.cjs.js +819 -68
  22. package/dist/plugin.cjs.js.map +1 -1
  23. package/dist/plugin.js +819 -68
  24. package/dist/plugin.js.map +1 -1
  25. package/ios/Sources/CapgoCameraPreviewPlugin/CameraController.swift +1663 -0
  26. package/ios/Sources/CapgoCameraPreviewPlugin/GridOverlayView.swift +65 -0
  27. package/ios/Sources/CapgoCameraPreviewPlugin/Plugin.swift +1550 -0
  28. package/ios/Tests/CameraPreviewPluginTests/CameraPreviewPluginTests.swift +15 -0
  29. package/package.json +2 -2
  30. package/android/src/main/java/com/ahm/capacitor/camera/preview/CameraActivity.java +0 -1279
  31. package/android/src/main/java/com/ahm/capacitor/camera/preview/CustomSurfaceView.java +0 -29
  32. package/android/src/main/java/com/ahm/capacitor/camera/preview/CustomTextureView.java +0 -39
  33. package/android/src/main/java/com/ahm/capacitor/camera/preview/Preview.java +0 -461
  34. package/android/src/main/java/com/ahm/capacitor/camera/preview/TapGestureDetector.java +0 -24
  35. package/ios/Plugin/CameraController.swift +0 -809
  36. package/ios/Plugin/Info.plist +0 -24
  37. package/ios/Plugin/Plugin.h +0 -10
  38. package/ios/Plugin/Plugin.m +0 -18
  39. package/ios/Plugin/Plugin.swift +0 -511
  40. package/ios/Plugin.xcodeproj/project.pbxproj +0 -593
  41. package/ios/Plugin.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  42. package/ios/Plugin.xcworkspace/contents.xcworkspacedata +0 -10
  43. package/ios/Plugin.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  44. package/ios/PluginTests/Info.plist +0 -22
  45. package/ios/PluginTests/PluginTests.swift +0 -83
  46. package/ios/Podfile +0 -13
  47. package/ios/Podfile.lock +0 -23
@@ -2,10 +2,22 @@
2
2
 
3
3
  var core = require('@capacitor/core');
4
4
 
5
+ exports.DeviceType = void 0;
6
+ (function (DeviceType) {
7
+ DeviceType["ULTRA_WIDE"] = "ultraWide";
8
+ DeviceType["WIDE_ANGLE"] = "wideAngle";
9
+ DeviceType["TELEPHOTO"] = "telephoto";
10
+ DeviceType["TRUE_DEPTH"] = "trueDepth";
11
+ DeviceType["DUAL"] = "dual";
12
+ DeviceType["DUAL_WIDE"] = "dualWide";
13
+ DeviceType["TRIPLE"] = "triple";
14
+ })(exports.DeviceType || (exports.DeviceType = {}));
15
+
5
16
  const CameraPreview = core.registerPlugin("CameraPreview", {
6
17
  web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.CameraPreviewWeb()),
7
18
  });
8
19
 
20
+ const DEFAULT_VIDEO_ID = "capgo_video";
9
21
  class CameraPreviewWeb extends core.WebPlugin {
10
22
  constructor() {
11
23
  super();
@@ -14,80 +26,320 @@ class CameraPreviewWeb extends core.WebPlugin {
14
26
  * used in capture
15
27
  */
16
28
  this.isBackCamera = false;
29
+ this.currentDeviceId = null;
30
+ this.videoElement = null;
31
+ this.isStarted = false;
17
32
  }
18
33
  async getSupportedPictureSizes() {
19
34
  throw new Error("getSupportedPictureSizes not supported under the web platform");
20
35
  }
21
36
  async start(options) {
22
- var _a;
23
- await navigator.mediaDevices
24
- .getUserMedia({
25
- audio: !options.disableAudio,
26
- video: true,
27
- })
28
- .then((stream) => {
29
- // Stop any existing stream so we can request media with different constraints based on user input
30
- stream.getTracks().forEach((track) => track.stop());
31
- })
32
- .catch((error) => {
33
- Promise.reject(error);
34
- });
35
- const video = document.getElementById("video");
37
+ if (options.aspectRatio && (options.width || options.height)) {
38
+ throw new Error("Cannot set both aspectRatio and size (width/height). Use setPreviewSize after start.");
39
+ }
40
+ if (this.isStarted) {
41
+ throw new Error("camera already started");
42
+ }
43
+ this.isBackCamera = true;
44
+ this.isStarted = false;
36
45
  const parent = document.getElementById((options === null || options === void 0 ? void 0 : options.parent) || "");
37
- if (!video) {
38
- const videoElement = document.createElement("video");
39
- videoElement.id = "video";
40
- videoElement.setAttribute("class", (options === null || options === void 0 ? void 0 : options.className) || "");
41
- // Don't flip video feed if camera is rear facing
42
- if (options.position !== "rear") {
43
- videoElement.setAttribute("style", "-webkit-transform: scaleX(-1); transform: scaleX(-1);");
44
- }
45
- const userAgent = navigator.userAgent.toLowerCase();
46
- const isSafari = userAgent.includes("safari") && !userAgent.includes("chrome");
47
- // Safari on iOS needs to have the autoplay, muted and playsinline attributes set for video.play() to be successful
48
- // Without these attributes videoElement.play() will throw a NotAllowedError
49
- // https://developer.apple.com/documentation/webkit/delivering_video_content_for_safari
50
- if (isSafari) {
51
- videoElement.setAttribute("autoplay", "true");
52
- videoElement.setAttribute("muted", "true");
53
- videoElement.setAttribute("playsinline", "true");
54
- }
55
- parent === null || parent === void 0 ? void 0 : parent.appendChild(videoElement);
56
- if ((_a = navigator === null || navigator === void 0 ? void 0 : navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.getUserMedia) {
57
- const constraints = {
58
- video: {
59
- width: { ideal: options.width },
60
- height: { ideal: options.height },
61
- },
62
- };
63
- if (options.position === "rear") {
64
- constraints.video.facingMode =
65
- "environment";
66
- this.isBackCamera = true;
46
+ const gridMode = (options === null || options === void 0 ? void 0 : options.gridMode) || "none";
47
+ const positioning = (options === null || options === void 0 ? void 0 : options.positioning) || "top";
48
+ if (options.position) {
49
+ this.isBackCamera = options.position === "rear";
50
+ }
51
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
52
+ if (video) {
53
+ video.remove();
54
+ }
55
+ const container = options.parent
56
+ ? document.getElementById(options.parent)
57
+ : document.body;
58
+ if (!container) {
59
+ throw new Error("container not found");
60
+ }
61
+ this.videoElement = document.createElement("video");
62
+ this.videoElement.id = DEFAULT_VIDEO_ID;
63
+ this.videoElement.className = options.className || "";
64
+ this.videoElement.playsInline = true;
65
+ this.videoElement.muted = true;
66
+ this.videoElement.autoplay = true;
67
+ // Remove objectFit as we'll match camera's native aspect ratio
68
+ this.videoElement.style.backgroundColor = "transparent";
69
+ // Reset any default margins that might interfere
70
+ this.videoElement.style.margin = "0";
71
+ this.videoElement.style.padding = "0";
72
+ container.appendChild(this.videoElement);
73
+ if (options.toBack) {
74
+ this.videoElement.style.zIndex = "-1";
75
+ }
76
+ // Default to 16:9 vertical (9:16 for portrait) if no aspect ratio or size specified
77
+ const useDefaultAspectRatio = !options.aspectRatio && !options.width && !options.height;
78
+ const effectiveAspectRatio = options.aspectRatio || (useDefaultAspectRatio ? "16:9" : null);
79
+ if (options.width) {
80
+ this.videoElement.width = options.width;
81
+ this.videoElement.style.width = `${options.width}px`;
82
+ }
83
+ if (options.height) {
84
+ this.videoElement.height = options.height;
85
+ this.videoElement.style.height = `${options.height}px`;
86
+ }
87
+ // Handle positioning - center if x or y not provided
88
+ const centerX = options.x === undefined;
89
+ const centerY = options.y === undefined;
90
+ // Always set position to absolute for proper positioning
91
+ this.videoElement.style.position = "absolute";
92
+ console.log("Initial positioning flags:", {
93
+ centerX,
94
+ centerY,
95
+ x: options.x,
96
+ y: options.y,
97
+ });
98
+ if (options.x !== undefined) {
99
+ this.videoElement.style.left = `${options.x}px`;
100
+ }
101
+ if (options.y !== undefined) {
102
+ this.videoElement.style.top = `${options.y}px`;
103
+ }
104
+ // Create and add grid overlay if needed
105
+ if (gridMode !== "none") {
106
+ const gridOverlay = this.createGridOverlay(gridMode);
107
+ gridOverlay.id = "camera-grid-overlay";
108
+ parent === null || parent === void 0 ? void 0 : parent.appendChild(gridOverlay);
109
+ }
110
+ // Aspect ratio handling is now done after getting camera stream
111
+ // Store centering flags for later use
112
+ const needsCenterX = centerX;
113
+ const needsCenterY = centerY;
114
+ console.log("Centering flags stored:", { needsCenterX, needsCenterY });
115
+ // First get the camera stream with basic constraints
116
+ const constraints = {
117
+ video: {
118
+ facingMode: this.isBackCamera ? "environment" : "user",
119
+ },
120
+ };
121
+ const stream = await navigator.mediaDevices.getUserMedia(constraints);
122
+ if (!stream) {
123
+ throw new Error("could not acquire stream");
124
+ }
125
+ if (!this.videoElement) {
126
+ throw new Error("video element not found");
127
+ }
128
+ // Get the actual camera dimensions from the video track
129
+ const videoTrack = stream.getVideoTracks()[0];
130
+ const settings = videoTrack.getSettings();
131
+ const cameraWidth = settings.width || 640;
132
+ const cameraHeight = settings.height || 480;
133
+ const cameraAspectRatio = cameraWidth / cameraHeight;
134
+ console.log("Camera native dimensions:", {
135
+ width: cameraWidth,
136
+ height: cameraHeight,
137
+ aspectRatio: cameraAspectRatio,
138
+ });
139
+ console.log("Container dimensions:", {
140
+ width: container.offsetWidth,
141
+ height: container.offsetHeight,
142
+ id: container.id,
143
+ });
144
+ // Now adjust video element size based on camera's native aspect ratio
145
+ if (!options.width && !options.height && !options.aspectRatio) {
146
+ // No size specified, fit camera view within container bounds
147
+ const containerWidth = container.offsetWidth || window.innerWidth;
148
+ const containerHeight = container.offsetHeight || window.innerHeight;
149
+ // Calculate dimensions that fit within container while maintaining camera aspect ratio
150
+ let targetWidth, targetHeight;
151
+ // Try fitting to container width first
152
+ targetWidth = containerWidth;
153
+ targetHeight = targetWidth / cameraAspectRatio;
154
+ // If height exceeds container, fit to height instead
155
+ if (targetHeight > containerHeight) {
156
+ targetHeight = containerHeight;
157
+ targetWidth = targetHeight * cameraAspectRatio;
158
+ }
159
+ console.log("Video element dimensions:", {
160
+ width: targetWidth,
161
+ height: targetHeight,
162
+ container: { width: containerWidth, height: containerHeight },
163
+ });
164
+ this.videoElement.width = targetWidth;
165
+ this.videoElement.height = targetHeight;
166
+ this.videoElement.style.width = `${targetWidth}px`;
167
+ this.videoElement.style.height = `${targetHeight}px`;
168
+ // Center the video element within its parent container
169
+ if (needsCenterX || options.x === undefined) {
170
+ const x = Math.round((containerWidth - targetWidth) / 2);
171
+ this.videoElement.style.left = `${x}px`;
172
+ }
173
+ if (needsCenterY || options.y === undefined) {
174
+ let y;
175
+ switch (positioning) {
176
+ case "top":
177
+ y = 0;
178
+ break;
179
+ case "bottom":
180
+ y = window.innerHeight - targetHeight;
181
+ break;
182
+ case "center":
183
+ default:
184
+ y = Math.round((window.innerHeight - targetHeight) / 2);
185
+ break;
67
186
  }
68
- else {
69
- this.isBackCamera = false;
187
+ this.videoElement.style.setProperty("top", `${y}px`, "important");
188
+ // Force a style recalculation
189
+ this.videoElement.offsetHeight;
190
+ console.log("Positioning video:", {
191
+ positioning,
192
+ viewportHeight: window.innerHeight,
193
+ targetHeight,
194
+ calculatedY: y,
195
+ actualTop: this.videoElement.style.top,
196
+ position: this.videoElement.style.position,
197
+ });
198
+ }
199
+ }
200
+ else if (effectiveAspectRatio && !options.width && !options.height) {
201
+ // Aspect ratio specified but no size
202
+ const [widthRatio, heightRatio] = effectiveAspectRatio
203
+ .split(":")
204
+ .map(Number);
205
+ const targetRatio = widthRatio / heightRatio;
206
+ const viewportWidth = window.innerWidth;
207
+ const viewportHeight = window.innerHeight;
208
+ let targetWidth, targetHeight;
209
+ // Try fitting to viewport width first
210
+ targetWidth = viewportWidth;
211
+ targetHeight = targetWidth / targetRatio;
212
+ // If height exceeds viewport, fit to height instead
213
+ if (targetHeight > viewportHeight) {
214
+ targetHeight = viewportHeight;
215
+ targetWidth = targetHeight * targetRatio;
216
+ }
217
+ this.videoElement.width = targetWidth;
218
+ this.videoElement.height = targetHeight;
219
+ this.videoElement.style.width = `${targetWidth}px`;
220
+ this.videoElement.style.height = `${targetHeight}px`;
221
+ // Center the video element within its parent container
222
+ if (needsCenterX || options.x === undefined) {
223
+ const parentWidth = container.offsetWidth || viewportWidth;
224
+ const x = Math.round((parentWidth - targetWidth) / 2);
225
+ this.videoElement.style.left = `${x}px`;
226
+ }
227
+ if (needsCenterY || options.y === undefined) {
228
+ const parentHeight = container.offsetHeight || viewportHeight;
229
+ let y;
230
+ switch (positioning) {
231
+ case "top":
232
+ y = 0;
233
+ break;
234
+ case "bottom":
235
+ y = parentHeight - targetHeight;
236
+ break;
237
+ case "center":
238
+ default:
239
+ y = Math.round((parentHeight - targetHeight) / 2);
240
+ break;
70
241
  }
71
- const self = this;
72
- await navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
73
- if (document.getElementById("video")) {
74
- // video.src = window.URL.createObjectURL(stream);
75
- videoElement.srcObject = stream;
76
- videoElement.play();
77
- Promise.resolve({});
242
+ this.videoElement.style.top = `${y}px`;
243
+ }
244
+ }
245
+ this.videoElement.srcObject = stream;
246
+ if (!this.isBackCamera) {
247
+ this.videoElement.style.transform = "scaleX(-1)";
248
+ }
249
+ // Set initial zoom level if specified and supported
250
+ if (options.initialZoomLevel && options.initialZoomLevel !== 1.0) {
251
+ // videoTrack already declared above
252
+ if (videoTrack) {
253
+ const capabilities = videoTrack.getCapabilities();
254
+ if (capabilities.zoom) {
255
+ const zoomLevel = options.initialZoomLevel;
256
+ const minZoom = capabilities.zoom.min || 1;
257
+ const maxZoom = capabilities.zoom.max || 1;
258
+ if (zoomLevel < minZoom || zoomLevel > maxZoom) {
259
+ stream.getTracks().forEach((track) => track.stop());
260
+ throw new Error(`Initial zoom level ${zoomLevel} is not available. Valid range is ${minZoom} to ${maxZoom}`);
261
+ }
262
+ try {
263
+ await videoTrack.applyConstraints({
264
+ advanced: [{ zoom: zoomLevel }],
265
+ });
78
266
  }
79
- else {
80
- self.stopStream(stream);
81
- Promise.reject(new Error("camera already stopped"));
267
+ catch (error) {
268
+ console.warn(`Failed to set initial zoom level: ${error}`);
269
+ // Don't throw, just continue without zoom
82
270
  }
83
- }, (err) => {
84
- Promise.reject(new Error(err));
271
+ }
272
+ }
273
+ }
274
+ this.isStarted = true;
275
+ // Wait for video to be ready and get actual dimensions
276
+ await new Promise((resolve) => {
277
+ if (this.videoElement.readyState >= 2) {
278
+ resolve();
279
+ }
280
+ else {
281
+ this.videoElement.addEventListener("loadeddata", () => resolve(), {
282
+ once: true,
85
283
  });
86
284
  }
285
+ });
286
+ // Ensure centering is applied after DOM updates
287
+ await new Promise((resolve) => requestAnimationFrame(resolve));
288
+ console.log("About to re-center, flags:", { needsCenterX, needsCenterY });
289
+ // Re-apply centering with correct parent dimensions
290
+ if (needsCenterX) {
291
+ const parentWidth = container.offsetWidth;
292
+ const x = Math.round((parentWidth - this.videoElement.offsetWidth) / 2);
293
+ this.videoElement.style.left = `${x}px`;
294
+ console.log("Re-centering X:", {
295
+ parentWidth,
296
+ videoWidth: this.videoElement.offsetWidth,
297
+ x,
298
+ });
87
299
  }
88
- else {
89
- Promise.reject(new Error("camera already started"));
300
+ if (needsCenterY) {
301
+ let y;
302
+ switch (positioning) {
303
+ case "top":
304
+ y = 0;
305
+ break;
306
+ case "bottom":
307
+ y = window.innerHeight - this.videoElement.offsetHeight;
308
+ break;
309
+ case "center":
310
+ default:
311
+ y = Math.round((window.innerHeight - this.videoElement.offsetHeight) / 2);
312
+ break;
313
+ }
314
+ this.videoElement.style.setProperty("top", `${y}px`, "important");
315
+ console.log("Re-positioning Y:", {
316
+ positioning,
317
+ viewportHeight: window.innerHeight,
318
+ videoHeight: this.videoElement.offsetHeight,
319
+ y,
320
+ position: this.videoElement.style.position,
321
+ top: this.videoElement.style.top,
322
+ });
90
323
  }
324
+ // Get the actual rendered dimensions after video is loaded
325
+ const rect = this.videoElement.getBoundingClientRect();
326
+ const computedStyle = window.getComputedStyle(this.videoElement);
327
+ console.log("Final video element state:", {
328
+ rect: { x: rect.x, y: rect.y, width: rect.width, height: rect.height },
329
+ style: {
330
+ position: computedStyle.position,
331
+ left: computedStyle.left,
332
+ top: computedStyle.top,
333
+ width: computedStyle.width,
334
+ height: computedStyle.height,
335
+ },
336
+ });
337
+ return {
338
+ width: Math.round(rect.width),
339
+ height: Math.round(rect.height),
340
+ x: Math.round(rect.x),
341
+ y: Math.round(rect.y),
342
+ };
91
343
  }
92
344
  stopStream(stream) {
93
345
  if (stream) {
@@ -97,16 +349,20 @@ class CameraPreviewWeb extends core.WebPlugin {
97
349
  }
98
350
  }
99
351
  async stop() {
100
- const video = document.getElementById("video");
352
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
101
353
  if (video) {
102
354
  video.pause();
103
355
  this.stopStream(video.srcObject);
104
356
  video.remove();
357
+ this.isStarted = false;
105
358
  }
359
+ // Remove grid overlay if it exists
360
+ const gridOverlay = document.getElementById("camera-grid-overlay");
361
+ gridOverlay === null || gridOverlay === void 0 ? void 0 : gridOverlay.remove();
106
362
  }
107
363
  async capture(options) {
108
364
  return new Promise((resolve, reject) => {
109
- const video = document.getElementById("video");
365
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
110
366
  if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
111
367
  reject(new Error("camera is not running"));
112
368
  return;
@@ -116,14 +372,59 @@ class CameraPreviewWeb extends core.WebPlugin {
116
372
  if (video && video.videoWidth > 0 && video.videoHeight > 0) {
117
373
  const canvas = document.createElement("canvas");
118
374
  const context = canvas.getContext("2d");
119
- canvas.width = video.videoWidth;
120
- canvas.height = video.videoHeight;
375
+ // Calculate capture dimensions
376
+ let captureWidth = video.videoWidth;
377
+ let captureHeight = video.videoHeight;
378
+ let sourceX = 0;
379
+ let sourceY = 0;
380
+ // Check for conflicting parameters
381
+ if (options.aspectRatio && (options.width || options.height)) {
382
+ reject(new Error("Cannot set both aspectRatio and size (width/height). Use setPreviewSize after start."));
383
+ return;
384
+ }
385
+ // Handle aspect ratio if no width/height specified
386
+ if (!options.width && !options.height && options.aspectRatio) {
387
+ const [widthRatio, heightRatio] = options.aspectRatio
388
+ .split(":")
389
+ .map(Number);
390
+ if (widthRatio && heightRatio) {
391
+ // For capture in portrait orientation, swap the aspect ratio (16:9 becomes 9:16)
392
+ const isPortrait = video.videoHeight > video.videoWidth;
393
+ const targetAspectRatio = isPortrait
394
+ ? heightRatio / widthRatio
395
+ : widthRatio / heightRatio;
396
+ const videoAspectRatio = video.videoWidth / video.videoHeight;
397
+ if (videoAspectRatio > targetAspectRatio) {
398
+ // Video is wider than target - crop sides
399
+ captureWidth = video.videoHeight * targetAspectRatio;
400
+ captureHeight = video.videoHeight;
401
+ sourceX = (video.videoWidth - captureWidth) / 2;
402
+ }
403
+ else {
404
+ // Video is taller than target - crop top/bottom
405
+ captureWidth = video.videoWidth;
406
+ captureHeight = video.videoWidth / targetAspectRatio;
407
+ sourceY = (video.videoHeight - captureHeight) / 2;
408
+ }
409
+ }
410
+ }
411
+ else if (options.width || options.height) {
412
+ // If width or height is specified, use them
413
+ if (options.width)
414
+ captureWidth = options.width;
415
+ if (options.height)
416
+ captureHeight = options.height;
417
+ }
418
+ canvas.width = captureWidth;
419
+ canvas.height = captureHeight;
121
420
  // flip horizontally back camera isn't used
122
421
  if (!this.isBackCamera) {
123
- context === null || context === void 0 ? void 0 : context.translate(video.videoWidth, 0);
422
+ context === null || context === void 0 ? void 0 : context.translate(captureWidth, 0);
124
423
  context === null || context === void 0 ? void 0 : context.scale(-1, 1);
125
424
  }
126
- context === null || context === void 0 ? void 0 : context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
425
+ context === null || context === void 0 ? void 0 : context.drawImage(video, sourceX, sourceY, captureWidth, captureHeight, 0, 0, captureWidth, captureHeight);
426
+ if (options.saveToGallery) ;
427
+ if (options.withExifLocation) ;
127
428
  if ((options.format || "jpeg") === "jpeg") {
128
429
  base64EncodedImage = canvas
129
430
  .toDataURL("image/jpeg", (options.quality || 85) / 100.0)
@@ -137,6 +438,7 @@ class CameraPreviewWeb extends core.WebPlugin {
137
438
  }
138
439
  resolve({
139
440
  value: base64EncodedImage,
441
+ exif: {},
140
442
  });
141
443
  });
142
444
  }
@@ -160,13 +462,462 @@ class CameraPreviewWeb extends core.WebPlugin {
160
462
  throw new Error(`setFlashMode not supported under the web platform${_options}`);
161
463
  }
162
464
  async flip() {
163
- throw new Error("flip not supported under the web platform");
465
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
466
+ if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
467
+ throw new Error("camera is not running");
468
+ }
469
+ // Stop current stream
470
+ this.stopStream(video.srcObject);
471
+ // Toggle camera position
472
+ this.isBackCamera = !this.isBackCamera;
473
+ // Get new constraints
474
+ const constraints = {
475
+ video: {
476
+ facingMode: this.isBackCamera ? "environment" : "user",
477
+ width: { ideal: video.videoWidth || 640 },
478
+ height: { ideal: video.videoHeight || 480 },
479
+ },
480
+ };
481
+ try {
482
+ const stream = await navigator.mediaDevices.getUserMedia(constraints);
483
+ video.srcObject = stream;
484
+ // Update current device ID from the new stream
485
+ const videoTrack = stream.getVideoTracks()[0];
486
+ if (videoTrack) {
487
+ this.currentDeviceId = videoTrack.getSettings().deviceId || null;
488
+ }
489
+ // Update video transform based on camera
490
+ if (this.isBackCamera) {
491
+ video.style.transform = "none";
492
+ video.style.webkitTransform = "none";
493
+ }
494
+ else {
495
+ video.style.transform = "scaleX(-1)";
496
+ video.style.webkitTransform = "scaleX(-1)";
497
+ }
498
+ await video.play();
499
+ }
500
+ catch (error) {
501
+ throw new Error(`Failed to flip camera: ${error}`);
502
+ }
164
503
  }
165
504
  async setOpacity(_options) {
166
- const video = document.getElementById("video");
505
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
167
506
  if (!!video && !!_options.opacity)
168
507
  video.style.setProperty("opacity", _options.opacity.toString());
169
508
  }
509
+ async isRunning() {
510
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
511
+ return { isRunning: !!video && !!video.srcObject };
512
+ }
513
+ async getAvailableDevices() {
514
+ var _a;
515
+ if (!((_a = navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.enumerateDevices)) {
516
+ throw new Error("getAvailableDevices not supported under the web platform");
517
+ }
518
+ const devices = await navigator.mediaDevices.enumerateDevices();
519
+ const videoDevices = devices.filter((device) => device.kind === "videoinput");
520
+ // Group devices by position (front/back)
521
+ const frontDevices = [];
522
+ const backDevices = [];
523
+ videoDevices.forEach((device, index) => {
524
+ const label = device.label || `Camera ${index + 1}`;
525
+ const labelLower = label.toLowerCase();
526
+ // Determine device type based on label
527
+ let deviceType = exports.DeviceType.WIDE_ANGLE;
528
+ let baseZoomRatio = 1.0;
529
+ if (labelLower.includes("ultra") || labelLower.includes("0.5")) {
530
+ deviceType = exports.DeviceType.ULTRA_WIDE;
531
+ baseZoomRatio = 0.5;
532
+ }
533
+ else if (labelLower.includes("telephoto") ||
534
+ labelLower.includes("tele") ||
535
+ labelLower.includes("2x") ||
536
+ labelLower.includes("3x")) {
537
+ deviceType = exports.DeviceType.TELEPHOTO;
538
+ baseZoomRatio = 2.0;
539
+ }
540
+ else if (labelLower.includes("depth") ||
541
+ labelLower.includes("truedepth")) {
542
+ deviceType = exports.DeviceType.TRUE_DEPTH;
543
+ baseZoomRatio = 1.0;
544
+ }
545
+ const lensInfo = {
546
+ deviceId: device.deviceId,
547
+ label,
548
+ deviceType,
549
+ focalLength: 4.25,
550
+ baseZoomRatio,
551
+ minZoom: 1.0,
552
+ maxZoom: 1.0,
553
+ };
554
+ // Determine position and add to appropriate array
555
+ if (labelLower.includes("back") || labelLower.includes("rear")) {
556
+ backDevices.push(lensInfo);
557
+ }
558
+ else {
559
+ frontDevices.push(lensInfo);
560
+ }
561
+ });
562
+ const result = [];
563
+ if (frontDevices.length > 0) {
564
+ result.push({
565
+ deviceId: frontDevices[0].deviceId,
566
+ label: "Front Camera",
567
+ position: "front",
568
+ lenses: frontDevices,
569
+ isLogical: false,
570
+ minZoom: Math.min(...frontDevices.map((d) => d.minZoom)),
571
+ maxZoom: Math.max(...frontDevices.map((d) => d.maxZoom)),
572
+ });
573
+ }
574
+ if (backDevices.length > 0) {
575
+ result.push({
576
+ deviceId: backDevices[0].deviceId,
577
+ label: "Back Camera",
578
+ position: "rear",
579
+ lenses: backDevices,
580
+ isLogical: false,
581
+ minZoom: Math.min(...backDevices.map((d) => d.minZoom)),
582
+ maxZoom: Math.max(...backDevices.map((d) => d.maxZoom)),
583
+ });
584
+ }
585
+ return { devices: result };
586
+ }
587
+ async getZoom() {
588
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
589
+ if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
590
+ throw new Error("camera is not running");
591
+ }
592
+ const stream = video.srcObject;
593
+ const videoTrack = stream.getVideoTracks()[0];
594
+ if (!videoTrack) {
595
+ throw new Error("no video track found");
596
+ }
597
+ const capabilities = videoTrack.getCapabilities();
598
+ const settings = videoTrack.getSettings();
599
+ if (!capabilities.zoom) {
600
+ throw new Error("zoom not supported by this device");
601
+ }
602
+ // Get current device info to determine lens type
603
+ let deviceType = exports.DeviceType.WIDE_ANGLE;
604
+ let baseZoomRatio = 1.0;
605
+ if (this.currentDeviceId) {
606
+ const devices = await navigator.mediaDevices.enumerateDevices();
607
+ const device = devices.find((d) => d.deviceId === this.currentDeviceId);
608
+ if (device) {
609
+ const labelLower = device.label.toLowerCase();
610
+ if (labelLower.includes("ultra") || labelLower.includes("0.5")) {
611
+ deviceType = exports.DeviceType.ULTRA_WIDE;
612
+ baseZoomRatio = 0.5;
613
+ }
614
+ else if (labelLower.includes("telephoto") ||
615
+ labelLower.includes("tele") ||
616
+ labelLower.includes("2x") ||
617
+ labelLower.includes("3x")) {
618
+ deviceType = exports.DeviceType.TELEPHOTO;
619
+ baseZoomRatio = 2.0;
620
+ }
621
+ else if (labelLower.includes("depth") ||
622
+ labelLower.includes("truedepth")) {
623
+ deviceType = exports.DeviceType.TRUE_DEPTH;
624
+ baseZoomRatio = 1.0;
625
+ }
626
+ }
627
+ }
628
+ const currentZoom = settings.zoom || 1;
629
+ const lensInfo = {
630
+ focalLength: 4.25,
631
+ deviceType,
632
+ baseZoomRatio,
633
+ digitalZoom: currentZoom / baseZoomRatio,
634
+ };
635
+ return {
636
+ min: capabilities.zoom.min || 1,
637
+ max: capabilities.zoom.max || 1,
638
+ current: currentZoom,
639
+ lens: lensInfo,
640
+ };
641
+ }
642
+ async setZoom(options) {
643
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
644
+ if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
645
+ throw new Error("camera is not running");
646
+ }
647
+ const stream = video.srcObject;
648
+ const videoTrack = stream.getVideoTracks()[0];
649
+ if (!videoTrack) {
650
+ throw new Error("no video track found");
651
+ }
652
+ const capabilities = videoTrack.getCapabilities();
653
+ if (!capabilities.zoom) {
654
+ throw new Error("zoom not supported by this device");
655
+ }
656
+ const zoomLevel = Math.max(capabilities.zoom.min || 1, Math.min(capabilities.zoom.max || 1, options.level));
657
+ // Note: autoFocus is not supported on web platform
658
+ try {
659
+ await videoTrack.applyConstraints({
660
+ advanced: [{ zoom: zoomLevel }],
661
+ });
662
+ }
663
+ catch (error) {
664
+ throw new Error(`Failed to set zoom: ${error}`);
665
+ }
666
+ }
667
+ async getFlashMode() {
668
+ throw new Error("getFlashMode not supported under the web platform");
669
+ }
670
+ async getDeviceId() {
671
+ return { deviceId: this.currentDeviceId || "" };
672
+ }
673
+ async setDeviceId(options) {
674
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
675
+ if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
676
+ throw new Error("camera is not running");
677
+ }
678
+ // Stop current stream
679
+ this.stopStream(video.srcObject);
680
+ // Update current device ID
681
+ this.currentDeviceId = options.deviceId;
682
+ // Get new constraints with specific device ID
683
+ const constraints = {
684
+ video: {
685
+ deviceId: { exact: options.deviceId },
686
+ width: { ideal: video.videoWidth || 640 },
687
+ height: { ideal: video.videoHeight || 480 },
688
+ },
689
+ };
690
+ try {
691
+ // Try to determine camera position from device
692
+ const devices = await navigator.mediaDevices.enumerateDevices();
693
+ const device = devices.find((d) => d.deviceId === options.deviceId);
694
+ this.isBackCamera =
695
+ (device === null || device === void 0 ? void 0 : device.label.toLowerCase().includes("back")) ||
696
+ (device === null || device === void 0 ? void 0 : device.label.toLowerCase().includes("rear")) ||
697
+ false;
698
+ const stream = await navigator.mediaDevices.getUserMedia(constraints);
699
+ video.srcObject = stream;
700
+ // Update video transform based on camera
701
+ if (this.isBackCamera) {
702
+ video.style.transform = "none";
703
+ video.style.webkitTransform = "none";
704
+ }
705
+ else {
706
+ video.style.transform = "scaleX(-1)";
707
+ video.style.webkitTransform = "scaleX(-1)";
708
+ }
709
+ await video.play();
710
+ }
711
+ catch (error) {
712
+ throw new Error(`Failed to swap to device ${options.deviceId}: ${error}`);
713
+ }
714
+ }
715
+ async getAspectRatio() {
716
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
717
+ if (!video) {
718
+ throw new Error("camera is not running");
719
+ }
720
+ const width = video.offsetWidth;
721
+ const height = video.offsetHeight;
722
+ if (width && height) {
723
+ const ratio = width / height;
724
+ // Check for portrait camera ratios: 4:3 -> 3:4, 16:9 -> 9:16
725
+ if (Math.abs(ratio - 3 / 4) < 0.01) {
726
+ return { aspectRatio: "4:3" };
727
+ }
728
+ if (Math.abs(ratio - 9 / 16) < 0.01) {
729
+ return { aspectRatio: "16:9" };
730
+ }
731
+ }
732
+ // Default to 4:3 if no specific aspect ratio is matched
733
+ return { aspectRatio: "4:3" };
734
+ }
735
+ async setAspectRatio(options) {
736
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
737
+ if (!video) {
738
+ throw new Error("camera is not running");
739
+ }
740
+ if (options.aspectRatio) {
741
+ const [widthRatio, heightRatio] = options.aspectRatio
742
+ .split(":")
743
+ .map(Number);
744
+ // For camera, use portrait orientation: 4:3 becomes 3:4, 16:9 becomes 9:16
745
+ const ratio = heightRatio / widthRatio;
746
+ // Get current position and size
747
+ const rect = video.getBoundingClientRect();
748
+ const currentWidth = rect.width;
749
+ const currentHeight = rect.height;
750
+ const currentRatio = currentWidth / currentHeight;
751
+ let newWidth;
752
+ let newHeight;
753
+ if (currentRatio > ratio) {
754
+ // Width is larger, fit by height and center horizontally
755
+ newWidth = currentHeight * ratio;
756
+ newHeight = currentHeight;
757
+ }
758
+ else {
759
+ // Height is larger, fit by width and center vertically
760
+ newWidth = currentWidth;
761
+ newHeight = currentWidth / ratio;
762
+ }
763
+ // Calculate position
764
+ let x, y;
765
+ if (options.x !== undefined && options.y !== undefined) {
766
+ // Use provided coordinates, ensuring they stay within screen boundaries
767
+ x = Math.max(0, Math.min(options.x, window.innerWidth - newWidth));
768
+ y = Math.max(0, Math.min(options.y, window.innerHeight - newHeight));
769
+ }
770
+ else {
771
+ // Auto-center the view
772
+ x = (window.innerWidth - newWidth) / 2;
773
+ y = (window.innerHeight - newHeight) / 2;
774
+ }
775
+ video.style.width = `${newWidth}px`;
776
+ video.style.height = `${newHeight}px`;
777
+ video.style.left = `${x}px`;
778
+ video.style.top = `${y}px`;
779
+ video.style.position = "absolute";
780
+ const offsetX = newWidth / 8;
781
+ const offsetY = newHeight / 8;
782
+ return {
783
+ width: Math.round(newWidth),
784
+ height: Math.round(newHeight),
785
+ x: Math.round(x + offsetX),
786
+ y: Math.round(y + offsetY),
787
+ };
788
+ }
789
+ else {
790
+ video.style.objectFit = "cover";
791
+ const rect = video.getBoundingClientRect();
792
+ const offsetX = rect.width / 8;
793
+ const offsetY = rect.height / 8;
794
+ return {
795
+ width: Math.round(rect.width),
796
+ height: Math.round(rect.height),
797
+ x: Math.round(rect.left + offsetX),
798
+ y: Math.round(rect.top + offsetY),
799
+ };
800
+ }
801
+ }
802
+ createGridOverlay(gridMode) {
803
+ const overlay = document.createElement("div");
804
+ overlay.style.position = "absolute";
805
+ overlay.style.top = "0";
806
+ overlay.style.left = "0";
807
+ overlay.style.width = "100%";
808
+ overlay.style.height = "100%";
809
+ overlay.style.pointerEvents = "none";
810
+ overlay.style.zIndex = "10";
811
+ const divisions = gridMode === "3x3" ? 3 : 4;
812
+ // Create SVG for grid lines
813
+ const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
814
+ svg.style.width = "100%";
815
+ svg.style.height = "100%";
816
+ svg.style.position = "absolute";
817
+ svg.style.top = "0";
818
+ svg.style.left = "0";
819
+ // Create grid lines
820
+ for (let i = 1; i < divisions; i++) {
821
+ // Vertical lines
822
+ const verticalLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
823
+ verticalLine.setAttribute("x1", `${(i / divisions) * 100}%`);
824
+ verticalLine.setAttribute("y1", "0%");
825
+ verticalLine.setAttribute("x2", `${(i / divisions) * 100}%`);
826
+ verticalLine.setAttribute("y2", "100%");
827
+ verticalLine.setAttribute("stroke", "rgba(255, 255, 255, 0.5)");
828
+ verticalLine.setAttribute("stroke-width", "1");
829
+ svg.appendChild(verticalLine);
830
+ // Horizontal lines
831
+ const horizontalLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
832
+ horizontalLine.setAttribute("x1", "0%");
833
+ horizontalLine.setAttribute("y1", `${(i / divisions) * 100}%`);
834
+ horizontalLine.setAttribute("x2", "100%");
835
+ horizontalLine.setAttribute("y2", `${(i / divisions) * 100}%`);
836
+ horizontalLine.setAttribute("stroke", "rgba(255, 255, 255, 0.5)");
837
+ horizontalLine.setAttribute("stroke-width", "1");
838
+ svg.appendChild(horizontalLine);
839
+ }
840
+ overlay.appendChild(svg);
841
+ return overlay;
842
+ }
843
+ async setGridMode(options) {
844
+ // Web implementation of grid mode would need to be implemented
845
+ // For now, just resolve as a no-op
846
+ console.warn(`Grid mode '${options.gridMode}' is not yet implemented for web platform`);
847
+ }
848
+ async getGridMode() {
849
+ // Web implementation - default to none
850
+ return { gridMode: "none" };
851
+ }
852
+ async getPreviewSize() {
853
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
854
+ if (!video) {
855
+ throw new Error("camera is not running");
856
+ }
857
+ const offsetX = video.width / 8;
858
+ const offsetY = video.height / 8;
859
+ return {
860
+ x: video.offsetLeft + offsetX,
861
+ y: video.offsetTop + offsetY,
862
+ width: video.width,
863
+ height: video.height,
864
+ };
865
+ }
866
+ async setPreviewSize(options) {
867
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
868
+ if (!video) {
869
+ throw new Error("camera is not running");
870
+ }
871
+ video.style.left = `${options.x}px`;
872
+ video.style.top = `${options.y}px`;
873
+ video.width = options.width;
874
+ video.height = options.height;
875
+ const offsetX = options.width / 8;
876
+ const offsetY = options.height / 8;
877
+ return {
878
+ width: options.width,
879
+ height: options.height,
880
+ x: options.x + offsetX,
881
+ y: options.y + offsetY,
882
+ };
883
+ }
884
+ async setFocus(options) {
885
+ // Reject if values are outside 0-1 range
886
+ if (options.x < 0 || options.x > 1 || options.y < 0 || options.y > 1) {
887
+ throw new Error("Focus coordinates must be between 0 and 1");
888
+ }
889
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
890
+ if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
891
+ throw new Error("camera is not running");
892
+ }
893
+ const stream = video.srcObject;
894
+ const videoTrack = stream.getVideoTracks()[0];
895
+ if (!videoTrack) {
896
+ throw new Error("no video track found");
897
+ }
898
+ const capabilities = videoTrack.getCapabilities();
899
+ // Check if focusing is supported
900
+ if (capabilities.focusMode) {
901
+ try {
902
+ // Web API supports focus mode settings but not coordinate-based focus
903
+ // Setting to manual mode allows for coordinate focus if supported
904
+ await videoTrack.applyConstraints({
905
+ advanced: [
906
+ {
907
+ focusMode: "manual",
908
+ focusDistance: 0.5, // Mid-range focus as fallback
909
+ },
910
+ ],
911
+ });
912
+ }
913
+ catch (error) {
914
+ console.warn(`setFocus is not fully supported on this device: ${error}. Focus coordinates (${options.x}, ${options.y}) were provided but cannot be applied.`);
915
+ }
916
+ }
917
+ else {
918
+ console.warn("Focus control is not supported on this device. Focus coordinates were provided but cannot be applied.");
919
+ }
920
+ }
170
921
  }
171
922
 
172
923
  var web = /*#__PURE__*/Object.freeze({