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.

public void onClick(View v) {

                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(cameraIntent, CAMERA_REQUEST);

 



Now you will create a method, OnActivityResult(), to be callled 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:
 

if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {

            Bitmap photo = (Bitmap) data.getExtras().get("data");

            imageView.setImageBitmap(photo);

        }
 

Step  1

Now create an XML file with
the following content:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

 

              android:layout_width="fill_parent"

              android:layout_height="fill_parent"
             
android:background="#80BFFF">

 

  <Button android:id="@+id/button1"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:text="Click">

  </Button>

 

  <ImageView

          android:id="@+id/imageView1"

          android:layout_height="fill_parent"

          android:layout_width="fill_parent"

          android:layout_centerHorizontal="true">

  </ImageView>

 

  <ImageView

          android:id="@+id/imageView2"

          android:layout_height="fill_parent"

          android:layout_width="fill_parent"

          android:layout_centerHorizontal="true">

  </ImageView>

 

 

  <Button android:id="@+id/button2"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:text="Crop"

          android:layout_alignParentBottom="true">

  </Button>

 

 

</RelativeLayout>

 

Step 2
 

package com.imagecaptureapplication; 

import android.app.Activity;

import android.content.Intent;

import android.graphics.Bitmap;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

 

public class MainActivity extends Activity {

    private static final int CAMERA_REQUEST = 1888;

    ImageView imageView,imageView2;

 

    Bitmap photo;

    Button crop;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

       imageView = (ImageView)this.findViewById(R.id.imageView1);

        imageView2 = (ImageView)this.findViewById(R.id.imageView1);

        Button photoButton = (Button) this.findViewById(R.id.button1);

      crop=(Button)findViewById(R.id.button2);

       photoButton.setOnClickListener(new View.OnClickListener() {

 

            @Override

            public void onClick(View v) {

                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(cameraIntent, CAMERA_REQUEST);

            }

        });

 

    }

 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {

          photo = (Bitmap) data.getExtras().get("data");

            imageView.setImageBitmap(photo);

         }

        performcrop();

    }

 

    private void performcrop() {

    crop.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {

            Bitmap croppedBmp = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(),photo.getHeight()-30);

            imageView2.setImageBitmap(croppedBmp);

 

        }

    });

    }

    }


Step 3

Add permission to the  Android menifest.xml file:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.capture"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.camera" />

    <uses-feature android:name="android.hardware.camera.autofocus" />

 

    <uses-sdk

        android:minSdkVersion="7"

        android:targetSdkVersion="16" />

 

 

 

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.capture.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</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

Up Next
    Ebook Download
    View all
    Learn
    View all