Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

android - how to zoom + crop a image and display the croped image on imageview

I am making android app in which I have a scenario that I pick an image from gallery, crop it and show it on imageview. Now at the time of cropping I want to zoom that image as well. I am using TouchImageView class for zooming an image.

Note that if I only want to apply TouchImageView on ImageView it works fine. But when I used it with crop functionality it doesn't work.

How should I apply crop+zoom functionality at a time on ImageView? Any type of help will be appreciated. Below is my try.

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        super.onActivityResult(requestCode, resultCode, data);
        System.out.println(requestCode);
         if (resultCode != RESULT_OK) {

                return;
            }

            Bitmap bitmap;

            switch (requestCode) {

                case 0:

                     mImageCaptureUri = data.getData();
                    doCrop();

                    break;
                case 1:

                    mImageCaptureUri = data.getData();
                    doCrop();
                    break;
                case 2:
                    Bundle extras = data.getExtras();
                    /**
                     * After cropping the image, get the bitmap of the cropped image and
                     * display it on imageview.
                     */
                    if (extras != null) {
                          Bitmap photo = extras.getParcelable("data");
                          img_v.setImageBitmap(photo); 

                          //myBitmap = getCircleImage(photo);

                          //String image_base64 = postimage();

                         // new PostCoverTask().execute(userid, image_base64);
                    }

                    File f = new File(mImageCaptureUri.getPath());
                    /**
                     * Delete the temporary image
                     */
                    if (f.exists())
                          f.delete();
                    break;
            }
            super.onActivityResult(requestCode, resultCode, data);


    }

    private void doCrop() 
    {
        final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
        /**
         * Open image crop app by starting an intent
         * ‘com.android.camera.action.CROP‘.
         */
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(mImageCaptureUri,"image/*");

        /**
         * Check if there is image cropper app installed.
         */
        List<ResolveInfo> list = getPackageManager().queryIntentActivities(
                intent, 0);

        int size = list.size();


        /**
         * If there is no image cropper app, display warning message
         */
        if (size == 0)
        {
            Toast.makeText(this, "Can not find image crop app",
                    Toast.LENGTH_SHORT).show();

            return;
        }
        else 
        {
            size=1;
            /**
             * Specify the image path, crop dimension and scale
             */
            // intent.setData(mImageCaptureUri);
            intent.putExtra("crop", "true");
            intent.putExtra("outputX", 256);
            intent.putExtra("outputY", 256);
            intent.putExtra("aspectX",3);
            intent.putExtra("aspectY", 1);
            intent.putExtra("scale", true);
            intent.putExtra("scaleUpIfNeeded", true);
            intent.putExtra("return-data", true);
            System.out.println("Put image in Extra");
            /**
             * There is posibility when more than one image cropper app exist,
             * so we have to check for it first. If there is only one app, open
             * then app.
             */

            if (size == 1) {
                Intent i = new Intent(intent);
                ResolveInfo res=null ;
                for (int i1=0;i1<list.size();i1++) 
                {
                    if(list.get(i1)!=null)
                    {
                        res = list.get(i1);
                        break;
                    }
                }
                i.setComponent(new ComponentName(res.activityInfo.packageName,
                        res.activityInfo.name));
                System.out.println("size is equal to "+size);
                startActivityForResult(i, 2);
            }
            else
            {
                System.out.println("size is equal to "+size);
                /**
                 * If there are several app exist, create a custom chooser to
                 * let user selects the app.
                 */
                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) {
                        System.out.println("option "+cropOptions.get(item));
                        //startActivityForResult(cropOptions.get(item).appIntent,
                            //  CROP_FROM_CAMERA);
                        System.out.println("builder.setAdapter");
                    }
                });

                builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {

                        if (mImageCaptureUri != null) {
                            getContentResolver().delete(mImageCaptureUri, null,
                                    null);
                            mImageCaptureUri = null;

                        }
                        System.out.println("Click on cancel");
                    }
                });

                AlertDialog alert = builder.create();

                alert.show();
                System.out.println("alert");
            }
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am using cropimage library from Github. It fits your requirement well. This is how I use this library in my project. Add this line to your manifest file:

 <activity android:name="eu.janmuller.android.simplecropimage.CropImage" />

Select image from gallery or camera and call this function:

public void runCropImage(String path) {
    Intent intent = new Intent(this, CropImage.class);
    intent.putExtra(CropImage.IMAGE_PATH, path);
    intent.putExtra(CropImage.SCALE, true);
    intent.putExtra(CropImage.ASPECT_X, 2);//change ration here via intent
    intent.putExtra(CropImage.ASPECT_Y, 2);
    startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);//final static int 1
}

And in your onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    // gallery and camera ommitted
    case REQUEST_CODE_CROP_IMAGE:
        String path = data.getStringExtra(CropImage.IMAGE_PATH);
        // if nothing received
        if (path == null) {
            return;
        }
        // cropped bitmap
         Bitmap bitmap = BitmapFactory.decodeFile(path);
        imageView.setImageBitmap(bitmap);
        break;
    default:
        break;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...