@capgo/camera-preview 7.3.12 → 7.4.0-beta.10

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 (61) hide show
  1. package/CapgoCameraPreview.podspec +16 -13
  2. package/README.md +467 -73
  3. package/android/.gradle/8.14.2/checksums/checksums.lock +0 -0
  4. package/android/.gradle/8.14.2/checksums/md5-checksums.bin +0 -0
  5. package/android/.gradle/8.14.2/checksums/sha1-checksums.bin +0 -0
  6. package/android/.gradle/8.14.2/executionHistory/executionHistory.bin +0 -0
  7. package/android/.gradle/8.14.2/executionHistory/executionHistory.lock +0 -0
  8. package/android/.gradle/8.14.2/fileChanges/last-build.bin +0 -0
  9. package/android/.gradle/8.14.2/fileHashes/fileHashes.bin +0 -0
  10. package/android/.gradle/8.14.2/fileHashes/fileHashes.lock +0 -0
  11. package/android/.gradle/8.14.2/fileHashes/resourceHashesCache.bin +0 -0
  12. package/android/.gradle/8.14.2/gc.properties +0 -0
  13. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  14. package/android/.gradle/buildOutputCleanup/cache.properties +2 -0
  15. package/android/.gradle/buildOutputCleanup/outputFiles.bin +0 -0
  16. package/android/.gradle/file-system.probe +0 -0
  17. package/android/.gradle/vcs-1/gc.properties +0 -0
  18. package/android/build.gradle +11 -0
  19. package/android/gradle/wrapper/gradle-wrapper.properties +1 -1
  20. package/android/src/main/AndroidManifest.xml +5 -3
  21. package/android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java +472 -541
  22. package/android/src/main/java/com/ahm/capacitor/camera/preview/CameraXView.java +1648 -0
  23. package/android/src/main/java/com/ahm/capacitor/camera/preview/GridOverlayView.java +82 -0
  24. package/android/src/main/java/com/ahm/capacitor/camera/preview/model/CameraDevice.java +54 -0
  25. package/android/src/main/java/com/ahm/capacitor/camera/preview/model/CameraLens.java +70 -0
  26. package/android/src/main/java/com/ahm/capacitor/camera/preview/model/CameraSessionConfiguration.java +79 -0
  27. package/android/src/main/java/com/ahm/capacitor/camera/preview/model/LensInfo.java +34 -0
  28. package/android/src/main/java/com/ahm/capacitor/camera/preview/model/ZoomFactors.java +34 -0
  29. package/dist/docs.json +934 -154
  30. package/dist/esm/definitions.d.ts +445 -83
  31. package/dist/esm/definitions.js +10 -1
  32. package/dist/esm/definitions.js.map +1 -1
  33. package/dist/esm/web.d.ts +73 -3
  34. package/dist/esm/web.js +492 -68
  35. package/dist/esm/web.js.map +1 -1
  36. package/dist/plugin.cjs.js +498 -68
  37. package/dist/plugin.cjs.js.map +1 -1
  38. package/dist/plugin.js +498 -68
  39. package/dist/plugin.js.map +1 -1
  40. package/ios/{Plugin → Sources/CapgoCameraPreview}/CameraController.swift +601 -59
  41. package/ios/Sources/CapgoCameraPreview/GridOverlayView.swift +65 -0
  42. package/ios/Sources/CapgoCameraPreview/Plugin.swift +1369 -0
  43. package/ios/Tests/CameraPreviewPluginTests/CameraPreviewPluginTests.swift +15 -0
  44. package/package.json +1 -1
  45. package/android/src/main/java/com/ahm/capacitor/camera/preview/CameraActivity.java +0 -1279
  46. package/android/src/main/java/com/ahm/capacitor/camera/preview/CustomSurfaceView.java +0 -29
  47. package/android/src/main/java/com/ahm/capacitor/camera/preview/CustomTextureView.java +0 -39
  48. package/android/src/main/java/com/ahm/capacitor/camera/preview/Preview.java +0 -461
  49. package/android/src/main/java/com/ahm/capacitor/camera/preview/TapGestureDetector.java +0 -24
  50. package/ios/Plugin/Info.plist +0 -24
  51. package/ios/Plugin/Plugin.h +0 -10
  52. package/ios/Plugin/Plugin.m +0 -18
  53. package/ios/Plugin/Plugin.swift +0 -511
  54. package/ios/Plugin.xcodeproj/project.pbxproj +0 -593
  55. package/ios/Plugin.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  56. package/ios/Plugin.xcworkspace/contents.xcworkspacedata +0 -10
  57. package/ios/Plugin.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  58. package/ios/PluginTests/Info.plist +0 -22
  59. package/ios/PluginTests/PluginTests.swift +0 -83
  60. package/ios/Podfile +0 -13
  61. 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,101 @@ 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;
