-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathadvanced_page.dart
266 lines (241 loc) · 6.83 KB
/
advanced_page.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
256
257
258
259
260
261
262
263
264
265
266
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_image_editor_example/const/resource.dart';
import 'package:image_editor/image_editor.dart' hide ImageSource;
import 'package:image_picker/image_picker.dart';
import 'package:oktoast/oktoast.dart';
class ExtendedImageExample extends StatefulWidget {
@override
_ExtendedImageExampleState createState() => _ExtendedImageExampleState();
}
class _ExtendedImageExampleState extends State<ExtendedImageExample> {
final GlobalKey<ExtendedImageEditorState> editorKey = GlobalKey();
ImageProvider provider = ExtendedExactAssetImageProvider(
R.ASSETS_HAVE_EXIF_3_JPG,
cacheRawData: true,
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Use extended_image library'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.photo),
onPressed: _pick,
),
IconButton(
icon: Icon(Icons.check),
onPressed: () async {
await crop(_editorController);
},
),
],
),
body: Container(
height: double.infinity,
child: Column(
children: <Widget>[
AspectRatio(
aspectRatio: 1,
child: buildImage(),
),
Expanded(
child: SliderTheme(
data: const SliderThemeData(
showValueIndicator: ShowValueIndicator.always,
),
child: Column(
children: <Widget>[
_buildSat(),
_buildBrightness(),
_buildCon(),
],
),
),
),
],
),
),
bottomNavigationBar: _buildFunctions(),
);
}
final ImageEditorController _editorController = ImageEditorController();
Widget buildImage() {
return ExtendedImage(
image: provider,
height: 400,
width: 400,
extendedImageEditorKey: editorKey,
mode: ExtendedImageMode.editor,
fit: BoxFit.contain,
initEditorConfigHandler: (_) => EditorConfig(
maxScale: 8.0,
cropRectPadding: const EdgeInsets.all(20.0),
hitTestSize: 20.0,
cropAspectRatio: 2 / 1,
controller: _editorController,
),
);
}
Widget _buildFunctions() {
return BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.flip),
label: 'Flip',
),
BottomNavigationBarItem(
icon: Icon(Icons.rotate_left),
label: 'Rotate left',
),
BottomNavigationBarItem(
icon: Icon(Icons.rotate_right),
label: 'Rotate right',
),
],
onTap: (int index) {
switch (index) {
case 0:
flip();
break;
case 1:
rotate(false);
break;
case 2:
rotate(true);
break;
}
},
currentIndex: 0,
selectedItemColor: Theme.of(context).primaryColor,
unselectedItemColor: Theme.of(context).primaryColor,
);
}
Future<void> crop(ImageEditorController imageEditorController) async {
print('native library start cropping');
final EditActionDetails action = imageEditorController.editActionDetails!;
final Uint8List img = imageEditorController.state!.rawImageData;
final ImageEditorOption option = ImageEditorOption();
if (action.hasRotateDegrees) {
final int rotateDegrees = action.rotateDegrees.toInt();
option.addOption(RotateOption(rotateDegrees));
}
if (action.flipY) {
option.addOption(const FlipOption(horizontal: true, vertical: false));
}
if (action.needCrop) {
Rect cropRect = imageEditorController.getCropRect()!;
if (imageEditorController.state!.widget.extendedImageState.imageProvider
is ExtendedResizeImage) {
final ImmutableBuffer buffer = await ImmutableBuffer.fromUint8List(img);
final ImageDescriptor descriptor =
await ImageDescriptor.encoded(buffer);
final double widthRatio =
descriptor.width / imageEditorController.state!.image!.width;
final double heightRatio =
descriptor.height / imageEditorController.state!.image!.height;
cropRect = Rect.fromLTRB(
cropRect.left * widthRatio,
cropRect.top * heightRatio,
cropRect.right * widthRatio,
cropRect.bottom * heightRatio,
);
}
option.addOption(ClipOption.fromRect(cropRect));
}
final DateTime start = DateTime.now();
final Uint8List? result = await ImageEditor.editImage(
image: img,
imageEditorOption: option,
);
print('${DateTime.now().difference(start)} :total time');
if (result != null) {
showPreviewDialog(result);
}
}
void flip() {
editorKey.currentState?.flip();
}
void rotate(bool right) {
editorKey.currentState?.rotate(
degree: right ? 90 : -90,
);
}
void showPreviewDialog(Uint8List image) {
showDialog<void>(
context: context,
builder: (BuildContext ctx) => GestureDetector(
onTap: () => Navigator.pop(context),
child: Container(
color: Colors.grey.withOpacity(0.5),
child: Center(
child: SizedBox.fromSize(
size: const Size.square(200),
child: Container(
child: Image.memory(image),
),
),
),
),
),
);
}
Future<void> _pick() async {
final XFile? result = await ImagePicker().pickImage(
source: ImageSource.camera,
);
if (result == null) {
showToast('The pick file is null');
return;
}
print(result.path);
provider = ExtendedFileImageProvider(File(result.path), cacheRawData: true);
setState(() {});
}
double sat = 1;
double bright = 1;
double con = 1;
Widget _buildSat() {
return Slider(
label: 'sat : ${sat.toStringAsFixed(2)}',
onChanged: (double value) {
setState(() {
sat = value;
});
},
value: sat,
min: 0,
max: 2,
);
}
Widget _buildBrightness() {
return Slider(
label: 'brightness : ${bright.toStringAsFixed(2)}',
onChanged: (double value) {
setState(() {
bright = value;
});
},
value: bright,
min: 0,
max: 2,
);
}
Widget _buildCon() {
return Slider(
label: 'con : ${con.toStringAsFixed(2)}',
onChanged: (double value) {
setState(() {
con = value;
});
},
value: con,
min: 0,
max: 4,
);
}
}