-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuModel.UserMedia.pas
186 lines (158 loc) · 5.52 KB
/
uModel.UserMedia.pas
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
unit uModel.UserMedia;
interface
uses
uModel.UserMedia.Interfaces;
type
TModelGetUserMedia = class(TInterfacedObject, iModelUserMedia)
private
FOnSuccess: string;
FOnError: string;
FVideoFacingMode: TFacingMode;
FVideoWidth: Integer;
FVideoHeight: Integer;
FFrameRate: Integer;
FWebcamElementID: string;
FPhotoCanvasID: string;
FCaptureButtonID: string;
function FacingModeToString(AFacingMode: TFacingMode): string;
public
class function New: iModelUserMedia;
constructor Create;
destructor Destroy; override;
function OnSuccess(AValue: string): iModelUserMedia;
function OnError(AValue: string): iModelUserMedia;
function VideoFacingMode(AValue: TFacingMode): iModelUserMedia;
function VideoResolution(AWidth, AHeight: Integer): iModelUserMedia;
function FrameRate(AValue: Integer): iModelUserMedia;
function WebcamElementID(AValue: string): iModelUserMedia;
function PhotoCanvasID(AValue: string): iModelUserMedia;
function CaptureButtonID(AValue: string): iModelUserMedia;
function Generate: string;
end;
implementation
uses
System.SysUtils;
{ TModelUserMedia }
constructor TModelGetUserMedia.Create;
begin
FVideoFacingMode := fmUser;
FVideoWidth := 1280;
FVideoHeight := 720;
FFrameRate := 30;
FWebcamElementID := 'webcam';
FPhotoCanvasID := 'photoCanvas';
FCaptureButtonID := 'captureButton';
end;
destructor TModelGetUserMedia.Destroy;
begin
inherited;
end;
function TModelGetUserMedia.FacingModeToString(AFacingMode: TFacingMode): string;
begin
case AFacingMode of
fmUser: Result := 'user';
fmEnvironment: Result := 'environment';
else
Result := 'user';
end;
end;
function TModelGetUserMedia.FrameRate(AValue: Integer): iModelUserMedia;
begin
Result := Self;
FFrameRate := AValue;
end;
function TModelGetUserMedia.Generate: string;
begin
Result :=
'const webcamElement = document.getElementById("' + FWebcamElementID + '");' +
'const photoCanvas = document.getElementById("' + FPhotoCanvasID + '");' +
'const captureButton = document.getElementById("' + FCaptureButtonID + '");' +
'const photoContext = photoCanvas.getContext("2d");' +
'let facingMode = "' + FacingModeToString(FVideoFacingMode) + '";' +
'async function initializeCamera() {' +
' try {' +
' const stream = await navigator.mediaDevices.getUserMedia({' +
' video: {' +
' facingMode: facingMode,' +
' width: { ideal: ' + FVideoWidth.ToString + ' },' +
' height: { ideal: ' + FVideoHeight.ToString + ' },' +
' frameRate: { ideal: ' + FFrameRate.ToString + ', max: 60 }' +
' },' +
' audio: false' +
' });' +
' webcamElement.srcObject = stream;' +
' } catch (error) {' +
' handleCameraError(error);' +
' }' +
'}' +
'function handleCameraError(error) {' +
' console.error("Erro ao acessar a câmera:", error);' +
' let errorMessage = "Erro ao acessar a câmera. ";' +
' if (error.name === "NotAllowedError") {' +
' errorMessage += "Verifique as permissões no navegador.";' +
' } else if (error.name === "NotFoundError") {' +
' errorMessage += "Nenhuma câmera foi encontrada no dispositivo.";' +
' } else if (error.name === "NotReadableError") {' +
' errorMessage += "A câmera está em uso por outro aplicativo.";' +
' } else {' +
' errorMessage += "Erro desconhecido. Verifique as configurações.";' +
' }' +
' ' + FOnError +
'}' +
'async function checkDevices() {' +
' const devices = await navigator.mediaDevices.enumerateDevices();' +
' const videoDevices = devices.filter(device => device.kind === "videoinput");' +
' const audioDevices = devices.filter(device => device.kind === "audioinput");' +
' console.log("Câmeras disponíveis:", videoDevices);' +
' console.log("Microfones disponíveis:", audioDevices);' +
'}' +
'function capturePhoto() {' +
' photoContext.drawImage(webcamElement, 0, 0, photoCanvas.width, photoCanvas.height);' +
' const base64Image = photoCanvas.toDataURL("image/png");' +
' ' + FOnSuccess +
'}' +
'captureButton.addEventListener("click", capturePhoto);' +
'checkDevices();' +
'initializeCamera();';
end;
class function TModelGetUserMedia.New: iModelUserMedia;
begin
Result := Self.Create;
end;
function TModelGetUserMedia.OnError(AValue: string): iModelUserMedia;
begin
Result := Self;
FOnError := AValue;
end;
function TModelGetUserMedia.OnSuccess(AValue: string): iModelUserMedia;
begin
Result := Self;
FOnSuccess := AValue;
end;
function TModelGetUserMedia.VideoFacingMode(AValue: TFacingMode): iModelUserMedia;
begin
Result := Self;
FVideoFacingMode := AValue;
end;
function TModelGetUserMedia.VideoResolution(AWidth, AHeight: Integer): iModelUserMedia;
begin
Result := Self;
FVideoWidth := AWidth;
FVideoHeight := AHeight;
end;
function TModelGetUserMedia.WebcamElementID(AValue: string): iModelUserMedia;
begin
Result := Self;
FWebcamElementID := AValue;
end;
function TModelGetUserMedia.PhotoCanvasID(AValue: string): iModelUserMedia;
begin
Result := Self;
FPhotoCanvasID := AValue;
end;
function TModelGetUserMedia.CaptureButtonID(AValue: string): iModelUserMedia;
begin
Result := Self;
FCaptureButtonID := AValue;
end;
end.