mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-06 13:15:49 -06:00
feat: view type stored in view/cubit, itself, cleanup coe
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import 'dart:io';
|
||||
import 'dart:isolate';
|
||||
|
||||
import 'package:image/image.dart' as im;
|
||||
|
||||
typedef ImageOperationCallback = im.Image Function(im.Image);
|
||||
|
||||
class DecodeParam {
|
||||
final File file;
|
||||
final SendPort sendPort;
|
||||
final im.Image Function(im.Image) imageOperation;
|
||||
DecodeParam(this.file, this.sendPort, this.imageOperation);
|
||||
}
|
||||
|
||||
void decodeIsolate(DecodeParam param) {
|
||||
// Read an image from file (webp in this case).
|
||||
// decodeImage will identify the format of the image and use the appropriate
|
||||
// decoder.
|
||||
var image = im.decodeImage(param.file.readAsBytesSync())!;
|
||||
// Resize the image to a 120x? thumbnail (maintaining the aspect ratio).
|
||||
var processed = param.imageOperation(image);
|
||||
param.sendPort.send(processed);
|
||||
}
|
||||
|
||||
// Decode and process an image file in a separate thread (isolate) to avoid
|
||||
// stalling the main UI thread.
|
||||
Future<File> processImage(
|
||||
File file,
|
||||
ImageOperationCallback imageOperation,
|
||||
) async {
|
||||
var receivePort = ReceivePort();
|
||||
|
||||
await Isolate.spawn(
|
||||
decodeIsolate,
|
||||
DecodeParam(
|
||||
file,
|
||||
receivePort.sendPort,
|
||||
imageOperation,
|
||||
));
|
||||
|
||||
var image = await receivePort.first as im.Image;
|
||||
|
||||
return file.writeAsBytes(im.encodePng(image));
|
||||
}
|
||||
Reference in New Issue
Block a user