Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cropping Pictures in Android by keeping Aspect Ratio

I use this code to start the crop activity and i get the cropped picture as data in onActivityResult back. So this works fine.

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

    List<ResolveInfo> list = getPackageManager().queryIntentActivities(
            intent, 0);

    int size = list.size();

    if (size == 0) {
        Toast.makeText(this, "Can not find image crop app",
                Toast.LENGTH_SHORT).show();
        return;
    } else {
        intent.setData(mImageCaptureUri);

        intent.putExtra("outputX", 200);
        intent.putExtra("outputY", 200);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);

        Intent i = new Intent(intent);
        ResolveInfo res = list.get(0);

        i.setComponent(new ComponentName(res.activityInfo.packageName,
                res.activityInfo.name));

        startActivityForResult(i, CROP_FROM_CAMERA);

This is the code for cropping the image into the size of 200x200, dont matter which aspect ratio I chose at the cropping activity. My concern is, that i want THE aspect ratio which i choose with the rectangle in the activity, not a fixed by putting the numbers 200 and 200.

But when I comment out these two lines, my program will force close....

Are there any solutions for cropping exact to the part of the picture which i chose in the crop activity with keeping the aspect ratio from the rectangle I placed on my picture? Which data do I have to put as extras? Need Help!

like image 672
MischaelKuhr Avatar asked Sep 19 '25 14:09

MischaelKuhr


1 Answers

How to crop a rectangle from an image in android

Cropping to anything other than a square shape is not possible using the built-in Android cropping handling (com.android.camera.action.CROP).

Look at the OP's reply in the next thread - and you might have a solution.

This thread also has some good suggestions:

  • How to select and crop an image in android?
like image 121
paulsm4 Avatar answered Sep 21 '25 05:09

paulsm4