How to Crop Images In Android – Elluminati

Image cropping is one of the major aspect to be learnt during android development. Learn how to crop images in android from the following procedure:-

 

Hello Friends,

I am going to explore a sample application in android,Which gives you an idea that how to select image from gallery or devicecamera and crop it according to use.

To select an image from files, we can pass an intent to image gallery then pass the selected image path to camera app to crop the image.

Let’s Start

I am using Eclipse IDE.  you can choose other IDE.

STEP-1: create new Android application project.
File -> New -> Android Application Project

screen_1

 

Give name to the project and click on “Next >” button and select and fill appropriate options and then done with making Android Application Project.

STEP-2: Your MainActivity class (MainActivity.java) should contain following lines:

import java.io.File;

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.os.Environment;
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 MainActivity extends Activity {

ImageView imVCature_pic;
Button btnCaptureCamera,btnCaptureGallery;
private static final int PICK_FROM_GALLERY = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imVCature_pic = (ImageView) findViewById(R.id.imVCature_pic);
btnCaptureCamera = (Button) findViewById(R.id.btnCaptureCamera);
btnCaptureGallery = (Button) findViewById(R.id.btnCaptureGallery);

btnCaptureCamera.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Intent intent = new Intent(“android.media.action.IMAGE_CAPTURE”);
/* create instance of File with name img.jpg */
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + “img.jpg”);
/* put uri as extra in intent object */
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));

startActivityForResult(intent, 1);
}
});

btnCaptureGallery.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
// call android default gallery
intent.setType(“image/*”);
intent.setAction(Intent.ACTION_GET_CONTENT);
// ******** code for crop image
intent.putExtra(“crop”, “true”);
intent.putExtra(“aspectX”, 0);
intent.putExtra(“aspectY”, 0);
intent.putExtra(“outputX”, 200);
intent.putExtra(“outputY”, 150);

try {

intent.putExtra(“return-data”, true);
startActivityForResult(Intent.createChooser(intent,
“Complete action using”), PICK_FROM_GALLERY);

}

catch (ActivityNotFoundException e) {
// Do nothing for now
}
}
});

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if request code is same we pass as argument in startActivityForResult
if (requestCode == 1) {
// create instance of File with same name we created before to get
// image from storage
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + “img.jpg”);
// Crop the captured image using an other intent
try {
/* the user’s device may not support cropping */
cropCapturedImage(Uri.fromFile(file));
} catch (ActivityNotFoundException aNFE) {
// display an error message if user device doesn’t support
String errorMessage = “Sorry – your device doesn’t support the crop action!”;
Toast toast = Toast.makeText(this, errorMessage,
Toast.LENGTH_SHORT);
toast.show();
}
}
if (requestCode == 2) {
// Create an instance of bundle and get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap from extras
Bitmap thePic = extras.getParcelable(“data”);
// set image bitmap to image view
imVCature_pic.setImageBitmap(thePic);
}
if (requestCode == PICK_FROM_GALLERY) {
Bundle extras2 = data.getExtras();
if (extras2 != null) {
Bitmap photo = extras2.getParcelable(“data”);
imVCature_pic.setImageBitmap(photo);

}
}
}

// create helping method cropCapturedImage(Uri picUri)
public void cropCapturedImage(Uri picUri) {
// call the standard crop action intent
Intent cropIntent = new Intent(“com.android.camera.action.CROP”);
// indicate image type and Uri of image
cropIntent.setDataAndType(picUri, “image/*”);
// set crop properties
cropIntent.putExtra(“crop”, “true”);
// indicate aspect of desired crop
cropIntent.putExtra(“aspectX”, 1);
cropIntent.putExtra(“aspectY”, 1);
// indicate output X and Y
cropIntent.putExtra(“outputX”, 256);
cropIntent.putExtra(“outputY”, 256);
// retrieve data on return
cropIntent.putExtra(“return-data”, true);
// start the activity – we handle returning in onActivityResult
startActivityForResult(cropIntent, 2);
}
}

 You can download source code from here

STEP-3: Your  layout(activity_main.xml) shold contain following components:

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”#000000″
android:orientation=”vertical”
tools:context=”.LaunchCamera” >

<ImageView
android:id=”@+id/imVCature_pic”
android:layout_width=”match_parent”
android:layout_height=”300dp”
android:layout_gravity=”center”
android:src=”@drawable/ic_launcher” />

<Button
android:id=”@+id/btnCaptureCamera”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:text=”Take photo from Camera” />

<Button
android:id=”@+id/btnCaptureGallery”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:text=”Take photo from Gallery” />

</LinearLayout>

 You can download source code from here

That is it….  And you are done with the demo application.
When you run above code then you will get these screens as shown below:

Main Screen(When you run the application)

Main Screen(When you run the application)

 

When You select Second option(Take photo from gallery)

When You select Second option(Take photo from gallery)

 

It shows cropping action when you choose image from gallery

It shows cropping action when you choose image from gallery

image that you crop is set on the main screen

image that you crop is set on the main screen

Similarly you can do with camera option. In that case you can click photo from camera and then you get option to crop and then it is set on the main screen.