4

我使用此代码开始裁剪活动,并将裁剪后的图片作为 onActivityResult 中的数据返回。所以这很好用。

    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);

这是将图像裁剪为 200x200 大小的代码,与我在裁剪活动中选择的纵横比无关。我担心的是,我想要在活动中使用矩形选择的纵横比,而不是通过放置数字 200 和 200 来固定。

但是当我注释掉这两行时,我的程序将强制关闭....

是否有任何解决方案可以精确地裁剪到我在裁剪活动中选择的图片部分,同时保持我放置在图片上的矩形的纵横比?我必须添加哪些数据作为额外数据?需要帮忙!

4

4 回答 4

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:

于 2012-05-04T17:34:05.963 回答
1

只需为纵横比添加以下代码行0.75

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

并将比例设置为true

这应该可以解决问题。

于 2012-05-09T06:09:25.110 回答
0

考虑到其他人可能会带着同样的问题来到这个帖子,现在回答可能有点晚了。

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

将确保你总是得到一个矩形。

于 2013-02-02T11:12:16.093 回答
0

从图库中选择图像并裁剪以保持所需的纵横比。在这种情况下,比率为 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; 
            }       
    }
于 2015-08-20T01:27:51.353 回答