You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there any way to split image into several parts with specific Rect in parallel?For 50M jpeg,crop one time cost 2s,so i want to crop in parallel. Thanks for your help!
Unfortunately Dart does not have multithreading support. Your code will all run in the main thread.
Dart does support Isolates, which are more similar to separate processes, they have isolated memory. The Image library Command API supports running commands in Isolates. It does need to copy memory between the isolates, due to the lack of shared memory, so it's not as efficient as regular threads.
final image =Image(width:256, height:256);
finalList<Future<Uint8List>> resTask = [];
const curW =128;
const curH =128;
for (var curY =0; curY < image.height; curY += curH) {
for (var curX =0; curX < image.width; curX += curW) {
final task =Future(() async {
final cmd =Command()
..image(image) // This will copy the image to the isolate
..copyCrop(x: curX, y: curY, width: curW, height: curH)
..encodeJpg(); // encode the cropped image to a JPEG// Execute the commands in an Isolate thread and wait for the// results.final result =await cmd.executeThread();
// The resulting bytes of the last command, encodeJpgreturn result.outputBytes!;
});
resTask.add(task);
}
}
awaitFuture.wait(resTask).then((value) {
// 4 jpeg files with the cropped imagesexpect(value.length, equals(4));
});
I want to crop one Image into several part. so I have some code below:
Even I have use List<Future>to dispatch task, it seems to be sync, only after the last crop task finished the next one could be start.
The text was updated successfully, but these errors were encountered: