-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.js
44 lines (36 loc) · 1.39 KB
/
camera.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const initializeCamera = async () => {
const cameraContainer = document.querySelector('#camera');
if (!cameraContainer) {
console.error('Camera container element not found');
return;
}
const cameraPermission = await navigator.permissions.query({ name: 'camera' });
if (cameraPermission.state === 'prompt') {
try {
await navigator.mediaDevices.getUserMedia({ audio: true, video: true });
alert('Camera is now active.');
} catch (error) {
console.error('Error accessing the camera:', error);
alert('Unable to activate the camera.');
return;
}
}
if (cameraPermission.state === 'denied') {
alert('Camera permission denied.');
return;
}
const activateCamera = async () => {
try {
const videoElement = document.createElement('video');
videoElement.setAttribute('autoplay', 'true');
videoElement.style.cssText = 'height: 200px; transform: scaleX(-1);';
cameraContainer.appendChild(videoElement);
const cameraStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
videoElement.srcObject = cameraStream;
} catch (error) {
console.error('Error starting the camera:', error);
}
};
await activateCamera();
};
initializeCamera();