cropImage static method

dynamic cropImage(
  1. Image originalImage
)

Implementation

static cropImage(img.Image originalImage) async {
  // Passport aspect ratio: 3.5 x 4.5 cm (≈ 0.78)
  const double passportRatio = 3.5 / 4.5;
  final int width = originalImage.width;
  final int height = originalImage.height;

  int cropHeight = height;
  int cropWidth = (cropHeight * passportRatio).toInt();

  if (cropWidth > width) {
    cropWidth = width;
    cropHeight = (cropWidth / passportRatio).toInt();
  }
  final int offsetX = ((width - cropWidth) / 2).round();
  final int offsetY = ((height - cropHeight) / 2).round();
  final cropped = img.copyCrop(
    originalImage,
    x: offsetX,
    y: offsetY,
    width: cropWidth,
    height: cropHeight,
  );
  return cropped;
}