How to Capture and Crop Image in Android

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.    
  4.               android:layout_width="fill_parent"  
  5.               android:layout_height="fill_parent"  
  6.    
  7.               android:background="#80BFFF">  
  8.    
  9.   <Button android:id="@+id/button1"  
  10.           android:layout_width="wrap_content"  
  11.           android:layout_height="wrap_content"  
  12.           android:text="Click">  
  13.   </Button>  
  14.    
  15.   <ImageView  
  16.           android:id="@+id/imageView1"  
  17.           android:layout_height="fill_parent"  
  18.           android:layout_width="fill_parent"  
  19.           android:layout_centerHorizontal="true">  
  20.   </ImageView>  
  21.    
  22.   <ImageView  
  23.           android:id="@+id/imageView2"  
  24.           android:layout_height="fill_parent"  
  25.           android:layout_width="fill_parent"  
  26.           android:layout_centerHorizontal="true">  
  27.   </ImageView>  
  28.    
  29.    
  30.   <Button android:id="@+id/button2"  
  31.           android:layout_width="wrap_content"  
  32.           android:layout_height="wrap_content"  
  33.           android:text="Crop"  
  34.           android:layout_alignParentBottom="true">  
  35.   </Button>  
  36.    
  37.    
  38. </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.    
  10. public class MainActivity extends Activity {  
  11.     private static final int CAMERA_REQUEST = 1888;  
  12.     ImageView imageView,imageView2;  
  13.    
  14.     Bitmap photo;  
  15.     Button crop;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.        imageView = (ImageView)this.findViewById(R.id.imageView1);  
  21.         imageView2 = (ImageView)this.findViewById(R.id.imageView1);  
  22.         Button photoButton = (Button) this.findViewById(R.id.button1);  
  23.       crop=(Button)findViewById(R.id.button2);  
  24.        photoButton.setOnClickListener(new View.OnClickListener() {  
  25.    
  26.             @Override  
  27.             public void onClick(View v) {  
  28.                 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
  29.                 startActivityForResult(cameraIntent, CAMERA_REQUEST);  
  30.             }  
  31.         });  
  32.    
  33.     }  
  34.    
  35.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  36.         if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
  37.           photo = (Bitmap) data.getExtras().get("data");  
  38.             imageView.setImageBitmap(photo);  
  39.          }  
  40.         performcrop();  
  41.     }  
  42.    
  43.     private void performcrop() {  
  44.     crop.setOnClickListener(new View.OnClickListener() {  
  45.         @Override  
  46.         public void onClick(View view) {  
  47.             Bitmap croppedBmp = Bitmap.createBitmap(photo, 00, photo.getWidth(),photo.getHeight()-30);  
  48.             imageView2.setImageBitmap(croppedBmp);  
  49.    
  50.         }  
  51.     });  
  52.   }  
  53. }
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.    
  7.     <uses-permission android:name="android.permission.CAMERA" />  
  8.     <uses-feature android:name="android.hardware.camera" />  
  9.     <uses-feature android:name="android.hardware.camera.autofocus" />  
  10.    
  11.     <uses-sdk  
  12.         android:minSdkVersion="7"  
  13.         android:targetSdkVersion="16" />  
  14.    
  15.     <application  
  16.         android:allowBackup="true"  
  17.         android:icon="@drawable/ic_launcher"  
  18.         android:label="@string/app_name"  
  19.         android:theme="@style/AppTheme" >  
  20.         <activity  
  21.             android:name="com.capture.MainActivity"  
  22.             android:label="@string/app_name" >  
  23.             <intent-filter>  
  24.                 <action android:name="android.intent.action.MAIN" />  
  25.    
  26.                 <category android:name="android.intent.category.LAUNCHER" />  
  27.             </intent-filter>  
  28.         </activity>  
  29.     </application>  
  30.    
  31. </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


Similar Articles