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