Mindinventory / image_cropping

Crop and Rotate Images using this Flutter plugin
https://www.mindinventory.com/flutter-app-development.php
MIT License
43 stars 18 forks source link

Performance on WEB #8

Closed leonardojribeiro closed 2 years ago

leonardojribeiro commented 2 years ago

Hi! I found a possibility of most performance on flutter web. Its possible to use the resources of the dart:ui package. In example bellow you can resize an image and possible pass the width and heigth properties for the image in result.

Size _calculateSize({required Size imageSize, double? width, double? height}) {
    if (width != null && height != null) {
      return Size(width, height);
    } else if (width != null) {
      final scale = width / imageSize.width;
      final height = imageSize.height * scale;
      return Size(width, height);
    } else if (height != null) {
      final scale = height / imageSize.height;
      final width = imageSize.width * scale;
      return Size(width, height);
    }
    return imageSize;
  }

Future<ui.Image> resize(Uint8List image, {double? width, double? height}) async {
    final codec = await ui.instantiateImageCodec(image);
    final frameInfo = await codec.getNextFrame();
    final pictureRecorder = ui.PictureRecorder();
    final size = _calculateSize(
      imageSize: Size(
        frameInfo.image.width.toDouble(),
        frameInfo.image.height.toDouble(),
      ),
      height: height,
      width: width,
    );
    Canvas(pictureRecorder).drawImageRect(
      frameInfo.image,
      Rect.fromLTWH(0, 0, frameInfo.image.width.toDouble(), frameInfo.image.height.toDouble()), //change here 
      Rect.fromLTWH(0, 0, size.width, size.height), //change here
      Paint(),
    );
    frameInfo.image.dispose();
    return await pictureRecorder.endRecording().toImage(size.width.toInt(), size.height.toInt());
  }

For croping, you maybe change the parameters commenteds such necessary. I hope this helps.

deep-mindinventory commented 2 years ago

@leonardojribeiro are you checking with our latest version?