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