getVisiblePortion function

Future<Image?> getVisiblePortion(
  1. File imageFile, {
  2. required Size flutterScreenSize,
})

Implementation

Future<Image?> getVisiblePortion(File imageFile,
    {required ui.Size flutterScreenSize}) async {
  Uint8List imageBytes = await imageFile.readAsBytes();

  Image? image = decodeImage(imageBytes);

  if (image == null) {
    return null;
  }

  ui.Size screenRectInimageSize = flutterToImageSize(flutterScreenSize, image);

  int y = (image.height ~/ 2) - (screenRectInimageSize.height ~/ 2);
  int x = (image.width ~/ 2) - (screenRectInimageSize.width ~/ 2);

  Image croppedImage = copyCrop(
    image,
    x > 0 ? x : 0,
    y > 0 ? y : 0,
    screenRectInimageSize.width.toInt(),
    screenRectInimageSize.height.toInt(),
  );

  return croppedImage;
}