-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.dart
More file actions
266 lines (240 loc) · 8.54 KB
/
Copy pathmain.dart
File metadata and controls
266 lines (240 loc) · 8.54 KB
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
256
257
258
259
260
261
262
263
264
265
266
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:frame_msg/rx/auto_exp_result.dart';
import 'package:logging/logging.dart';
import 'package:simple_frame_app/frame_vision_app.dart';
import 'package:simple_frame_app/simple_frame_app.dart';
import 'package:frame_msg/tx/plain_text.dart';
void main() => runApp(const MainApp());
final _log = Logger("MainApp");
class MainApp extends StatefulWidget {
const MainApp({super.key});
@override
MainAppState createState() => MainAppState();
}
class MainAppState extends State<MainApp> with SimpleFrameAppState, FrameVisionAppState {
// main state of camera streaming on/off
bool _processing = false;
// the image and metadata to show
Image? _image;
ImageMetadata? _imageMeta;
// auto exposure result stream
final RxAutoExpResult _rxAutoExpResult = RxAutoExpResult();
StreamSubscription<AutoExpResult>? _autoExpResultSubs;
AutoExpResult? _autoExpResult;
MainAppState() {
Logger.root.level = Level.INFO;
Logger.root.onRecord.listen((record) {
debugPrint('${record.level.name}: ${record.time}: ${record.loggerName}: ${record.message}');
});
}
@override
void initState() {
super.initState();
// kick off the connection to Frame and start the app if possible
tryScanAndConnectAndStart(andRun: true);
}
@override
Future<void> onRun() async {
// set up receive handler for auto exposure results stream
// TODO put the values into a state variable
_autoExpResultSubs?.cancel();
_autoExpResultSubs = _rxAutoExpResult.attach(frame!.dataResponse).listen((autoExpResult) {
// update the UI with the latest auto exposure result
setState(() {
_autoExpResult = autoExpResult;
});
_log.fine('auto exposure result: $autoExpResult');
},);
// initial message to display when running
final text = TxPlainText(text: '2-Tap: start or stop stream');
await frame!.sendMessage(0x0a, text.pack());
}
@override
Future<void> onCancel() async {
// cancel the auto exposure result stream
_autoExpResultSubs?.cancel();
// no app-specific cleanup required here
}
@override
Future<void> onTap(int taps) async {
switch (taps) {
case 2:
// check if there's processing in progress already and drop the request if so
if (!_processing) {
// start new vision capture
// asynchronously kick off the capture/processing pipeline
startStreaming();
}
else {
// state moves to stopping streaming after current image
// processing completes
stopStreaming();
}
break;
default:
}
}
/// Long-running loop that continues requesting photos
/// and processing them until _processing is set to false
Future<void> startStreaming() async {
_log.fine('start streaming');
_processing = true;
while (_processing) {
// synchronously call the capture and processing (just display) of each photo
await capture().then(process);
}
}
void stopStreaming() {
_log.fine('stop streaming');
_processing = false;
}
/// The vision pipeline to run when a photo is captured
/// Which in this case is just displaying
FutureOr<void> process((Uint8List, ImageMetadata) photo) async {
var imageData = photo.$1;
var meta = photo.$2;
setState(() {
_image = Image.memory(imageData, gaplessPlayback: true,);
_imageMeta = meta;
});
}
/// cancel the current photo
@override
Future<void> cancel() async {
currentState = ApplicationState.ready;
if (mounted) setState(() {});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Frame Live Camera Feed',
theme: ThemeData.dark(),
home: Scaffold(
appBar: AppBar(
title: const Text('Frame Live Camera Feed'),
actions: [getBatteryWidget()]
),
drawer: getCameraDrawer(),
onDrawerChanged: (isOpened) {
if (isOpened) {
// if the user opens the camera settings, stop streaming
_processing = false;
}
else {
// if the user closes the camera settings, send the updated settings to Frame
sendExposureSettings();
}
},
body: Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
children: [
if (_autoExpResult != null) AutoExpResultWidget(result: _autoExpResult!),
const Divider(),
_image ?? Container(),
const Divider(),
if (_imageMeta != null) ImageMetadataWidget(meta: _imageMeta!),
],
)
),
const Divider(),
],
),
floatingActionButton: getFloatingActionButtonWidget(const Icon(Icons.camera_alt), const Icon(Icons.cancel)),
persistentFooterButtons: getFooterButtonsWidget(),
),
);
}
}
class AutoExpResultWidget extends StatelessWidget {
final AutoExpResult result;
final TextStyle dataStyle = const TextStyle(fontSize: 10, fontFamily: 'helvetica');
const AutoExpResultWidget({super.key, required this.result});
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Colors.grey[800],
borderRadius: BorderRadius.circular(5.0),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Error: ${result.error.toStringAsFixed(2)}', style: dataStyle),
Text('RGain: ${result.redGain.toStringAsFixed(2)}', style: dataStyle),
],
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Shutter: ${result.shutter.toInt()}', style: dataStyle),
Text('GGain: ${result.greenGain.toStringAsFixed(2)}', style: dataStyle),
],
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Analog Gain: ${result.analogGain.toInt()}', style: dataStyle),
Text('BGain: ${result.blueGain.toStringAsFixed(2)}', style: dataStyle),
],
),
],
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Divider(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'CW Average: ${result.brightness.centerWeightedAverage.toStringAsFixed(2)}',
style: dataStyle),
Text(
'Matrix: [${result.brightness.matrix.r.toStringAsFixed(2)},'
'${result.brightness.matrix.g.toStringAsFixed(2)},'
'${result.brightness.matrix.b.toStringAsFixed(2)},'
'${result.brightness.matrix.average.toStringAsFixed(2)}]',
style: dataStyle),
],
),
const SizedBox(width: 16), // Add spacing between columns
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Scene: ${result.brightness.scene.toStringAsFixed(2)}',
style: dataStyle),
Text(
'Spot: [${result.brightness.spot.r.toStringAsFixed(2)},'
'${result.brightness.spot.g.toStringAsFixed(2)},'
'${result.brightness.spot.b.toStringAsFixed(2)},'
'${result.brightness.spot.average.toStringAsFixed(2)}]',
style: dataStyle)
,
],
),
],
),
],
),
],
),
);
}
}