compress method

void compress(
  1. dynamic onBytesLoaded(),
  2. dynamic onLibraryImageLoaded(
    1. Image
    )
)

compressed image is shown for user's reference

Implementation

void compress(Function() onBytesLoaded, Function(Image) onLibraryImageLoaded) async {
  worker.postMessage([0, imageBytes]);
  final event = await worker.onMessage.first;
  final List<int> intList = event.data[0];
  imageBytes = Uint8List.fromList(intList);
  onBytesLoaded.call();
  Uint8List byteData = Uint8List.fromList(imageBytes);
  int width = byteData.buffer.asByteData().getInt32(0, Endian.big);
  int height = byteData.buffer.asByteData().getInt32(4, Endian.big);

  final image = await Image.fromBytes(width: width, height: height, bytes: imageBytes.buffer);
  onLibraryImageLoaded.call(image);
}