Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:io';
- import 'dart:isolate';
- import 'dart:ui';
- import 'package:image/image.dart';
- class ImageCompressor{
- ImageCompressor();
- static Future<void> getCompressedImage(SendPort sendPort) async {
- ReceivePort receivePort = ReceivePort();
- sendPort.send(receivePort.sendPort);
- List msg = (await receivePort.first) as List;
- File fileImg = msg[0];
- SendPort replyPort = msg[1];
- Image image = decodeImage(await fileImg.readAsBytes());
- if (image.width > 800) {
- image = copyResize(image, width: 800);
- }
- replyPort.send(image);
- }
- static Future<Image> compressImage(File file) async {
- ReceivePort receivePort = ReceivePort();
- await Isolate.spawn(getCompressedImage, receivePort.sendPort);
- SendPort sendPort = await receivePort.first;
- ReceivePort receivePort2 = ReceivePort();
- sendPort.send([
- file,
- receivePort2.sendPort,
- ]);
- return await receivePort2.first;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement