0

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");
            }
        }
    }
old-ufo
  • 2,799
  • 2
  • 28
  • 40
user3710617
  • 29
  • 1
  • 7
  • Could you specify what do you mean by "doesn`t work", may be error message? – old-ufo Jun 05 '14 at 11:06
  • no i want this above code is correct. but it only crop image for me .. i want that during crop i zoom in and out the image and crop specefic portion of image after zooming. – user3710617 Jun 05 '14 at 11:44

2 Answers2

0

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;
    }
}
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
0

I don't know whether this will solve your problem . I created somewhat similar thing for my project adding the functionality to pick image from Camera or Gallery, then showing a fixed cropping window with image zoomin out functionality. Combining Photoview and Cropper tool. Shared the code on Github. You can modify the code to fit in your need. Added an apk file in the project. Use real device for testing camera as emulator doesn't handle camera well. Here's the link to my project.

https://github.com/ozeetee/AndroidImageZoomCrop

Ozeetee
  • 126
  • 1
  • 5