Let user crop image

前端 未结 4 736
慢半拍i
慢半拍i 2020-12-05 16:27

I am having a hard time in figuring out how to let the user crop the picture. I would like to give bitmap variable with loaded bitmap to crop the picture before setting it a

4条回答
  •  一个人的身影
    2020-12-05 17:11

    Ok Dear Here I put my Whole code of Crop Image In Android. This the Global variable.

        //This For Image Crop
            /**
             *  Uri for set image crop option .
             */
            private Uri mImageCaptureUri;
            /**
             *  int for set key and get key from result activity . 
             */
            public final int CROP_FROM_CAMERA = 0;
    
    /**
         * Bitmap for apply Crop Operation Result.
         */
        private Bitmap _tempOpration;
    
        //This is Crop Method.
    
    /**
         * Method for apply Crop .
         * @param filePath -  String path of file .
         */
        private void doCrop(String filePath){
            try{
                //New Flow
                mImageCaptureUri = Uri.fromFile(new File(filePath));
    
                final ArrayList cropOptions = new ArrayList();
                Intent intent = new Intent("com.android.camera.action.CROP");
                intent.setType("image/*");
                List 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", 300);
                    intent.putExtra("outputY", 300);
                    intent.putExtra("aspectX", 1);
                    intent.putExtra("aspectY", 1);
                    intent.putExtra("scale", true);
                    intent.putExtra("return-data", true);
    
                    if (size == 1) 
                    {
                        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);
                    }
    
                    else
                    {
                        for (ResolveInfo res : list) 
                        {
                            final CropOption co = new CropOption();
                            co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
                            co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
                            co.appIntent= new Intent(intent);
                            co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
                            cropOptions.add(co);
                        }
    
                        CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);
                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        builder.setTitle("Choose Crop App");
                        builder.setAdapter( adapter, new DialogInterface.OnClickListener()
                        {
                            public void onClick( DialogInterface dialog, int item )
                            {
                                startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
                            }
                        });
    
                        builder.setOnCancelListener( new DialogInterface.OnCancelListener() 
                        {
                            public void onCancel( DialogInterface dialog ) 
                            {
                                if (mImageCaptureUri != null ) 
                                {
                                    getContentResolver().delete(mImageCaptureUri, null, null );
                                    mImageCaptureUri = null;
                                }
                            }
                        } );
                        AlertDialog alert = builder.create();
                        alert.show();
                    }
                }
            }
            catch (Exception ex) 
            {
                genHelper.showErrorLog("Error in Crop Function-->"+ex.toString());
            }
        }
    

    This is the another Class witch use for find Crop Activity Intent in Application.

    CropOption Class.

    public class CropOption 
    {
        public CharSequence title;
        public Drawable icon;
        public Intent appIntent;
    }
    

    this is use For Display List.

    CropOptionAdapter

    public class CropOptionAdapter extends ArrayAdapter 
    {
        private ArrayList mOptions;
        private LayoutInflater mInflater;
    
        public CropOptionAdapter(Context context, ArrayList options) 
        {
            super(context, R.layout.crop_selector, options);
    
            mOptions    = options;
    
            mInflater   = LayoutInflater.from(context);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup group)
        {
            if (convertView == null)
                convertView = mInflater.inflate(R.layout.crop_selector, null);
    
            CropOption item = mOptions.get(position);
    
            if (item != null) {
                ((ImageView) convertView.findViewById(R.id.iv_icon)).setImageDrawable(item.icon);
                ((TextView) convertView.findViewById(R.id.tv_name)).setText(item.title);
    
                return convertView;
            }
    
            return null;
        }
    }
    

    Layout File For CropOptionAdapter

    
    
    
        
    
        
    
    

    This is the resultActivity.witch give the crop image.

    /**
         * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
         */
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode != RESULT_OK) return;
            switch (requestCode)
            {
            case CROP_FROM_CAMERA:
                if (data == null) 
                {
                    genHelper.showToast("No Crop Activity in This");
                    return;
                }
                final Bundle extras = data.getExtras();
                if (extras != null) 
                {
                    try 
                    {
    
                        _tempOpration=extras.getParcelable("data");
                        imageLayout.setImageBitmap(_tempOpration);
                        _tempOpration=null;
    
                    } 
                    catch (Exception e) 
                    {
                        e.printStackTrace();
                    }
                }
                break;
            }
    
        }
    

    //Do This Type It's work on my live app.

    genHelper.showToast("No Crop Activity in This");

    is my general Class to help display toast message and Error Log.

    Bestof Luck.

提交回复
热议问题