Crop a fixed size image in Android

toobsco42 picture toobsco42 · Dec 16, 2014 · Viewed 12.7k times · Source

I am trying to crop an image but I want to be able to set the cropped area to exactly 640px x 640px. I want to prevent a user from cropping down to a really small area. So basically I would prefer to have a fixed height and width for the cropping area. I have looked into several third party libraries but don't see solving this problem. How can I do this?

enter image description here

Answer

klimat picture klimat · Dec 19, 2014

I'd use one of these solutions:

  1. https://github.com/jdamcd/android-crop
  2. https://github.com/edmodo/cropper

Both seems to be appropriate to solve your problem and for sure cover more edge cases, devices and other Android stuff just to be more stable and reliable.

EDIT:
I have introduced a few changes to android-crop and now you can use withFixedSize(int width, int height) to set fixed cropping area in pixels.

Looks like this:

 private void beginCrop(Uri source) {
        Uri outputUri = Uri.fromFile(new File(getCacheDir(), "cropped"));
        new Crop(source).output(outputUri).withFixedSize(640, 640).start(this);
    }

Here it's a pull request.

Check full code at my github https://github.com/mklimek/android-crop/tree/newfeature_fied_size_crop.

You can clone build and add it to your project afterwards.

Hope it will help you.