2

I am developing an application where i need to crop image . But this image croping should happen on my activity only so i can not use default image cropping concept of android where image will go on another view where user can crop .

What i need is to create custom image cropper rectangle .

I am trying this from long but no luck . Would be glad if any body can help .

Thanks

krishnendra
  • 99
  • 1
  • 1
  • 14
  • http://stackoverflow.com/questions/18463102/crop-an-image-by-passing-the-image-file-path-in-android. may more examples search on stackoverflow and this http://stackoverflow.com/questions/10776438/crop-an-image-when-selected-from-gallery-in-android and this http://stackoverflow.com/questions/2085003/how-to-select-and-crop-an-image-in-android – Raghunandan Dec 26 '13 at 05:39
  • https://github.com/rajeshcp/Android-Crop-Tool/ one cropping tool I did for one of my project – Triode Dec 26 '13 at 05:40
  • Hello Rajesh , Thanks for sharing example can you please let me know how did you crop image after image displayed in cropper ? (CroppingTool)findViewById(R.id.crop_tool)).setmTargetImage(BitmapFactory.decodeResource(getResources(), R.drawable.arrahman)); – krishnendra Dec 26 '13 at 06:20
  • Hello Rajesh,Thanks Below code worked for me imageview.setImageBitmap(CroppingTool.cropedBitmap); – krishnendra Dec 26 '13 at 07:10

2 Answers2

8

my mainactivity..

public class MainActivity extends Activity {

private DragRectView view;
private ImageView original;

// private ImageView croppedImage;

/**
 * 
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    view = (DragRectView) findViewById(R.id.dragRect);
    original = (ImageView) findViewById(R.id.image);
    // croppedImage = (ImageView) findViewById(R.id.crop_image);
    if (null != view) {
        view.setOnUpCallback(new DragRectView.OnUpCallback() {
            @Override
            public void onRectFinished(final Rect rect) {
                Toast.makeText(
                        getApplicationContext(),
                        "Rect is (" + rect.left + ", " + rect.top + ", "
                                + rect.right + ", " + rect.bottom + ")",
                        Toast.LENGTH_LONG).show();
                Bitmap bitmap1 = Bitmap.createScaledBitmap(
                        ((BitmapDrawable) original.getDrawable())
                                .getBitmap(), original.getWidth(), original
                                .getHeight(), false);
                System.out.println(rect.height() + "    "
                        + bitmap1.getHeight() + "      " + rect.width()
                        + "    " + bitmap1.getWidth());
                if (rect.height() <= bitmap1.getHeight()
                        && rect.width() <= bitmap1.getWidth()) {
                    Bitmap bitmap = Bitmap.createBitmap(bitmap1,
                            view.getLeft(), view.getTop(), view.getWidth(),
                            view.getHeight());
                    System.out
                            .println("MainActivity.onCreate(...).new OnUpCallback() {...}.onRectFinished() if true");
                    Intent intent = new Intent(MainActivity.this,
                            CropImageActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("cropimage", bitmap);
                    startActivity(intent);
                    System.out.println("MainActivity.onCreate() ");
                }
            }
        });
    }
}
}

cropImageAcivity...

public class CropImageActivity extends Activity {
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.crop_image);
    imageView = (ImageView) findViewById(R.id.crop_image);
    Intent intent = getIntent();
    if (intent != null) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            System.out.println("CropImageActivity.onCreate()");
            Bitmap bitmap = extras.getParcelable("cropimage");
            imageView.setImageBitmap(bitmap);
        }
    }
}
}

DragRectangle...

public class DragRectView extends View {
private Paint mRectPaint;

private int mStartX = 0;
private int mStartY = 0;
private int mEndX = 0;
private int mEndY = 0;
private boolean mDrawRect = false;
private TextPaint mTextPaint = null;

private OnUpCallback mCallback = null;

public interface OnUpCallback {
    void onRectFinished(Rect rect);
}

public DragRectView(final Context context) {
    super(context);
    init();
}

public DragRectView(final Context context, final AttributeSet attrs) {
    super(context, attrs);
    init();
}

public DragRectView(final Context context, final AttributeSet attrs,
        final int defStyle) {
    super(context, attrs, defStyle);
    init();
}

/**
 * Sets callback for up
 * 
 * @param callback
 *            {@link OnUpCallback}
 */
public void setOnUpCallback(OnUpCallback callback) {
    mCallback = callback;
}

/**
 * Inits internal data
 */
private void init() {
    mRectPaint = new Paint();
    mRectPaint.setColor(Color.GREEN);
    mRectPaint.setStyle(Paint.Style.STROKE);
    mRectPaint.setStrokeWidth(5); 

    mTextPaint = new TextPaint();
    mTextPaint.setColor(Color.MAGENTA);
    mTextPaint.setTextSize(20);
}

@Override
public boolean onTouchEvent(final MotionEvent event) {

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mDrawRect = false;
        mStartX = (int) event.getX();
        mStartY = (int) event.getY();
        invalidate();
        break;

    case MotionEvent.ACTION_MOVE:
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        if (!mDrawRect || Math.abs(x - mEndX) > 5
                || Math.abs(y - mEndY) > 5) {
            mEndX = x;
            mEndY = y;
            invalidate();
        }
        mDrawRect = true;
        break;

    case MotionEvent.ACTION_UP:
        if (mCallback != null) {
            mCallback.onRectFinished(new Rect(Math.min(mStartX, mEndX),
                    Math.min(mStartY, mEndY), Math.max(mEndX, mStartX),
                    Math.max(mEndY, mStartX)));
        }
        invalidate();
        break;

    default:
        break;
    }

