initializeCropRect static method

Rect initializeCropRect({
  1. required Size canvasSize,
  2. required Rect imageBounds,
  3. double? aspectRatio,
})

初始化裁剪框

Implementation

static Rect initializeCropRect({
  required Size canvasSize,
  required Rect imageBounds,
  double? aspectRatio,
}) {
  final double initialCropWidth;
  final double initialCropHeight;

  if (aspectRatio != null) {
    // 计算裁剪框尺寸,不超过图片显示区域
    double w = imageBounds.width * 0.8;
    double h = w / aspectRatio;
    if (h > imageBounds.height * 0.8) {
      h = imageBounds.height * 0.8;
      w = h * aspectRatio;
    }
    initialCropWidth = w;
    initialCropHeight = h;
  } else {
    final double maxSize = math.min(imageBounds.width, imageBounds.height) * 0.8;
    initialCropWidth = maxSize;
    initialCropHeight = maxSize;
  }

  // 将裁剪框居中在图片显示区域内
  final double left = imageBounds.left + (imageBounds.width - initialCropWidth) / 2;
  final double top = imageBounds.top + (imageBounds.height - initialCropHeight) / 2;
  return Rect.fromLTWH(left, top, initialCropWidth, initialCropHeight);
}