Q:

Crop a Bitmap image

How can i crop a bitmap image? this is my question i have tried some concepts using intents but still fail..
I am having a bitmap image which i want to crop!!
here is the code :
 Intent intent = new Intent("com.android.camera.action.CROP");  
                      intent.setClassName("com.android.camera", "com.android.camera.CropImage");  
                      File file = new File(filePath);  
                      Uri uri = Uri.fromFile(file);  
                      intent.setData(uri);  
                      intent.putExtra("crop", "true");  
                      intent.putExtra("aspectX", 1);  
                      intent.putExtra("aspectY", 1);  
                      intent.putExtra("outputX", 96);  
                      intent.putExtra("outputY", 96);  
                      intent.putExtra("noFaceDetection", true);  
                      intent.putExtra("return-data", true);                                  
                      startActivityForResult(intent, REQUEST_CROP_ICON);

Could anybody help me regarding this @Thanks

A:

I used this method to crop the image and it works perfect:
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.xyz);

resizedbitmap1=Bitmap.createBitmap(bmp, 0,0,yourwidth, yourheight);

createBitmap() takes bitmap,start X,start Y,width & height as parameter

A:

Using the answer above does not work if you want to slice/crop areas particular out of bounds!
Using this code you will always get your desired size - even if the source is smaller.
//  Here I want to slice a piece "out of bounds" starting at -50, -25
//  Given an endposition of 150, 75 you will get a result of 200x100px
Rect rect = new Rect(-50, -25, 150, 75);  
//  Be sure that there is at least 1px to slice.
assert(rect.left < rect.right && rect.top < rect.bottom);
//  Create our resulting image (150--50),(75--25) = 200x100px
Bitmap resultBmp = Bitmap.createBitmap(rect.right-rect.left, rect.bottom-rect.top, Bitmap.Config.ARGB_8888);
//  draw source bitmap into resulting image at given position:
new Canvas(resultBmp).drawBitmap(bmp, -rect.left, -rect.top, null);

...and you're done!

