-
Notifications
You must be signed in to change notification settings - Fork 38
/
main.dart
162 lines (140 loc) · 4.03 KB
/
main.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
import 'dart:async';
import 'dart:io';
import 'dart:isolate';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_opencv_example/native_opencv.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
const title = 'Native OpenCV Example';
late Directory tempDir;
String get tempPath => '${tempDir.path}/temp.jpg';
void main() {
WidgetsFlutterBinding.ensureInitialized();
getTemporaryDirectory().then((dir) => tempDir = dir);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: title,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _picker = ImagePicker();
bool _isProcessed = false;
bool _isWorking = false;
void showVersion() {
final scaffoldMessenger = ScaffoldMessenger.of(context);
final snackbar = SnackBar(
content: Text('OpenCV version: ${opencvVersion()}'),
);
scaffoldMessenger
..removeCurrentSnackBar(reason: SnackBarClosedReason.dismiss)
..showSnackBar(snackbar);
}
Future<String?> pickAnImage() async {
if (Platform.isIOS || Platform.isAndroid) {
return _picker
.pickImage(
source: ImageSource.gallery,
imageQuality: 100,
)
.then((v) => v?.path);
} else {
return FilePicker.platform
.pickFiles(
dialogTitle: 'Pick an image',
type: FileType.image,
allowMultiple: false,
)
.then((v) => v?.files.first.path);
}
}
Future<void> takeImageAndProcess() async {
final imagePath = await pickAnImage();
if (imagePath == null) {
return;
}
setState(() {
_isWorking = true;
});
// Creating a port for communication with isolate and arguments for entry point
final port = ReceivePort();
final args = ProcessImageArguments(imagePath, tempPath);
// Spawning an isolate
Isolate.spawn<ProcessImageArguments>(
processImage,
args,
onError: port.sendPort,
onExit: port.sendPort,
);
// Making a variable to store a subscription in
late StreamSubscription sub;
// Listening for messages on port
sub = port.listen((_) async {
// Cancel a subscription after message received called
await sub.cancel();
setState(() {
_isProcessed = true;
_isWorking = false;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body: Stack(
children: <Widget>[
Center(
child: ListView(
shrinkWrap: true,
children: <Widget>[
if (_isProcessed && !_isWorking)
ConstrainedBox(
constraints: BoxConstraints(maxWidth: 3000, maxHeight: 300),
child: Image.file(
File(tempPath),
alignment: Alignment.center,
),
),
Column(
children: [
ElevatedButton(
child: Text('Show version'),
onPressed: showVersion,
),
ElevatedButton(
child: Text('Process photo'),
onPressed: takeImageAndProcess,
),
],
)
],
),
),
if (_isWorking)
Positioned.fill(
child: Container(
color: Colors.black.withOpacity(.7),
child: Center(
child: CircularProgressIndicator(),
),
),
),
],
),
);
}
}