cropImageDataWithDartLibrary function

Future<Uint8List?> cropImageDataWithDartLibrary({
  1. required Uint8List rawImage,
  2. required EditActionDetails editAction,
  3. required Rect cropRect,
  4. required int imageWidth,
  5. required int imageHeight,
  6. required ExtendedImage extendedImage,
  7. required ImageProvider<Object> imageProvider,
})

Crop image data using Dart's image library.

This function crops image data using Dart's image library for web and desktop platforms.

Parameters:

  • Various parameters for image cropping and editing.

Returns: A Future that resolves to a Uint8List containing the cropped image data.

Example Usage: Not typically used directly; it's an internal implementation for specific platforms.

Implementation

Future<Uint8List?> cropImageDataWithDartLibrary({
  required Uint8List rawImage,
  required EditActionDetails editAction,
  required Rect cropRect,
  required int imageWidth,
  required int imageHeight,
  required ExtendedImage extendedImage,
  required ImageProvider imageProvider,
}) async {
  Uint8List? val;

  ///crop rect base on raw image
  Uint8List data = (isDesktop || isWebMobile) &&
          extendedImage.image is ExtendedNetworkImageProvider
      ? await _loadNetwork(extendedImage.image as ExtendedNetworkImageProvider)
      : rawImage;

  if (data == rawImage && imageProvider is ExtendedResizeImage) {
    cropRect = await _getCropRect(
      cropRect: cropRect,
      imageHeight: imageHeight,
      imageWidth: imageWidth,
      rawImage: rawImage,
    );
  }

  final EditActionDetails action = editAction;

  Image? src;
  //LoadBalancer lb;
  if (isDesktop || isWebMobile) {
    src = decodeImage(data);
  } else {
    src = await compute(decodeImage, data);
  }

  if (src != null) {
    //handle every frame.
    src.frames = src.frames.map((Image image) {
      //clear orientation
      image = bakeOrientation(image);

      if (action.needCrop) {
        image = copyCrop(
          image,
          x: cropRect.left.toInt(),
          y: cropRect.top.toInt(),
          width: cropRect.width.toInt(),
          height: cropRect.height.toInt(),
        );
      }

      if (action.needFlip) {
        late FlipDirection mode;
        if (action.flipY && action.flipX) {
          mode = FlipDirection.both;
        } else if (action.flipY) {
          mode = FlipDirection.horizontal;
        } else if (action.flipX) {
          mode = FlipDirection.vertical;
        }
        image = flip(image, direction: mode);
      }

      if (action.hasRotateAngle) {
        image = copyRotate(image, angle: action.rotateAngle);
      }
      return image;
    }).toList();
    if (src.frames.length == 1) {}
  }

  /// you can encode your image
  ///
  /// it costs much time and blocks ui.
  //var fileData = encodeJpg(src);

  /// it will not block ui with using isolate.
  //var fileData = await compute(encodeJpg, src);
  //var fileData = await isolateEncodeImage(src);
  Uint8List? fileData;
  if (src != null) {
    final bool onlyOneFrame = src.numFrames == 1;
    //If there's only one frame, encode it to jpg.
    if (isDesktop || isWebMobile) {
      fileData = onlyOneFrame
          ? encodeJpg(Image.from(src.frames.first))
          : encodeGif(src);
    } else {
      //fileData = await lb.run<List<int>, Image>(encodeJpg, src);
      fileData = onlyOneFrame
          ? await compute(encodeJpg, src)
          : await compute(encodeGif, src);
    }
  }
  val = Uint8List.fromList(fileData!);
  return val;
}