Camera Tutorial

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Android Camera Tutorial

Camera is mainly used to capture picture and video. We can control the camera by using methods of camera api.

Android provides the facility to work on camera by 2 ways:

  1. By Camera Intent

  2. By Camera API

Understanding basic classes of Camera Intent and API

There are mainly four classes that we are going to discuss.

Intent

By the help of 2 constants of MediaStore class, we can capture picture and video without using the instance of Camera class.

  1. ACTION_IMAGE_CAPTURE

  2. ACTION_VIDEO_CAPTURE


Camera

It is main class of camera api, that can be used to take picture and video.


SurfaceView

It represents a surface view ore preview of live camera.


MediaRecorder

It is used to record video using camera. It can also be used to record audio files as we have seen in the previous example of media framework.


Android camera app example by camera intent

In this example, we are writing the simple code to capture image using camera and displaying the image using imageview.

activity_main.xml

Drag one imageview and one button from the pallete, now the xml file will look like this:

  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.    <Button  
  8.         android:id="@+id/button1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentBottom="true"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:text="Take a Photo" >  
  14.     </Button>  
  15.   
  16.     <ImageView  
  17.         android:id="@+id/imageView1"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="fill_parent"  
  20.         android:layout_above="@+id/button1"  
  21.         android:layout_alignParentTop="true"  
  22.         android:src="@drawable/ic_launcher" >  
  23.     </ImageView>  
  24. </RelativeLayout> 

Activity class

Let's write the code to capture image using camera and displaying it on the image view.

  1. package com.example.simplecamera;  

  2.   

  3. import android.app.Activity;  

  4. import android.content.Intent;  

  5. import android.graphics.Bitmap;  

  6. import android.os.Bundle;  

  7. import android.view.Menu;  

  8. import android.view.View;  

  9. import android.widget.Button;  

  10. import android.widget.ImageView;  

  11.   

  12. public class MainActivity extends Activity {  

  13.      private static final int CAMERA_REQUEST = 1888;  

  14.      ImageView imageView;  

  15.      public void onCreate(Bundle savedInstanceState) {  

  16.   

  17.          super.onCreate(savedInstanceState);  

  18.          setContentView(R.layout.activity_main);  

  19.   

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

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

  22.   

  23.          photoButton.setOnClickListener(new View.OnClickListener() {  

  24.   

  25.          @Override  

  26.          public void onClick(View v) {  

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

  28.               startActivityForResult(cameraIntent, CAMERA_REQUEST);  

  29.          }  

  30.         });  

  31.        }  

  32.   

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

  34.       if (requestCode == CAMERA_REQUEST) {  

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

  36.        imageView.setImageBitmap(photo);  

  37.       }  

  38.    }  

  39.   

  40.     @Override  

  41.     public boolean onCreateOptionsMenu(Menu menu) {  

  42.         // Inflate the menu; this adds items to the action bar if it is present.  

  43.         getMenuInflater().inflate(R.menu.activity_main, menu);  

  44.         return true;  

  45.     }  

  46.   

  47. }  

Output

android simple camera example output 1