hnvn / flutter_image_cropper

A Flutter plugin for Android and iOS supports cropping images
969 stars 375 forks source link

issue when cropping photo and selecting the bottom part of image #443

Open KingOfPeru opened 8 months ago

KingOfPeru commented 8 months ago

when i want to crop the image from the very bottom, without zoom, just selecting the very bottom with the specified maxheight and maxwidth I get an error. This doesnt happen if I dont specify the maxheight and maxwidth. Error:

D/ForceDarkHelper( 2074): updateByCheckExcludeList: pkg: myapp.com activity: com.yalantis.ucrop.UCropActivity@fcf8296 V/PhoneWindow( 2074): DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@f03d2c4, this = DecorView@670a5ad[MainActivity] I/flutter ( 2074): ERROR_CROPPING_IMAGE: PlatformException(crop_error, y + height must be <= bitmap.height(), java.lang.IllegalArgumentException: y + height must be <= bitmap.height() I/flutter ( 2074): at android.graphics.Bitmap.createBitmap(Bitmap.java:879) I/flutter ( 2074): at android.graphics.Bitmap.createBitmap(Bitmap.java:836) I/flutter ( 2074): at com.yalantis.ucrop.task.BitmapCropTask.crop(BitmapCropTask.java:165) I/flutter ( 2074): at com.yalantis.ucrop.task.BitmapCropTask.doInBackground(BitmapCropTask.java:105) I/flutter ( 2074): at com.yalantis.ucrop.task.BitmapCropTask.doInBackground(BitmapCropTask.java:37) I/flutter ( 2074): at android.os.AsyncTask$3.call(AsyncTask.java:378) I/flutter ( 2074): at java.util.concurrent.FutureTask.run(FutureTask.java:266) I/flutter ( 2074): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) I/flutter ( 2074): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) I/flutter ( 2074): at java.lang.Thread.run(Thread.java:919) I/flutter ( 2074): , null)


Future<File?> cropImage({required File imageFile,required double ratioX, required double ratioY}) async{

  AndroidUiSettings androidUiSettings() => AndroidUiSettings(
    toolbarColor: toolbarColor,
    toolbarTitle: cropScreenTitle,
    toolbarWidgetColor: colorWhite,
    lockAspectRatio: true,
    showCropGrid: true,
    hideBottomControls: false,
  );

  IOSUiSettings iosUiSettings() => IOSUiSettings(
      rotateButtonsHidden: true,
    rotateClockwiseButtonHidden: true,
    aspectRatioLockEnabled: true,
    aspectRatioPickerButtonHidden: true,
    resetAspectRatioEnabled: false,
    title: cropScreenTitle,
    cancelButtonTitle: cancelCropButtonTitle,
    doneButtonTitle: doneCropButtonTitle,
    rectHeight: 290,rectWidth: 240,
  );

  try{
    CroppedFile? croppedFile = await ImageCropper().cropImage(
      sourcePath: imageFile.path,
      aspectRatio: CropAspectRatio(ratioX: ratioX, ratioY: ratioY),
      aspectRatioPresets: [CropAspectRatioPreset.original],
      maxHeight: 290,
      maxWidth: 240,
      uiSettings: [
        androidUiSettings(),
        iosUiSettings(),
      ],
    );

    if (croppedFile != null) {
      File croppedRegularFile = File(croppedFile.path);
      if(kDebugMode){
        printImageBytes(await croppedRegularFile.length());
        printImageDimensions(croppedRegularFile);
      }
      return croppedRegularFile;
    }

    return null;
  } catch(e){
    debugPrint("ERROR_CROPPING_IMAGE: $e");
    return null;
  }
}```