35

Possible Duplicate:
How to crop the parsed image in android?

I have an image in my res/drawable folder and I would like to crop (i.e. slice out some part of the image) the image when loading it into an ImageView. However I am unsure how to do this, any suggestions?

Community
  • 1
  • 1
James
  • 13,891
  • 26
  • 68
  • 93
  • Linked question answer do not answer how to crop, please read it. – m0skit0 Jan 16 '17 at 18:12
  • The best library I found to crop images was [Android-Image-Cropper](https://github.com/ArthurHub/Android-Image-Cropper). See this [answer](https://stackoverflow.com/a/51985041/8383332). – Soon Santos Aug 23 '18 at 11:47

5 Answers5

40

From Bitmap.createBitmap: "Returns an immutable bitmap from the specified subset of the source bitmap. The new bitmap may be the same object as source, or a copy may have been made. It is initialized with the same density as the original bitmap."

Pass it a bitmap, and define the rectangle from which the new bitmap will be created.

// Take 10 pixels off the bottom of a Bitmap
Bitmap croppedBmp = Bitmap.createBitmap(originalBmp, 0, 0, originalBmp.getWidth(), originalBmp.getHeight()-10);
Hamzeh Soboh
  • 7,572
  • 5
  • 43
  • 54
roglewis
  • 401
  • 4
  • 2
13

The Android Contact manager EditContactActivity uses Intent("com.android.camera.action.CROP")

This is a sample code:

Intent intent = new Intent("com.android.camera.action.CROP");
// this will open all images in the Galery
intent.setDataAndType(photoUri, "image/*");
intent.putExtra("crop", "true");
// this defines the aspect ration
intent.putExtra("aspectX", aspectY);
intent.putExtra("aspectY", aspectX);
// this defines the output bitmap size
intent.putExtra("outputX", sizeX);
intent.putExtra("outputY", xizeY);
// true to return a Bitmap, false to directly save the cropped iamge
intent.putExtra("return-data", false);
//save output image in uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
rds
  • 26,253
  • 19
  • 107
  • 134
  • 1
    If I have an image at SD Card/myimage.jpg, How can I put the URI to CROP intent? ie. I want to crop a region of SD Card/myimage.jpg image. – Nguyen Minh Binh May 20 '11 at 18:04
  • #Nguyen, have you what you commented. – Abdul Wahab Nov 25 '12 at 08:20
  • @ rds, I can crop the image using above code, but the camera crop activity is still running outside my app, how to finish that intent, when user press save or cancel in the crop intent – Abdul Wahab Nov 27 '12 at 09:00
  • Of course it runs outside your app. Read the Intent: 1. It runs in the application that handles `com.android.camera.action.CROP`. 2. The output in saved in the `uri` that you specify. Last thing: Start this activity with `startActivityForResult` to know whether user pressed OK or Cancel. – rds Nov 27 '12 at 21:22
  • This approach means that you have to have permissions to access folders and in kti kat and higher need to add documents permission. If I am not mistaken – Zapnologica Feb 09 '16 at 11:48
7

Try this:

ImageView ivPeakOver=(ImageView) findViewById(R.id.yourImageViewID);

Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.yourImageID);
int width=(int)(bmp.getWidth()*peakPercent/100);
int height=bmp.getHeight();

Bitmap resizedbitmap=Bitmap.createBitmap(bmp,0,0, width, height);
ivPeakOver.setImageBitmap(resizedbitmap);

From the Docs:

static Bitmap    createBitmap(Bitmap source, int x, int y, int width, int height)

Returns an immutable bitmap from the specified subset of the source bitmap.

Saikat
  • 2,613
  • 1
  • 22
  • 14
  • Nice! Still silly it can't be done with a simple attribute on the ImageView though, but I guess this should be efficient enough. – Olof Hedman Dec 19 '11 at 14:32
6

If you want to equally crop the outside of the image, you should check out the ScaleType attribute for an ImageView: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

In particular, you would be interested in the "centerCrop" option. It crops out part of the image that is larger than the defined size.

Here's an example of doing this in the XML layout:

<ImageView  android:id="@+id/title_logo"
            android:src="@drawable/logo"
            android:scaleType="centerCrop" android:padding="4dip"/>
Josh Clemm
  • 2,966
  • 20
  • 21
4
 int targetWidth = 100;
 int targetHeight = 100;
 RectF rectf = new RectF(0, 0, 100, 100);//was missing before update
 Bitmap targetBitmap = Bitmap.createBitmap(
 targetWidth, targetHeight,Bitmap.Config.ARGB_8888);
 Canvas canvas = new Canvas(targetBitmap);
 Path path = new Path();
 path.addRect(rectf, Path.Direction.CW);
 canvas.clipPath(path);
 canvas.drawBitmap(
 sourceBitmap,
 new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
 new Rect(0, 0, targetWidth, targetHeight),
 null);
 ImageView imageView = (ImageView)findViewById(R.id.my_image_view);
 imageView.setImageBitmap(targetBitmap);
Swapnil Kadam
  • 4,075
  • 5
  • 29
  • 35
Sankar Ganesh PMP
  • 11,927
  • 11
  • 57
  • 90