-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebcam.js
More file actions
48 lines (44 loc) · 1.04 KB
/
webcam.js
File metadata and controls
48 lines (44 loc) · 1.04 KB
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
export async function askWebcamPermissions() {
let stream;
try {
let constraints = { video: true, audio: false }; //ask for camera and microphone permission
stream = await navigator.mediaDevices.getUserMedia(constraints);
} catch (error) {
alert(error);
}
closeStream(stream);
}
export function closeStream(stream){
try{
if (stream){
stream.getTracks().forEach(track => track.stop());
}
} catch (e){
alert(e.message);
}
}
export async function captureCamera(video, hasMicrophone, callback) {
let constraints = {
audio: true,
video: true
}
const devices = [0];
let device = devices[0];
let deviceID = device.deviceId;
console.log(deviceID);
constraints = {
video: {
deviceId: deviceID,
width: 1280,
height: 720,
},
audio: false
}
try {
const camera = await navigator.mediaDevices.getUserMedia(constraints);
video.srcObject = camera;
} catch (error) {
alert('Unable to capture your camera. Please check console logs.');
console.error(error);
}
}