-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcamera_provider.dart
255 lines (212 loc) · 7.01 KB
/
camera_provider.dart
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import 'dart:io' show File;
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'package:hive/hive.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:retrotrack/core/index.dart';
enum Selection { person, temperature, done }
class CameraProvider extends ChangeNotifier {
CameraProvider(this.context, this.cameraDescription) {
init(cameraDescription);
}
final BuildContext context;
final CameraDescription cameraDescription;
bool _isLoading = false;
LogEntry _logEntry;
Temperature _temperature;
int _currentPersonIndex = 0;
int _currentTemperatureIndex = 0;
String _peopleImagePath;
String _thermometerImagePath;
CameraController _controller;
Selection _currentSelection;
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
bool get isLoading => _isLoading;
LogEntry get logEntry => _logEntry;
Temperature get temperature => _temperature;
int get currentPersonIndex => _currentPersonIndex;
int get currentTemperatureIndex => _currentTemperatureIndex;
String get peopleImagePath => _peopleImagePath;
String get thermometerImagePath => _thermometerImagePath;
CameraController get controller => _controller;
Selection get currentSelection => _currentSelection;
GlobalKey<ScaffoldState> get scaffoldKey => _scaffoldKey;
void init(CameraDescription cameraDescription) {
if (cameraDescription != null) {
_controller = CameraController(cameraDescription, ResolutionPreset.high);
}
_currentSelection = Selection.person;
notifyListeners();
}
void setIsLoading(bool loading) {
_isLoading = loading;
notifyListeners();
}
void setPeopleImagePath(String imagePath) {
_peopleImagePath = imagePath;
notifyListeners();
}
void setThermometerImagePath(String imagePath) {
_thermometerImagePath = imagePath;
notifyListeners();
}
void setCurrentSelection(Selection selection) {
_currentSelection = selection;
notifyListeners();
}
void setLogEntry(LogEntry logEntry) {
_logEntry = logEntry;
notifyListeners();
}
void setTemperature(Temperature temperature) {
_temperature = temperature;
notifyListeners();
}
void selectAllPeople() {
setCurrentSelection(Selection.person);
for (int i = 0; i < logEntry.people.length; i++) {
logEntry.people[i].isSelected = true;
logEntry.people[i].temperature.isSelected = false;
}
notifyListeners();
}
void selectPerson(int index) {
setCurrentSelection(Selection.person);
_currentPersonIndex = index;
for (int i = 0; i < logEntry.people.length; i++) {
logEntry.people[i].isSelected = false;
logEntry.people[i].temperature.isSelected = false;
}
logEntry.people[_currentPersonIndex].isSelected = true;
notifyListeners();
}
void selectTemperature(int index) {
setCurrentSelection(Selection.temperature);
_currentTemperatureIndex = index;
for (int i = 0; i < logEntry.people.length; i++) {
logEntry.people[i].isSelected = false;
logEntry.people[i].temperature.isSelected = false;
}
logEntry.people[_currentTemperatureIndex].temperature.isSelected = true;
notifyListeners();
}
void reset() {
_currentPersonIndex = 0;
_currentTemperatureIndex = 0;
for (int i = 0; i < logEntry.people.length; i++) {
logEntry.people[i].isSelected = false;
logEntry.people[i].temperature.isSelected = false;
}
notifyListeners();
}
Future<void> addLogEntry(LogEntry logEntry) async {
final Box<LogEntry> logEntryBox = await Hive.openBox('log_entries');
logEntryBox.add(logEntry);
}
Future<bool> takePhoto() async {
setIsLoading(true);
if (currentSelection == Selection.done) {
addLogEntry(logEntry);
return true;
}
try {
final String id = generateId();
final String path =
join((await getExternalStorageDirectory()).path, '$id.jpg');
await controller.takePicture(path);
if (currentSelection == Selection.person) {
setPeopleImagePath(path);
final File compressedFile = await compressImageFile(
File(peopleImagePath),
peopleImagePath.replaceAll(
id,
generateId(),
),
);
setLogEntry(await processCropFaceImage(compressedFile));
if (logEntry != null) {
if (logEntry.people != null) {
if (logEntry.people.isNotEmpty) {
setCurrentSelection(Selection.temperature);
} else {
checkTakePhotoErrors(context);
}
} else {
checkTakePhotoErrors(context);
}
} else {
checkTakePhotoErrors(context);
}
} else if (currentSelection == Selection.temperature) {
setThermometerImagePath(path);
final File compressedFile = await compressImageFile(
File(thermometerImagePath),
thermometerImagePath.replaceAll(
id,
generateId(),
),
);
setTemperature(await processThermometerImage(compressedFile));
logEntry.people[_currentTemperatureIndex].temperature.photo =
temperature.photo;
logEntry.people[_currentTemperatureIndex].temperature.temperature =
temperature.temperature;
if (_currentTemperatureIndex < logEntry.people.length) {
_currentTemperatureIndex++;
if (_currentTemperatureIndex < logEntry.people.length) {
selectTemperature(_currentTemperatureIndex);
}
}
if (_currentTemperatureIndex == logEntry.people.length) {
reset();
setCurrentSelection(Selection.done);
}
}
} catch (e) {
if (logEntry != null) {
if (logEntry.people == null) {
setCurrentSelection(Selection.person);
}
}
checkTakePhotoErrors(context);
setIsLoading(false);
}
setIsLoading(false);
return false;
}
void checkTakePhotoErrors(BuildContext context) {
if (_currentSelection == Selection.person) {
showSnackBarMessage(context, '👾 Person not detected!');
} else if (_currentSelection == Selection.temperature) {
showSnackBarMessage(context, '👾 Temperature not detected!');
} else {
showSnackBarMessage(context, '👾 Error has detected!');
}
}
void showSnackBarMessage(BuildContext context, String message) {
_scaffoldKey.currentState.removeCurrentSnackBar();
_scaffoldKey.currentState.showSnackBar(
SnackBar(
content: Text(
message,
style: Theme.of(context).textTheme.caption.copyWith(
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
duration: const Duration(milliseconds: 3000),
),
);
}
String getFABText(Selection selection) {
switch (selection) {
case Selection.person:
return 'PERSON';
case Selection.temperature:
return 'TEMPERATURE';
default:
return 'SAVE';
}
}
}