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?
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.
I'd use one of these solutions:
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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With