-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathapp.component.ts
95 lines (79 loc) · 2.79 KB
/
app.component.ts
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
95
import {Component, OnInit} from '@angular/core';
import {Observable, Subject} from 'rxjs';
import {WebcamImage} from './modules/webcam/domain/webcam-image';
import {WebcamUtil} from './modules/webcam/util/webcam.util';
import {WebcamInitError} from './modules/webcam/domain/webcam-init-error';
@Component({
selector: 'appRoot',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
// toggle webcam on/off
public showWebcam = true;
public allowCameraSwitch = true;
public multipleWebcamsAvailable = false;
public deviceId: string;
public facingMode: string = 'environment';
public messages: any[] = [];
// latest snapshot
public webcamImage: WebcamImage = null;
// webcam snapshot trigger
private trigger: Subject<void> = new Subject<void>();
// switch to next / previous / specific webcam; true/false: forward/backwards, string: deviceId
private nextWebcam: Subject<boolean|string> = new Subject<boolean|string>();
public ngOnInit(): void {
this.readAvailableVideoInputs();
}
public triggerSnapshot(): void {
this.trigger.next();
}
public toggleWebcam(): void {
this.showWebcam = !this.showWebcam;
}
public handleInitError(error: WebcamInitError): void {
this.messages.push(error);
if (error.mediaStreamError && error.mediaStreamError.name === 'NotAllowedError') {
this.addMessage('User denied camera access');
}
}
public showNextWebcam(directionOrDeviceId: boolean|string): void {
// true => move forward through devices
// false => move backwards through devices
// string => move to device with given deviceId
this.nextWebcam.next(directionOrDeviceId);
}
public handleImage(webcamImage: WebcamImage): void {
this.addMessage('Received webcam image');
console.log(webcamImage);
this.webcamImage = webcamImage;
}
public cameraWasSwitched(deviceId: string): void {
this.addMessage('Active device: ' + deviceId);
this.deviceId = deviceId;
this.readAvailableVideoInputs();
}
addMessage(message: any): void {
console.log(message);
this.messages.unshift(message);
}
public get triggerObservable(): Observable<void> {
return this.trigger.asObservable();
}
public get nextWebcamObservable(): Observable<boolean|string> {
return this.nextWebcam.asObservable();
}
public get videoOptions(): MediaTrackConstraints {
const result: MediaTrackConstraints = {};
if (this.facingMode && this.facingMode !== '') {
result.facingMode = { ideal: this.facingMode };
}
return result;
}
private readAvailableVideoInputs() {
WebcamUtil.getAvailableVideoInputs()
.then((mediaDevices: MediaDeviceInfo[]) => {
this.multipleWebcamsAvailable = mediaDevices && mediaDevices.length > 1;
});
}
}