CoderzHeaven

December 15, 2012

How to crop an Image in Android?

This is a sample program that launches the camera and crop the captured image.

Check this link to another crop image example.

http://www.coderzheaven.com/2011/03/15/crop-an-image-in-android/

This is the layout xml.
activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_margin="3dp"android:text="@string/intro"android:textStyle="bold" /><Buttonandroid:id="@+id/capture_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/capture" /><ImageViewandroid:id="@+id/picture"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="5dp"android:contentDescription="@string/picture" /></LinearLayout>

Now this is the Main Java File that implements the crop functionality.

Here we are using the “com.android.camera.action.CROP” Intent to crop the Image passing the captured Image URI to it.

package com.coderzheaven.cropimage;import android.app.Activity;import android.content.ActivityNotFoundException;import android.content.Intent;import android.graphics.Bitmap;import android.net.Uri;import android.os.Bundle;import android.provider.MediaStore;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class ShootAndCropActivity extends Activity implements OnClickListener {final int CAMERA_CAPTURE = 1;final int CROP_PIC = 2;private Uri picUri;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button captureBtn = (Button) findViewById(R.id.capture_btn);captureBtn.setOnClickListener(this);}public void onClick(View v) {if (v.getId() == R.id.capture_btn) {try {// use standard intent to capture an imageIntent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// we will handle the returned data in onActivityResultstartActivityForResult(captureIntent, CAMERA_CAPTURE);} catch (ActivityNotFoundException anfe) {Toast toast = Toast.makeText(this, "This device doesn't support the crop action!",Toast.LENGTH_SHORT);toast.show();}}}protected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode == RESULT_OK) {if (requestCode == CAMERA_CAPTURE) {// get the Uri for the captured imagepicUri = data.getData();performCrop();}// user is returning from cropping the imageelse if (requestCode == CROP_PIC) {// get the returned dataBundle extras = data.getExtras();// get the cropped bitmapBitmap thePic = extras.getParcelable("data");ImageView picView = (ImageView) findViewById(R.id.picture);picView.setImageBitmap(thePic);}}}/*** this function does the crop operation.*/private void performCrop() {// take care of exceptionstry {// call the standard crop action intent (the user device may not// support it)Intent cropIntent = new Intent("com.android.camera.action.CROP");// indicate image type and UricropIntent.setDataAndType(picUri, "image/*");// set crop propertiescropIntent.putExtra("crop", "true");// indicate aspect of desired cropcropIntent.putExtra("aspectX", 2);cropIntent.putExtra("aspectY", 1);// indicate output X and YcropIntent.putExtra("outputX", 256);cropIntent.putExtra("outputY", 256);// retrieve data on returncropIntent.putExtra("return-data", true);// start the activity - we handle returning in onActivityResultstartActivityForResult(cropIntent, CROP_PIC);}// respond to users whose devices do not support the crop actioncatch (ActivityNotFoundException anfe) {Toast toast = Toast.makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT);toast.show();}}}

Download thecomplete source code for the above example from here.

Website PinFacebook TwitterMyspaceFriendfeedTechnoratidel.icio.usDiggGoogleStumbleUponPremium Responsive

Leave a Reply Cancel reply