Skip to content

Commit a6ffb58

Browse files
committedAug 5, 2023
Improve camera start/switch experience
1 parent 1fadd91 commit a6ffb58

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed
 

Diff for: ‎.gitpod.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This configuration file was automatically generated by Gitpod.
2+
# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml)
3+
# and commit this file to your remote git repository to share the goodness with others.
4+
5+
# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart
6+
7+
tasks:
8+
- init: npm install && npm run build
9+
command: npm run start
10+
11+

Diff for: ‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.0",
44
"scripts": {
55
"ng": "ng",
6-
"start": "ng serve",
6+
"start": "ng serve --host 0.0.0.0 --port 4200 --disable-host-check true",
77
"build": "ng build",
88
"watch": "ng build --watch --configuration development",
99
"test": "ng test"

Diff for: ‎src/app/document-capture/document-capture.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<div>
22
<video #video autoplay></video>
33
<canvas #canvas [width]="video.videoWidth" [height]="video.videoHeight"></canvas>
4-
<button (click)="startCamera()">Start Camera</button>
54
<button (click)="changeCamera(device)" *ngFor="let device of videoDevices">
65
{{ device.label || 'Camera ' + (device.deviceId || '') }}
76
</button>
@@ -17,5 +16,6 @@ <h2>Results:</h2>
1716
<p>Source Text: {{ sourceText }}</p>
1817
<p>Category: {{ category }}</p>
1918
<p>Tags: {{ tags.join(', ') }}</p>
19+
<pre>{{ videoDevices | json }}</pre>
2020
</div>
2121

Diff for: ‎src/app/document-capture/document-capture.component.ts

+15-19
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export class DocumentCaptureComponent implements AfterViewInit {
1717
tags: string[] = [];
1818
sourceText!: string;
1919
videoDevices!: MediaDeviceInfo[];
20-
selectedVideoDevice!: MediaDeviceInfo | null;
2120

2221
constructor() {
2322
this.captures = [];
@@ -26,36 +25,33 @@ export class DocumentCaptureComponent implements AfterViewInit {
2625
ngAfterViewInit() {
2726
this.video = this.videoElement.nativeElement;
2827
this.canvas = this.canvasElement.nativeElement;
28+
this.getCameraList();
2929
}
3030

31-
startCamera() {
31+
getCameraList() {
3232
navigator.mediaDevices.enumerateDevices().then(devices => {
33+
console.log(devices)
3334
this.videoDevices = devices.filter(device => device.kind === 'videoinput');
34-
if (this.videoDevices.length > 0) {
35-
this.selectedVideoDevice = this.videoDevices[0]; // Select the first available video device by default
36-
this.startMediaStream();
37-
} else {
35+
if (this.videoDevices?.length == 0) {
3836
console.error('No video devices found.');
3937
}
4038
});
4139
}
4240

4341
changeCamera(device: MediaDeviceInfo) {
44-
this.selectedVideoDevice = device;
45-
this.startMediaStream();
42+
this.startMediaStream(device);
4643
}
4744

48-
startMediaStream() {
49-
if (this.selectedVideoDevice) {
50-
navigator.mediaDevices.getUserMedia({ video: { deviceId: this.selectedVideoDevice.deviceId } })
51-
.then(stream => {
52-
this.video.srcObject = stream;
53-
this.video.play();
54-
})
55-
.catch(err => {
56-
console.error('Error accessing video stream:', err);
57-
});
58-
}
45+
startMediaStream(device: MediaDeviceInfo) {
46+
this.video.load();
47+
navigator.mediaDevices.getUserMedia({ video: { deviceId: device.deviceId } })
48+
.then(stream => {
49+
this.video.srcObject = stream;
50+
this.video.play();
51+
})
52+
.catch(err => {
53+
console.error('Error accessing video stream:', err);
54+
});
5955
}
6056

6157
capture() {

0 commit comments

Comments
 (0)
Please sign in to comment.