Crop a fixed size image in Android

2019-03-15 04:00发布

问题:

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?

回答1:

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.



回答2:

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
    }



}


标签: android crop