Introduction

This article describes how to capture and crop an image on a button click and show it in an ImageView in Android. You need to first open the camera by writing this code for the button click. In this, you will use an Intent to start the camera.
  1. public void onClick(View v) {
  2. Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  3. startActivityForResult(cameraIntent, CAMERA_REQUEST);
Now you will create a method, OnActivityResult(), to be called when the activity you started has finished. In this, you will use a Bitmap class object to draw the image. In this you will write this code:
  1. if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
  2. Bitmap photo = (Bitmap) data.getExtras().get("data");
  3. imageView.setImageBitmap(photo);
  4. }
Step 1
Now create an XML file with the following content:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#80BFFF">
  6. <Button android:id="@+id/button1"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="Click">
  10. </Button>
  11. <ImageView
  12. android:id="@+id/imageView1"
  13. android:layout_height="fill_parent"
  14. android:layout_width="fill_parent"
  15. android:layout_centerHorizontal="true">
  16. </ImageView>
  17. <ImageView
  18. android:id="@+id/imageView2"
  19. android:layout_height="fill_parent"
  20. android:layout_width="fill_parent"
  21. android:layout_centerHorizontal="true">
  22. </ImageView>
  23. <Button android:id="@+id/button2"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="Crop"
  27. android:layout_alignParentBottom="true">
  28. </Button>
  29. </RelativeLayout>
Step 2
  1. package com.imagecaptureapplication;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.graphics.Bitmap;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.ImageView;
  9. public class MainActivity extends Activity {
  10. private static final int CAMERA_REQUEST = 1888;
  11. ImageView imageView,imageView2;
  12. Bitmap photo;
  13. Button crop;
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. imageView = (ImageView)this.findViewById(R.id.imageView1);
  19. imageView2 = (ImageView)this.findViewById(R.id.imageView1);
  20. Button photoButton = (Button) this.findViewById(R.id.button1);
  21. crop=(Button)findViewById(R.id.button2);
  22. photoButton.setOnClickListener(new View.OnClickListener() {
  23. @Override
  24. public void onClick(View v) {
  25. Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  26. startActivityForResult(cameraIntent, CAMERA_REQUEST);
  27. }
  28. });
  29. }
  30. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  31. if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
  32. photo = (Bitmap) data.getExtras().get("data");
  33. imageView.setImageBitmap(photo);
  34. }
  35. performcrop();
  36. }
  37. private void performcrop() {
  38. crop.setOnClickListener(new View.OnClickListener() {
  39. @Override
  40. public void onClick(View view) {
  41. Bitmap croppedBmp = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(),photo.getHeight()-30);
  42. imageView2.setImageBitmap(croppedBmp);
  43. }
  44. });
  45. }
  46. }
Step 3
Add permission to the Android menifest.xml file:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.capture"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-permission android:name="android.permission.CAMERA" />
  7. <uses-feature android:name="android.hardware.camera" />
  8. <uses-feature android:name="android.hardware.camera.autofocus" />
  9. <uses-sdk
  10. android:minSdkVersion="7"
  11. android:targetSdkVersion="16" />
  12. <application
  13. android:allowBackup="true"
  14. android:icon="@drawable/ic_launcher"
  15. android:label="@string/app_name"
  16. android:theme="@style/AppTheme" >
  17. <activity
  18. android:name="com.capture.MainActivity"
  19. android:label="@string/app_name" >
  20. <intent-filter>
  21. <action android:name="android.intent.action.MAIN" />
  22. <category android:name="android.intent.category.LAUNCHER" />
  23. </intent-filter>
  24. </activity>
  25. </application>
  26. </manifest>
Step 4
Output
1.jpg
Step 5
When you click the button:
2.jpg
Step 6
After capturing the image:
3.jpg
Step 7
When you press on the button to crop:
5.jpg