Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crop a fixed size image in Android

Tags:

android

crop

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

like image 789
Etienne Lawlor Avatar asked Dec 16 '14 20:12

Etienne Lawlor


People also ask

Can you Crop a picture to a specific size?

Open Fotor Photo Crop and upload the image you want to modify. Drag the corners of the scale diagram to adjust the photo size. Choose from our selection of pre-determined dimensions, or type the exact measurements in the "Width" and "Height" fields. Choose a format for your image and save it.


2 Answers

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.

like image 79
klimat Avatar answered Oct 16 '22 07:10

klimat


There is a method you can useÆ

private void performCrop(){
    try {
        //call the standard crop action intent (the user device may not support it)
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        //indicate image type and Uri
        cropIntent.setDataAndType(selectedImage, "image/*");
        //set crop properties
        cropIntent.putExtra("crop", "true");
        //indicate aspect of desired crop
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        //indicate output X and Y
        cropIntent.putExtra("outputX", 640);
        cropIntent.putExtra("outputY", 640);
        //retrieve data on return
        cropIntent.putExtra("return-data", true);
        //start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, PIC_CROP);
    }
    catch(ActivityNotFoundException anfe){
        //display an error message
        String errorMessage = "Whoops - your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}

This part of code is what youøre interested in:

        cropIntent.putExtra("outputX", 640);
        cropIntent.putExtra("outputY", 640);

You should call crop method like this:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA && resultCode == RESULT_OK) {

        selectedImage = data.getData();
        performCrop();

    } 

    if (requestCode == UPLOAD && resultCode == RESULT_OK) {
        selectedImage = data.getData();
        performCrop();
    }

    if (requestCode == PIC_CROP && resultCode == RESULT_OK){
        Bundle extras = data.getExtras();
        //get the cropped bitmap
        Bitmap thePic = extras.getParcelable("data");

        // Do what you want to do with the pic here
    }



}
like image 23
SteBra Avatar answered Oct 16 '22 08:10

SteBra