4

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!

MischaelKuhr
  • 41
  • 1
  • 1
  • 3

4 Answers4

2

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:

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Thats's the point, i want a rectangle, but i don't want the picture fit the size afterwards in 200x200.... so the aspect ratio is not as I chose and the picture become distorted. – MischaelKuhr May 05 '12 at 06:11
1

Just add the following lines of code for aspect ratio 0.75

intent.putExtra("aspectX", 3);
intent.putExtra("aspectY", 4);

and set scale to true.

this should solve the problem.

Makram Saleh
  • 8,613
  • 4
  • 27
  • 44
  • 1
    Its hard for me to explain! I dont want to fix the aspect ratio, i want the user to be able to choose the rectangle how he want, not pre defined by setting the aspect ratio or the pixel.... just, open the activity without setting the aspectX/Y or outputX/Y – MischaelKuhr May 09 '12 at 13:53
0

It might be little late to answer, considering that the other people might come to this thread with this same question.

cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);

will make sure that you always get a rectangle.

Ganesh Bhat
  • 687
  • 5
  • 5
0

Selecting a image from gallery and cropping keeping Aspect Ratio wanted. In this case the ratio is BANNER_WIDTH_PX:BANNER_HEIGHT_PX.

protected static final int REQ_CODE_PICK_BANNER = 2; //identificador del inten
private static final int BANNER_WIDTH_PX = 640;
private static final int BANNER_HEIGHT_PX =244;

   protected void startSelectorBanner(){
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            photoPickerIntent.setType("image/*");
            photoPickerIntent.putExtra("crop", "true");
             photoPickerIntent.putExtra("aspectX",BANNER_WIDTH_PX);  
             photoPickerIntent.putExtra("aspectY",BANNER_HEIGHT_PX);  
             photoPickerIntent.putExtra("outputX",BANNER_WIDTH_PX);  
             photoPickerIntent.putExtra("outputY",BANNER_HEIGHT_PX);  
            photoPickerIntent.putExtra("return-data", true);
            photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
            startActivityForResult(photoPickerIntent, REQ_CODE_PICK_BANNER);        
  }


public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {

            getMainActivity().removeOnActivityResultsListeners(EditQcardFragment.this); 

            switch (requestCode) {
               case REQ_CODE_PICK_BANNER:
                    if (resultCode == Activity.RESULT_OK) {  
                        if (imageReturnedIntent!=null) {
                            Bundle extras = imageReturnedIntent.getExtras();          
                            Bitmap selectedBitmap = extras.getParcelable("data");
                            int[] screenSize = Screen.getSize(getActivity()); 
                            int bannerX = screenSize[0]; 
                            int bannerY = (int) (screenSize[0]/BANNER_RATIO);  

                            Bitmap resizedBitmap = processPicture(selectedBitmap,"banner.png",bannerX,bannerY);
                            mBytesBanner = getPictureBytes("banner.png", resizedBitmap); 
                            log.debug("Get png width: " + mBytesBanner.length);
                            if(mBytesBanner!=null){
                                  mBanner.setImageBitmap(resizedBitmap);
                            }

                        }
                    }break; 
            }       
    }
Campino
  • 682
  • 7
  • 11