cropImage function

Future<MaskForCameraViewResult?> cropImage(
  1. String imagePath,
  2. int cropHeight,
  3. int cropWeight,
  4. double screenHeight,
  5. double screenWidth,
  6. MaskForCameraViewInsideLine? insideLine,
)

Implementation

Future<MaskForCameraViewResult?> cropImage(
    String imagePath,
    int cropHeight,
    int cropWeight,
    double screenHeight,
    double screenWidth,
    MaskForCameraViewInsideLine? insideLine) async {
  Uint8List imageBytes = await File(imagePath).readAsBytes();

  Image? image = decodeImage(imageBytes);

  double? increasedTimesW;
  double? increasedTimesH;
  if (image!.width > screenWidth) {
    increasedTimesW = image.width / screenWidth;
    increasedTimesH = image.height / screenHeight;
  } else {
    return null;
  }

  double sX = (screenWidth - cropWeight) / 2;
  double sY = (screenHeight - cropHeight) / 2;

  double x = sX * increasedTimesW;
  double y = sY * increasedTimesH;

  double w = cropWeight * increasedTimesW;
  double h = cropHeight * increasedTimesH;

  Image croppedImage = copyCrop(image,
      x: x.toInt(), y: y.toInt(), width: w.toInt(), height: h.toInt());
  MaskForCameraViewResult res = MaskForCameraViewResult();
  if (insideLine != null) {
    MaskForCameraViewResult halfRes =
        await _cropHalfImage(croppedImage, insideLine);
    res = halfRes;
  }
  List<int> croppedList = encodeJpg(croppedImage);
  Uint8List croppedBytes = Uint8List.fromList(croppedList);
  res.croppedImage = croppedBytes;
  return res;
}