-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.dart
120 lines (111 loc) · 3.71 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
// ignore_for_file: avoid_print
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:opencv_core/opencv.dart' as cv;
import 'package:image_picker/image_picker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var images = <Uint8List>[];
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
Future<(cv.Mat, cv.Mat)> heavyTaskAsync(cv.Mat im, {int count = 1000}) async {
late cv.Mat gray, blur;
for (var i = 0; i < count; i++) {
gray = await cv.cvtColorAsync(im, cv.COLOR_BGR2GRAY);
blur = await cv.gaussianBlurAsync(im, (7, 7), 2, sigmaY: 2);
if (i != count - 1) {
gray.dispose(); // manually dispose
blur.dispose(); // manually dispose
}
}
return (gray, blur);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Native Packages'),
),
body: Container(
alignment: Alignment.center,
child: Column(
children: [
ElevatedButton(
onPressed: () async {
final picker = ImagePicker();
final img = await picker.pickImage(source: ImageSource.gallery);
if (img != null) {
final path = img.path;
final mat = cv.imread(path);
print("cv.imread: width: ${mat.cols}, height: ${mat.rows}, path: $path");
debugPrint("mat.data.length: ${mat.data.length}");
// heavy computation
final (gray, blur) = await heavyTaskAsync(mat, count: 1);
setState(() {
images = [
cv.imencode(".png", mat).$2,
cv.imencode(".png", gray).$2,
cv.imencode(".png", blur).$2,
];
});
}
},
child: const Text("Pick Image"),
),
ElevatedButton(
onPressed: () async {
final data = await DefaultAssetBundle.of(context).load("images/lenna.png");
final bytes = data.buffer.asUint8List();
// heavy computation
// final (gray, blur) = await heavyTask(bytes);
// setState(() {
// images = [bytes, gray, blur];
// });
final (gray, blur) = await heavyTaskAsync(cv.imdecode(bytes, cv.IMREAD_COLOR));
setState(() {
images = [bytes, cv.imencode(".png", gray).$2, cv.imencode(".png", blur).$2];
});
},
child: const Text("Process"),
),
Expanded(
flex: 2,
child: Row(
children: [
Expanded(
child: ListView.builder(
itemCount: images.length,
itemBuilder: (ctx, idx) => Card(
child: Image.memory(images[idx]),
),
),
),
Expanded(
child: SingleChildScrollView(
child: Text(cv.getBuildInformation()),
),
),
],
),
),
],
),
),
),
);
}
}