cropImage static method

Future<File> cropImage(
  1. String path, {
  2. int originX = 0,
  3. int originY = 0,
  4. required double widthPercent,
  5. required double heightPercent,
})

Crop image file in path.

Implementation

static Future<File> cropImage(String path,
    {int originX = 0,
    int originY = 0,
    required double widthPercent,
    required double heightPercent}) async {
  // Get image properties.
  final ImageProperties properties =
      await FlutterNativeImage.getImageProperties(path);

  // Get exact image size from properties.
  final int width = properties.width!;
  final int height = properties.height!;

  // Re-calculate crop params with orientation info.
  double wPercent = widthPercent;
  double hPercent = heightPercent;
  if (properties.orientation == ImageOrientation.rotate90 ||
      properties.orientation == ImageOrientation.rotate270) {
    wPercent = heightPercent;
    hPercent = widthPercent;
  }

  // Crop image.
  int x = originX;
  int y = originY;
  if (properties.orientation == ImageOrientation.rotate270) {
    x = ((1.0 - wPercent) * width).toInt();
    y = ((1.0 - hPercent) * height).toInt();
  }
  return FlutterNativeImage.cropImage(
      path, x, y, (wPercent * width).toInt(), (hPercent * height).toInt());
}