How to Crop an Image in Android

Crop image in android

Can you use default android Crop functionality?

Here is my code

private void performCrop(Uri picUri) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// set crop properties here
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", 128);
cropIntent.putExtra("outputY", 128);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
// respond to users whose devices do not support the crop action
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();
}
}

declare:

final int PIC_CROP = 1;

at top.

In onActivity result method, writ following code:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PIC_CROP) {
if (data != null) {
// get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedBitmap = extras.getParcelable("data");

imgView.setImageBitmap(selectedBitmap);
}
}
}

It is pretty easy for me to implement and also shows darken areas.

How to auto center crop ImageView Android?

I am not sure if I understood correctly. It must be either of the two, that you want, I guess.

In your image view set the attribute

android:scaleType="fitXY"

to fit the ImageView completely.

You can choose

android:scaleType="centerCrop"

to crop Center.

How to crop an image in Android

You can crop an image using an onActivityResult().
This video shows how to set it up properly with setOnClickListener().

Don't forget to set the implementation in your build.gradle:

implementation 'com.theartofdev.edmodo:android-image-cropper:2.4.+'

Good luck! :)

Crop an image when selected from gallery in android

Yes it's possible to crop image in android by using com.android.camera.action.CROP. after picking image url from gallery.you will start Crop Editor as:

Intent intent = new Intent("com.android.camera.action.CROP");  
intent.setClassName("com.android.camera", "com.android.camera.CropImage");
File file = new File(filePath);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 96);
intent.putExtra("outputY", 96);
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, REQUEST_CROP_ICON);

When the picture select Activity return will be selected to save the contents.in onActivityResult:

Bundle extras = data.getExtras();  
if(extras != null ) {
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);
// The stream to write to a file or directly using the photo
}

and see this post which is also help you for cropping image in android

How to crop images in android?

You can try using this Cropper Library

Its a great image cropping library. I have used it mostly in my apps.
Here is a screenshot of the library in action:

Sample Image

I hope you can find it useful. Cheers! :)



Related Topics



Leave a reply



Submit