blob: e176012440c2dc3df4200410cff3f297e38f819e [file] [log] [blame]
import 'dart:io';
import 'dart:isolate';
import 'package:image/image.dart';
class DecodeParam {
final File file;
final SendPort sendPort;
DecodeParam(this.file, this.sendPort);
}
void decode(DecodeParam param) {
// Read an image from file (webp in this case).
// decodeImage will identify the format of the image and use the appropriate
// decoder.
final image = decodeImage(param.file.readAsBytesSync())!;
// Resize the image to a 120x? thumbnail (maintaining the aspect ratio).
final thumbnail = gaussianBlur(copyResize(image, width: 120), 5);
param.sendPort.send(thumbnail);
}
// Decode and process an image file in a separate thread (isolate) to avoid
// stalling the main UI thread.
Future<void> main() async {
final receivePort = ReceivePort();
await Isolate.spawn(
decode, DecodeParam(File('test.webp'), receivePort.sendPort));
// Get the processed image from the isolate.
final image = await receivePort.first as Image;
File('thumbnail.png').writeAsBytesSync(encodePng(image));
}