67
- }
68
- else {
69
- this.isBackCamera = false;
70
- }
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({});
78
- }
79
- else {
80
- self.stopStream(stream);
81
- Promise.reject(new Error("camera already stopped"));
82
- }
83
- }, (err) => {
84
- Promise.reject(new Error(err));
85
- });
46
+ const gridMode = (options === null || options === void 0 ? void 0 : options.gridMode) || "none";
47
+ if (options.position) {
48
+ this.isBackCamera = options.position === "rear";
49
+ }
50
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
51
+ if (video) {
52
+ video.remove();
53
+ }
54
+ const container = options.parent ? document.getElementById(options.parent) : document.body;
55
+ if (!container) {
56
+ throw new Error("container not found");
57
+ }
58
+ this.videoElement = document.createElement("video");
59
+ this.videoElement.id = DEFAULT_VIDEO_ID;
60
+ this.videoElement.className = options.className || "";
61
+ this.videoElement.playsInline = true;
62
+ this.videoElement.muted = true;
63
+ this.videoElement.autoplay = true;
64
+ container.appendChild(this.videoElement);
65
+ if (options.toBack) {
66
+ this.videoElement.style.zIndex = "-1";
67
+ }
68
+ if (options.width) {
69
+ this.videoElement.width = options.width;
70
+ }
71
+ if (options.height) {
72
+ this.videoElement.height = options.height;
73
+ }
74
+ if (options.x) {
75
+ this.videoElement.style.left = `${options.x}px`;
76
+ }
77
+ // Create and add grid overlay if needed
78
+ if (gridMode !== "none") {
79
+ const gridOverlay = this.createGridOverlay(gridMode);
80
+ gridOverlay.id = "camera-grid-overlay";
81
+ parent === null || parent === void 0 ? void 0 : parent.appendChild(gridOverlay);
82
+ }
83
+ if (options.y) {
84
+ this.videoElement.style.top = `${options.y}px`;
85
+ }
86
+ if (options.aspectRatio) {
87
+ const [widthRatio, heightRatio] = options.aspectRatio.split(':').map(Number);
88
+ const ratio = widthRatio / heightRatio;
89
+ if (options.width) {
90
+ this.videoElement.height = options.width / ratio;
91
+ }
92
+ else if (options.height) {
93
+ this.videoElement.width = options.height * ratio;
86
94
  }
87
95
  }
88
96
  else {
89
- Promise.reject(new Error("camera already started"));
97
+ this.videoElement.style.objectFit = 'cover';
98
+ }
99
+ const constraints = {
100
+ video: {
101
+ width: { ideal: this.videoElement.width || 640 },
102
+ height: { ideal: this.videoElement.height || window.innerHeight },
103
+ facingMode: this.isBackCamera ? "environment" : "user",
104
+ },
105
+ };
106
+ const stream = await navigator.mediaDevices.getUserMedia(constraints);
107
+ if (!stream) {
108
+ throw new Error("could not acquire stream");
109
+ }
110
+ if (!this.videoElement) {
111
+ throw new Error("video element not found");
112
+ }
113
+ this.videoElement.srcObject = stream;
114
+ if (!this.isBackCamera) {
115
+ this.videoElement.style.transform = "scaleX(-1)";
90
116
  }
117
+ this.isStarted = true;
118
+ return {
119
+ width: this.videoElement.width,
120
+ height: this.videoElement.height,
121
+ x: this.videoElement.getBoundingClientRect().x,
122
+ y: this.videoElement.getBoundingClientRect().y,
123
+ };
91
124
  }
92
125
  stopStream(stream) {
93
126
  if (stream) {
@@ -97,16 +130,20 @@ class CameraPreviewWeb extends core.WebPlugin {
97
130
  }
98
131
  }
99
132
  async stop() {
100
- const video = document.getElementById("video");
133
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
101
134
  if (video) {
102
135
  video.pause();
103
136
  this.stopStream(video.srcObject);
104
137
  video.remove();
138
+ this.isStarted = false;
105
139
  }
140
+ // Remove grid overlay if it exists
141
+ const gridOverlay = document.getElementById("camera-grid-overlay");
142
+ gridOverlay === null || gridOverlay === void 0 ? void 0 : gridOverlay.remove();
106
143
  }
107
144
  async capture(options) {
108
145
  return new Promise((resolve, reject) => {
109
- const video = document.getElementById("video");
146
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
110
147
  if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
111
148
  reject(new Error("camera is not running"));
112
149
  return;
@@ -124,6 +161,8 @@ class CameraPreviewWeb extends core.WebPlugin {
124
161
  context === null || context === void 0 ? void 0 : context.scale(-1, 1);
125
162
  }
126
163
  context === null || context === void 0 ? void 0 : context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
164
+ if (options.saveToGallery) ;
165
+ if (options.withExifLocation) ;
127
166
  if ((options.format || "jpeg") === "jpeg") {
128
167
  base64EncodedImage = canvas
129
168
  .toDataURL("image/jpeg", (options.quality || 85) / 100.0)
@@ -137,6 +176,7 @@ class CameraPreviewWeb extends core.WebPlugin {
137
176
  }
138
177
  resolve({
139
178
  value: base64EncodedImage,
179
+ exif: {},
140
180
  });
141
181
  });
142
182
  }
@@ -160,13 +200,403 @@ class CameraPreviewWeb extends core.WebPlugin {
160
200
  throw new Error(`setFlashMode not supported under the web platform${_options}`);
161
201
  }
162
202
  async flip() {
163
- throw new Error("flip not supported under the web platform");
203
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
204
+ if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
205
+ throw new Error("camera is not running");
206
+ }
207
+ // Stop current stream
208
+ this.stopStream(video.srcObject);
209
+ // Toggle camera position
210
+ this.isBackCamera = !this.isBackCamera;
211
+ // Get new constraints
212
+ const constraints = {
213
+ video: {
214
+ facingMode: this.isBackCamera ? "environment" : "user",
215
+ width: { ideal: video.videoWidth || 640 },
216
+ height: { ideal: video.videoHeight || 480 },
217
+ },
218
+ };
219
+ try {
220
+ const stream = await navigator.mediaDevices.getUserMedia(constraints);
221
+ video.srcObject = stream;
222
+ // Update current device ID from the new stream
223
+ const videoTrack = stream.getVideoTracks()[0];
224
+ if (videoTrack) {
225
+ this.currentDeviceId = videoTrack.getSettings().deviceId || null;
226
+ }
227
+ // Update video transform based on camera
228
+ if (this.isBackCamera) {
229
+ video.style.transform = "none";
230
+ video.style.webkitTransform = "none";
231
+ }
232
+ else {
233
+ video.style.transform = "scaleX(-1)";
234
+ video.style.webkitTransform = "scaleX(-1)";
235
+ }
236
+ await video.play();
237
+ }
238
+ catch (error) {
239
+ throw new Error(`Failed to flip camera: ${error}`);
240
+ }
164
241
  }
165
242
  async setOpacity(_options) {
166
- const video = document.getElementById("video");
243
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
167
244
  if (!!video && !!_options.opacity)
168
245
  video.style.setProperty("opacity", _options.opacity.toString());
169
246
  }
247
+ async isRunning() {
248
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
249
+ return { isRunning: !!video && !!video.srcObject };
250
+ }
251
+ async getAvailableDevices() {
252
+ var _a;
253
+ if (!((_a = navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.enumerateDevices)) {
254
+ throw new Error("getAvailableDevices not supported under the web platform");
255
+ }
256
+ const devices = await navigator.mediaDevices.enumerateDevices();
257
+ const videoDevices = devices.filter(device => device.kind === 'videoinput');
258
+ // Group devices by position (front/back)
259
+ const frontDevices = [];
260
+ const backDevices = [];
261
+ videoDevices.forEach((device, index) => {
262
+ const label = device.label || `Camera ${index + 1}`;
263
+ const labelLower = label.toLowerCase();
264
+ // Determine device type based on label
265
+ let deviceType = exports.DeviceType.WIDE_ANGLE;
266
+ let baseZoomRatio = 1.0;
267
+ if (labelLower.includes('ultra') || labelLower.includes('0.5')) {
268
+ deviceType = exports.DeviceType.ULTRA_WIDE;
269
+ baseZoomRatio = 0.5;
270
+ }
271
+ else if (labelLower.includes('telephoto') || labelLower.includes('tele') || labelLower.includes('2x') || labelLower.includes('3x')) {
272
+ deviceType = exports.DeviceType.TELEPHOTO;
273
+ baseZoomRatio = 2.0;
274
+ }
275
+ else if (labelLower.includes('depth') || labelLower.includes('truedepth')) {
276
+ deviceType = exports.DeviceType.TRUE_DEPTH;
277
+ baseZoomRatio = 1.0;
278
+ }
279
+ const lensInfo = {
280
+ deviceId: device.deviceId,
281
+ label,
282
+ deviceType,
283
+ focalLength: 4.25,
284
+ baseZoomRatio,
285
+ minZoom: 1.0,
286
+ maxZoom: 1.0
287
+ };
288
+ // Determine position and add to appropriate array
289
+ if (labelLower.includes('back') || labelLower.includes('rear')) {
290
+ backDevices.push(lensInfo);
291
+ }
292
+ else {
293
+ frontDevices.push(lensInfo);
294
+ }
295
+ });
296
+ const result = [];
297
+ if (frontDevices.length > 0) {
298
+ result.push({
299
+ deviceId: frontDevices[0].deviceId,
300
+ label: "Front Camera",
301
+ position: "front",
302
+ lenses: frontDevices,
303
+ isLogical: false,
304
+ minZoom: Math.min(...frontDevices.map(d => d.minZoom)),
305
+ maxZoom: Math.max(...frontDevices.map(d => d.maxZoom))
306
+ });
307
+ }
308
+ if (backDevices.length > 0) {
309
+ result.push({
310
+ deviceId: backDevices[0].deviceId,
311
+ label: "Back Camera",
312
+ position: "rear",
313
+ lenses: backDevices,
314
+ isLogical: false,
315
+ minZoom: Math.min(...backDevices.map(d => d.minZoom)),
316
+ maxZoom: Math.max(...backDevices.map(d => d.maxZoom))
317
+ });
318
+ }
319
+ return { devices: result };
320
+ }
321
+ async getZoom() {
322
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
323
+ if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
324
+ throw new Error("camera is not running");
325
+ }
326
+ const stream = video.srcObject;
327
+ const videoTrack = stream.getVideoTracks()[0];
328
+ if (!videoTrack) {
329
+ throw new Error("no video track found");
330
+ }
331
+ const capabilities = videoTrack.getCapabilities();
332
+ const settings = videoTrack.getSettings();
333
+ if (!capabilities.zoom) {
334
+ throw new Error("zoom not supported by this device");
335
+ }
336
+ // Get current device info to determine lens type
337
+ let deviceType = exports.DeviceType.WIDE_ANGLE;
338
+ let baseZoomRatio = 1.0;
339
+ if (this.currentDeviceId) {
340
+ const devices = await navigator.mediaDevices.enumerateDevices();
341
+ const device = devices.find(d => d.deviceId === this.currentDeviceId);
342
+ if (device) {
343
+ const labelLower = device.label.toLowerCase();
344
+ if (labelLower.includes('ultra') || labelLower.includes('0.5')) {
345
+ deviceType = exports.DeviceType.ULTRA_WIDE;
346
+ baseZoomRatio = 0.5;
347
+ }
348
+ else if (labelLower.includes('telephoto') || labelLower.includes('tele') || labelLower.includes('2x') || labelLower.includes('3x')) {
349
+ deviceType = exports.DeviceType.TELEPHOTO;
350
+ baseZoomRatio = 2.0;
351
+ }
352
+ else if (labelLower.includes('depth') || labelLower.includes('truedepth')) {
353
+ deviceType = exports.DeviceType.TRUE_DEPTH;
354
+ baseZoomRatio = 1.0;
355
+ }
356
+ }
357
+ }
358
+ const currentZoom = settings.zoom || 1;
359
+ const lensInfo = {
360
+ focalLength: 4.25,
361
+ deviceType,
362
+ baseZoomRatio,
363
+ digitalZoom: currentZoom / baseZoomRatio
364
+ };
365
+ return {
366
+ min: capabilities.zoom.min || 1,
367
+ max: capabilities.zoom.max || 1,
368
+ current: currentZoom,
369
+ lens: lensInfo,
370
+ };
371
+ }
372
+ async setZoom(options) {
373
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
374
+ if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
375
+ throw new Error("camera is not running");
376
+ }
377
+ const stream = video.srcObject;
378
+ const videoTrack = stream.getVideoTracks()[0];
379
+ if (!videoTrack) {
380
+ throw new Error("no video track found");
381
+ }
382
+ const capabilities = videoTrack.getCapabilities();
383
+ if (!capabilities.zoom) {
384
+ throw new Error("zoom not supported by this device");
385
+ }
386
+ const zoomLevel = Math.max(capabilities.zoom.min || 1, Math.min(capabilities.zoom.max || 1, options.level));
387
+ try {
388
+ await videoTrack.applyConstraints({
389
+ advanced: [{ zoom: zoomLevel }]
390
+ });
391
+ }
392
+ catch (error) {
393
+ throw new Error(`Failed to set zoom: ${error}`);
394
+ }
395
+ }
396
+ async getFlashMode() {
397
+ throw new Error("getFlashMode not supported under the web platform");
398
+ }
399
+ async getDeviceId() {
400
+ return { deviceId: this.currentDeviceId || "" };
401
+ }
402
+ async setDeviceId(options) {
403
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
404
+ if (!(video === null || video === void 0 ? void 0 : video.srcObject)) {
405
+ throw new Error("camera is not running");
406
+ }
407
+ // Stop current stream
408
+ this.stopStream(video.srcObject);
409
+ // Update current device ID
410
+ this.currentDeviceId = options.deviceId;
411
+ // Get new constraints with specific device ID
412
+ const constraints = {
413
+ video: {
414
+ deviceId: { exact: options.deviceId },
415
+ width: { ideal: video.videoWidth || 640 },
416
+ height: { ideal: video.videoHeight || 480 },
417
+ },
418
+ };
419
+ try {
420
+ // Try to determine camera position from device
421
+ const devices = await navigator.mediaDevices.enumerateDevices();
422
+ const device = devices.find(d => d.deviceId === options.deviceId);
423
+ this.isBackCamera = (device === null || device === void 0 ? void 0 : device.label.toLowerCase().includes('back')) || (device === null || device === void 0 ? void 0 : device.label.toLowerCase().includes('rear')) || false;
424
+ const stream = await navigator.mediaDevices.getUserMedia(constraints);
425
+ video.srcObject = stream;
426
+ // Update video transform based on camera
427
+ if (this.isBackCamera) {
428
+ video.style.transform = "none";
429
+ video.style.webkitTransform = "none";
430
+ }
431
+ else {
432
+ video.style.transform = "scaleX(-1)";
433
+ video.style.webkitTransform = "scaleX(-1)";
434
+ }
435
+ await video.play();
436
+ }
437
+ catch (error) {
438
+ throw new Error(`Failed to swap to device ${options.deviceId}: ${error}`);
439
+ }
440
+ }
441
+ async getAspectRatio() {
442
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
443
+ if (!video) {
444
+ throw new Error("camera is not running");
445
+ }
446
+ const width = video.offsetWidth;
447
+ const height = video.offsetHeight;
448
+ if (width && height) {
449
+ const ratio = width / height;
450
+ // Check for portrait camera ratios: 4:3 -> 3:4, 16:9 -> 9:16
451
+ if (Math.abs(ratio - (3 / 4)) < 0.01) {
452
+ return { aspectRatio: '4:3' };
453
+ }
454
+ if (Math.abs(ratio - (9 / 16)) < 0.01) {
455
+ return { aspectRatio: '16:9' };
456
+ }
457
+ }
458
+ // Default to 4:3 if no specific aspect ratio is matched
459
+ return { aspectRatio: '4:3' };
460
+ }
461
+ async setAspectRatio(options) {
462
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
463
+ if (!video) {
464
+ throw new Error("camera is not running");
465
+ }
466
+ if (options.aspectRatio) {
467
+ const [widthRatio, heightRatio] = options.aspectRatio.split(':').map(Number);
468
+ // For camera, use portrait orientation: 4:3 becomes 3:4, 16:9 becomes 9:16
469
+ const ratio = heightRatio / widthRatio;
470
+ // Get current position and size
471
+ const rect = video.getBoundingClientRect();
472
+ const currentWidth = rect.width;
473
+ const currentHeight = rect.height;
474
+ const currentRatio = currentWidth / currentHeight;
475
+ let newWidth;
476
+ let newHeight;
477
+ if (currentRatio > ratio) {
478
+ // Width is larger, fit by height and center horizontally
479
+ newWidth = currentHeight * ratio;
480
+ newHeight = currentHeight;
481
+ }
482
+ else {
483
+ // Height is larger, fit by width and center vertically
484
+ newWidth = currentWidth;
485
+ newHeight = currentWidth / ratio;
486
+ }
487
+ // Calculate position
488
+ let x, y;
489
+ if (options.x !== undefined && options.y !== undefined) {
490
+ // Use provided coordinates, ensuring they stay within screen boundaries
491
+ x = Math.max(0, Math.min(options.x, window.innerWidth - newWidth));
492
+ y = Math.max(0, Math.min(options.y, window.innerHeight - newHeight));
493
+ }
494
+ else {
495
+ // Auto-center the view
496
+ x = (window.innerWidth - newWidth) / 2;
497
+ y = (window.innerHeight - newHeight) / 2;
498
+ }
499
+ video.style.width = `${newWidth}px`;
500
+ video.style.height = `${newHeight}px`;
501
+ video.style.left = `${x}px`;
502
+ video.style.top = `${y}px`;
503
+ video.style.position = 'absolute';
504
+ return {
505
+ width: Math.round(newWidth),
506
+ height: Math.round(newHeight),
507
+ x: Math.round(x),
508
+ y: Math.round(y)
509
+ };
510
+ }
511
+ else {
512
+ video.style.objectFit = 'cover';
513
+ const rect = video.getBoundingClientRect();
514
+ return {
515
+ width: Math.round(rect.width),
516
+ height: Math.round(rect.height),
517
+ x: Math.round(rect.left),
518
+ y: Math.round(rect.top)
519
+ };
520
+ }
521
+ }
522
+ createGridOverlay(gridMode) {
523
+ const overlay = document.createElement("div");
524
+ overlay.style.position = "absolute";
525
+ overlay.style.top = "0";
526
+ overlay.style.left = "0";
527
+ overlay.style.width = "100%";
528
+ overlay.style.height = "100%";
529
+ overlay.style.pointerEvents = "none";
530
+ overlay.style.zIndex = "10";
531
+ const divisions = gridMode === "3x3" ? 3 : 4;
532
+ // Create SVG for grid lines
533
+ const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
534
+ svg.style.width = "100%";
535
+ svg.style.height = "100%";
536
+ svg.style.position = "absolute";
537
+ svg.style.top = "0";
538
+ svg.style.left = "0";
539
+ // Create grid lines
540
+ for (let i = 1; i < divisions; i++) {
541
+ // Vertical lines
542
+ const verticalLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
543
+ verticalLine.setAttribute("x1", `${(i / divisions) * 100}%`);
544
+ verticalLine.setAttribute("y1", "0%");
545
+ verticalLine.setAttribute("x2", `${(i / divisions) * 100}%`);
546
+ verticalLine.setAttribute("y2", "100%");
547
+ verticalLine.setAttribute("stroke", "rgba(255, 255, 255, 0.5)");
548
+ verticalLine.setAttribute("stroke-width", "1");
549
+ svg.appendChild(verticalLine);
550
+ // Horizontal lines
551
+ const horizontalLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
552
+ horizontalLine.setAttribute("x1", "0%");
553
+ horizontalLine.setAttribute("y1", `${(i / divisions) * 100}%`);
554
+ horizontalLine.setAttribute("x2", "100%");
555
+ horizontalLine.setAttribute("y2", `${(i / divisions) * 100}%`);
556
+ horizontalLine.setAttribute("stroke", "rgba(255, 255, 255, 0.5)");
557
+ horizontalLine.setAttribute("stroke-width", "1");
558
+ svg.appendChild(horizontalLine);
559
+ }
560
+ overlay.appendChild(svg);
561
+ return overlay;
562
+ }
563
+ async setGridMode(options) {
564
+ // Web implementation of grid mode would need to be implemented
565
+ // For now, just resolve as a no-op
566
+ console.warn(`Grid mode '${options.gridMode}' is not yet implemented for web platform`);
567
+ }
568
+ async getGridMode() {
569
+ // Web implementation - default to none
570
+ return { gridMode: 'none' };
571
+ }
572
+ async getPreviewSize() {
573
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
574
+ if (!video) {
575
+ throw new Error("camera is not running");
576
+ }
577
+ return {
578
+ x: video.offsetLeft,
579
+ y: video.offsetTop,
580
+ width: video.width,
581
+ height: video.height
582
+ };
583
+ }
584
+ async setPreviewSize(options) {
585
+ const video = document.getElementById(DEFAULT_VIDEO_ID);
586
+ if (!video) {
587
+ throw new Error("camera is not running");
588
+ }
589
+ video.style.left = `${options.x}px`;
590
+ video.style.top = `${options.y}px`;
591
+ video.width = options.width;
592
+ video.height = options.height;
593
+ return {
594
+ width: options.width,
595
+ height: options.height,
596
+ x: options.x,
597
+ y: options.y
598
+ };
599
+ }
170
600
  }
171
601
 
172
602
  var web = /*#__PURE__*/Object.freeze({