-
Notifications
You must be signed in to change notification settings - Fork 0
/
startPage.js
94 lines (82 loc) · 3.24 KB
/
startPage.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
let currentStream;
async function requestCameraPermission() {
const permissionStatus = await navigator.permissions.query({ name: 'camera' });
if (permissionStatus.state === 'granted') {
// Permission already granted, proceed with displaying the camera stream
displayCameraList();
displayVideoStream();
} else if (permissionStatus.state === 'prompt') {
// Permission not yet granted, show the permission prompt and wait for the user's response
const permissionResult = await navigator.permissions.query({ name: 'camera' });
if (permissionResult.state === 'granted') {
// Permission granted, proceed with displaying the camera stream
displayCameraList();
displayVideoStream();
} else {
// Permission denied, show an error message
alert('You must grant camera permission in order to use this app.');
}
} else {
// Permission denied, show an error message
alert('You must grant camera permission in order to use this app.');
}
}
function displayCameraList() {
// Get a list of all connected devices that are webcams
navigator.mediaDevices.enumerateDevices()
.then(devices => {
const webcams = devices.filter(device => device.kind === 'videoinput');
// Display the list of webcams
const webcamList = document.getElementById('webcam-list');
webcams.forEach(webcams => {
const option = document.createElement('option');
option.value = webcams.deviceId;
option.text = webcams.label;
webcamList.appendChild(option);
});
});
}
function displayVideoStream() {
// Get the selected camera device
const selectedDeviceId = document.getElementById('webcam-list').value;
// Request access to the camera device
navigator.mediaDevices.getUserMedia({ video: { deviceId: selectedDeviceId } })
.then(stream => {
// Get the video element
const videoElement = document.getElementById('video-element');
// Display the video stream in the video element
videoElement.srcObject = stream;
videoElement.play();
})
.catch(error => {
console.error(error);
});
storeCurrentCamera(selectedDeviceId);
}
function storeCurrentCamera(currentCam) {
localStorage.setItem('cam-ID', currentCam);
}
function nextPageButton() {
stopCam();
window.location.href='translate.html'
}
function stopCam() {
// Get the video element
const videoElement = document.getElementById('video-element');
// Display the video stream in the video element
let stream = videoElement.srcObject
stream.getTracks().forEach(track => {
track.stop();
});
}
function loadPage() {
document.getElementById('loading-screen').style.display = 'block';
setTimeout(function() {
window.location.href = 'page2.html';
}, 2000);
}
requestCameraPermission();
const webcamList = document.getElementById('webcam-list');
webcamList.addEventListener('change', displayVideoStream);
const startButton = document.getElementById('start');
startButton.addEventListener('click', nextPageButton);