Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to crop an image in android? [duplicate]

Tags:

android

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?

like image 517
James Avatar asked Oct 02 '10 15:10

James


People also ask

How do you crop a screenshot on Android?

After you take the screenshot, you'll see a preview of the image in the bottom-left corner. From here you can tap “Edit.” This will take you to a basic photo editing screen. First, you can grab the corners to crop the screenshot.


2 Answers

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); 
like image 67
roglewis Avatar answered Sep 29 '22 14:09

roglewis


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); 
like image 28
rds Avatar answered Sep 29 '22 14:09

rds