-
-
Notifications
You must be signed in to change notification settings - Fork 503
/
crop_editor_helper.dart
257 lines (222 loc) · 8.3 KB
/
crop_editor_helper.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
//import 'dart:typed_data';
import 'dart:isolate';
import 'dart:ui';
// import 'package:isolate/load_balancer.dart';
// import 'package:isolate/isolate_runner.dart';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/foundation.dart';
// ignore: implementation_imports
import 'package:http_client_helper/http_client_helper.dart';
import 'package:image/image.dart';
import 'package:image_editor/image_editor.dart';
// final Future<LoadBalancer> loadBalancer =
// LoadBalancer.create(1, IsolateRunner.spawn);
enum ImageType { gif, jpg }
class EditImageInfo {
EditImageInfo(
this.data,
this.imageType,
);
final Uint8List? data;
final ImageType imageType;
}
Future<EditImageInfo> cropImageDataWithDartLibrary(
ImageEditorController imageEditorController) async {
print('dart library start cropping');
///crop rect base on raw image
Rect cropRect = imageEditorController.getCropRect()!;
final ExtendedImageEditorState state = imageEditorController.state!;
print('getCropRect : $cropRect');
// in web, we can't get rawImageData due to .
// using following code to get imageCodec without download it.
// final Uri resolved = Uri.base.resolve(key.url);
// // This API only exists in the web engine implementation and is not
// // contained in the analyzer summary for Flutter.
// return ui.webOnlyInstantiateImageCodecFromUrl(
// resolved); //
final Uint8List data = kIsWeb &&
imageEditorController.state!.widget.extendedImageState.imageWidget
.image is ExtendedNetworkImageProvider
? await _loadNetwork(imageEditorController.state!.widget
.extendedImageState.imageWidget.image as ExtendedNetworkImageProvider)
///toByteData is not work on web
///https://github.com/flutter/flutter/issues/44908
// (await state.image.toByteData(format: ui.ImageByteFormat.png))
// .buffer
// .asUint8List()
: state.rawImageData;
if (data == state.rawImageData &&
state.widget.extendedImageState.imageProvider is ExtendedResizeImage) {
final ImmutableBuffer buffer =
await ImmutableBuffer.fromUint8List(state.rawImageData);
final ImageDescriptor descriptor = await ImageDescriptor.encoded(buffer);
final double widthRatio = descriptor.width / state.image!.width;
final double heightRatio = descriptor.height / state.image!.height;
cropRect = Rect.fromLTRB(
cropRect.left * widthRatio,
cropRect.top * heightRatio,
cropRect.right * widthRatio,
cropRect.bottom * heightRatio,
);
}
final EditActionDetails editAction = state.editAction!;
final DateTime time1 = DateTime.now();
//Decode source to Animation. It can holds multi frame.
Image? src;
//LoadBalancer lb;
if (kIsWeb) {
src = decodeImage(data);
} else {
src = await compute(decodeImage, data);
}
if (src != null) {
//handle every frame.
src.frames = src.frames.map((Image image) {
final DateTime time2 = DateTime.now();
//clear orientation
image = bakeOrientation(image);
if (editAction.hasRotateDegrees) {
image = copyRotate(image, angle: editAction.rotateDegrees);
}
if (editAction.flipY) {
image = flip(image, direction: FlipDirection.horizontal);
}
if (editAction.needCrop) {
image = copyCrop(
image,
x: cropRect.left.toInt(),
y: cropRect.top.toInt(),
width: cropRect.width.toInt(),
height: cropRect.height.toInt(),
);
}
final DateTime time3 = DateTime.now();
print('${time3.difference(time2)} : crop/flip/rotate');
return image;
}).toList();
if (src.frames.length == 1) {}
}
/// you can encode your image
///
/// it costs much time and blocks ui.
//var fileData = encodeJpg(src);
/// it will not block ui with using isolate.
//var fileData = await compute(encodeJpg, src);
//var fileData = await isolateEncodeImage(src);
assert(src != null);
List<int>? fileData;
print('start encode');
final DateTime time4 = DateTime.now();
final bool onlyOneFrame = src!.numFrames == 1;
//If there's only one frame, encode it to jpg.
if (kIsWeb) {
fileData =
onlyOneFrame ? encodeJpg(Image.from(src.frames.first)) : encodeGif(src);
} else {
//fileData = await lb.run<List<int>, Image>(encodeJpg, src);
fileData = (onlyOneFrame
? await compute(encodeJpg, Image.from(src.frames.first))
: await compute(encodeGif, src));
}
final DateTime time5 = DateTime.now();
print('${time5.difference(time4)} : encode');
print('${time5.difference(time1)} : total time');
return EditImageInfo(
Uint8List.fromList(fileData!),
onlyOneFrame ? ImageType.jpg : ImageType.gif,
);
}
Future<EditImageInfo> cropImageDataWithNativeLibrary(
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');
return EditImageInfo(result, ImageType.jpg);
}
Future<dynamic> isolateDecodeImage(List<int> data) async {
final ReceivePort response = ReceivePort();
await Isolate.spawn(_isolateDecodeImage, response.sendPort);
final dynamic sendPort = await response.first;
final ReceivePort answer = ReceivePort();
// ignore: always_specify_types
sendPort.send([answer.sendPort, data]);
return answer.first;
}
Future<dynamic> isolateEncodeImage(Image src) async {
final ReceivePort response = ReceivePort();
await Isolate.spawn(_isolateEncodeImage, response.sendPort);
final dynamic sendPort = await response.first;
final ReceivePort answer = ReceivePort();
// ignore: always_specify_types
sendPort.send([answer.sendPort, src]);
return answer.first;
}
void _isolateDecodeImage(SendPort port) {
final ReceivePort rPort = ReceivePort();
port.send(rPort.sendPort);
rPort.listen((dynamic message) {
final SendPort send = message[0] as SendPort;
final List<int> data = message[1] as List<int>;
send.send(decodeImage(Uint8List.fromList(data)));
});
}
void _isolateEncodeImage(SendPort port) {
final ReceivePort rPort = ReceivePort();
port.send(rPort.sendPort);
rPort.listen((dynamic message) {
final SendPort send = message[0] as SendPort;
final Image src = message[1] as Image;
send.send(encodeJpg(src));
});
}
/// it may be failed, due to Cross-domain
Future<Uint8List> _loadNetwork(ExtendedNetworkImageProvider key) async {
try {
final Response? response = await HttpClientHelper.get(Uri.parse(key.url),
headers: key.headers,
timeLimit: key.timeLimit,
timeRetry: key.timeRetry,
retries: key.retries,
cancelToken: key.cancelToken);
return response!.bodyBytes;
} on OperationCanceledError catch (_) {
print('User cancel request ${key.url}.');
return Future<Uint8List>.error(
StateError('User cancel request ${key.url}.'));
} catch (e) {
return Future<Uint8List>.error(StateError('failed load ${key.url}. \n $e'));
}
}