Skip to content Skip to sidebar Skip to footer

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.ac

Solution 1:

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:

Solution 2:

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.

Solution 3:

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.

Solution 4:

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

protectedstaticfinalintREQ_CODE_PICK_BANNER=2; //identificador del intenprivatestaticfinalintBANNER_WIDTH_PX=640;
privatestaticfinalintBANNER_HEIGHT_PX=244;

   protectedvoidstartSelectorBanner(){
        IntentphotoPickerIntent=newIntent(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);        
  }


publicvoidonActivityResult(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) {
                            Bundleextras= imageReturnedIntent.getExtras();          
                            BitmapselectedBitmap= extras.getParcelable("data");
                            int[] screenSize = Screen.getSize(getActivity()); 
                            intbannerX= screenSize[0]; 
                            intbannerY= (int) (screenSize[0]/BANNER_RATIO);  

                            BitmapresizedBitmap= 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; 
            }       
    }

Post a Comment for "Cropping Pictures In Android By Keeping Aspect Ratio"