cropImage function

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

Implementation

Future<MaskForCameraViewResult?> cropImage(
    String ocrType,
    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 (ocrType.isNotEmpty && insideLine != null) {
    MaskForCameraViewResult halfRes =
        await _cropHalfImage(ocrType, croppedImage, insideLine);
    res = halfRes;
  }
  List<int> croppedList = encodeJpg(croppedImage);
  Uint8List croppedBytes = Uint8List.fromList(croppedList);
  res.croppedImage = croppedBytes;
  return res;
}