Android: Crop an Image After Taking It with Camera with a Fixed Aspect Ratio

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

Crop image on different devices

I found a better code for this problem. This here will search for apps which are able to crop images and start the first that is found. Hope that help someone.

Intent cropApps = new Intent("com.android.camera.action.CROP");
cropApps.setType("image/*");

List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(cropApps, 0);
int size = list.size();

if (size == 0)
{
Toast.makeText(context, "Can not find image crop app", Toast.LENGTH_SHORT).show();
return null;
}
else
{
ResolveInfo res = list.get(0);

Intent intent = new Intent();
intent.setClassName(res.activityInfo.packageName, res.activityInfo.name);

intent.setData(imageCaptureUri);
intent.putExtra("outputX", 96);
intent.putExtra("outputY", 96);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);

startActivityForResult(intent, CROP_FROM_CAMERA);
}


Related Topics



Leave a reply



Submit