1
- import { Component , OnInit , ViewChild , ElementRef , Renderer2 , Output , EventEmitter } from '@angular/core' ;
1
+ import { Component , OnInit , ViewChild , ElementRef , Renderer2 , Output , EventEmitter , AfterViewInit } from '@angular/core' ;
2
2
import { BehaviorSubject } from 'rxjs' ;
3
3
4
4
@Component ( {
5
5
selector : 'app-photo-capture' ,
6
6
templateUrl : './photo-capture.component.html' ,
7
7
styleUrls : [ './photo-capture.component.scss' ]
8
8
} )
9
- export class PhotoCaptureComponent implements OnInit {
10
- @ViewChild ( 'video' , { static : true } ) videoElement : ElementRef ;
11
- @ViewChild ( 'canvas' , { static : true } ) canvas : ElementRef ;
12
- @Output ( ) imageData = new EventEmitter < string > ( ) ;
9
+ export class PhotoCaptureComponent implements AfterViewInit {
10
+ @ViewChild ( 'video' , { static : false } ) videoElement : ElementRef ;
11
+ @ViewChild ( 'canvas' , { static : false } ) canvas : ElementRef ;
12
+ @Output ( ) imageDataUpdated = new EventEmitter < string > ( ) ;
13
13
14
14
constraints : object = {
15
15
video : {
@@ -22,11 +22,11 @@ export class PhotoCaptureComponent implements OnInit {
22
22
videoHeight : number = 0 ;
23
23
videoWidth : number = 0 ;
24
24
25
- photoCaptureState = new BehaviorSubject < boolean > ( false ) ;
25
+ photoCaptured = new BehaviorSubject < boolean > ( false ) ;
26
26
27
27
constructor ( private renderer : Renderer2 ) { }
28
28
29
- ngOnInit ( ) : void {
29
+ ngAfterViewInit ( ) {
30
30
this . startCamera ( ) ;
31
31
}
32
32
@@ -35,7 +35,7 @@ export class PhotoCaptureComponent implements OnInit {
35
35
if ( ( navigator . mediaDevices && navigator . mediaDevices . getUserMedia ) ) {
36
36
navigator . mediaDevices . getUserMedia ( this . constraints ) . then ( this . attachVideo . bind ( this ) ) . catch ( this . handleError . bind ( this ) ) ;
37
37
}
38
- else {
38
+ else {
39
39
alert ( 'Sorry, camera not available.' ) ;
40
40
}
41
41
}
@@ -59,11 +59,15 @@ export class PhotoCaptureComponent implements OnInit {
59
59
this . renderer . setProperty ( this . canvas . nativeElement , 'width' , this . videoWidth ) ;
60
60
this . renderer . setProperty ( this . canvas . nativeElement , 'height' , this . videoHeight ) ;
61
61
this . canvas . nativeElement . getContext ( '2d' ) . drawImage ( this . videoElement . nativeElement , 0 , 0 ) ;
62
- this . photoCaptureState . next ( true ) ;
62
+ this . photoCaptured . next ( true ) ;
63
+ }
64
+
65
+ retake ( ) {
66
+ this . photoCaptured . next ( false ) ;
63
67
}
64
68
65
69
acceptImage ( ) {
66
70
const canvasElement = this . canvas . nativeElement as HTMLCanvasElement ;
67
- this . imageData . emit ( canvasElement . toDataURL ( ) ) ;
71
+ this . imageDataUpdated . emit ( canvasElement . toDataURL ( ) ) ;
68
72
}
69
73
}
0 commit comments