Crop particular part of image in android

user1273676 picture user1273676 · Mar 17, 2012 · Viewed 24k times · Source

I want to crop Red part from following image, Is there any simple method available in android that can crop following image.

Sample Image that i want to crop

I have found many SO questions but all are suggesting to used following code:

Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100); 

This code work well if width & height are around 2MP resolution, but if that cropped part is more than 3MP resolution than application got crashed with OOM error.

Is there any way that handle image more than 3MP during cropping?

Answer

Hitesh Patel picture Hitesh Patel · Mar 17, 2012

You can used following code that can solve your problem.

Matrix matrix = new Matrix();
matrix.postScale(0.5f, 0.5f);
Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100, matrix, true);

Above method do postScalling of image before cropping, so you can get best result with cropped image without getting OOM error.

For more detail you can refer this blog