    return true;
}

@Override
protected void onDraw(final Canvas canvas) {
    super.onDraw(canvas);
    if (mDrawRect) {
        canvas.drawRect(Math.min(mStartX, mEndX), Math.min(mStartY, mEndY),
                Math.max(mEndX, mStartX), Math.max(mEndY, mStartY),
                mRectPaint);
        canvas.drawText(
                "  (" + Math.abs(mStartX - mEndX) + ", "
                        + Math.abs(mStartY - mEndY) + ")",
                Math.max(mEndX, mStartX), Math.max(mEndY, mStartY),
                mTextPaint);
    }
}
}

mainactvity layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_dark"
android:orientation="vertical"
android:weightSum="2" >

<ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitXY"
    android:src="@drawable/image" />

<com.afbb.imagecrop.DragRectView
    android:id="@+id/dragRect"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!--
     <ImageView
    android:id="@+id/crop_image"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:scaleType="fitXY"
    android:src="@drawable/image" />
-->

</RelativeLayout>

cropactivity layout

 <?xml version="1.0" encoding="utf-8"?>
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >

<ImageView
    android:id="@+id/crop_image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/ic_launcher" />

</FrameLayout>

it may help you...

anand
  • 416
  • 2
  • 11
  • 1
    Hello @anand thank-you can you please share your complete project with layouts ? – krishnendra Dec 26 '13 at 06:17
  • 2
    All one is perfect but it is not displaying the crop image into the cropimageactivity class. please helo me for this. Thanks – Denny Sharma Mar 03 '14 at 06:10
  • The `Bitmap bitmap = Bitmap.createBitmap(bitmap1, view.getLeft(), view.getTop(), view.getWidth(), view.getHeight())` part helped me solve my problem - displaying an image based on the crop rect provided. Thanks! – Aba Aug 18 '18 at 12:53
  • @Aba what is view? view.getLeft() – MSaudi Sep 05 '19 at 14:42
  • @MSaudi in his `mainactivity` code, `view = (DragRectView) findViewById(R.id.dragRect);` – Aba Sep 05 '19 at 14:54
  • 1
    @Aba still not working with me. selects a different area. see here : https://stackoverflow.com/questions/57807767/creating-bitmap-from-area-selected-by-touch-returns-wrong-image-part-android – MSaudi Sep 05 '19 at 14:58
  • @MSaudi sorry I don't remember what exactly I did with the code.. Hope you find what you're looking for. :) – Aba Sep 05 '19 at 14:59
0

you can try this for flexible and custom crop.....

  Intent intent = new Intent("com.android.camera.action.CROP");
  intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);            
  intent.setType("image/*");
  intent.putExtra("aspectX", 0);
  intent.putExtra("aspectY", 0);
  intent.putExtra("outputY", 600);
  intent.putExtra("outputX", 600);
  intent.putExtra("scale", "true);
aarti Ladva
  • 113
  • 9