Aswin Balaji

Aswin Balaji

186 Sadasivam Nagar,Madipakkam,Chennai, TN 600091
Aswin Balaji

Android Camera examples

September 26, 2015, by Aswin Balaji, category ANDROID
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

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:
File: activity_main.xml

  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.    <Button
  7.         android:id=”@+id/button1″
  8.         android:layout_width=”wrap_content”
  9.         android:layout_height=”wrap_content”
  10.         android:layout_alignParentBottom=”true”
  11.         android:layout_centerHorizontal=”true”
  12.         android:text=”Take a Photo” >
  13.     </Button>
  14.     <ImageView
  15.         android:id=”@+id/imageView1″
  16.         android:layout_width=”fill_parent”
  17.         android:layout_height=”fill_parent”
  18.         android:layout_above=”@+id/button1″
  19.         android:layout_alignParentTop=”true”
  20.         android:src=”@drawable/ic_launcher” >
  21.     </ImageView>
  22. </RelativeLayout>

Activity class

Let’s write the code to capture image using camera and displaying it on the image view.
File: MainActivity.java
  1. package com.example.simplecamera;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.graphics.Bitmap;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.ImageView;
  10. public class MainActivity extends Activity {
  11.      private static final int CAMERA_REQUEST = 1888;
  12.      ImageView imageView;
  13.      public void onCreate(Bundle savedInstanceState) {
  14.          super.onCreate(savedInstanceState);
  15.          setContentView(R.layout.activity_main);
  16.          imageView = (ImageView) this.findViewById(R.id.imageView1);
  17.          Button photoButton = (Button) this.findViewById(R.id.button1);
  18.          photoButton.setOnClickListener(new View.OnClickListener() {
  19.          @Override
  20.          public void onClick(View v) {
  21.               Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  22.               startActivityForResult(cameraIntent, CAMERA_REQUEST);
  23.          }
  24.         });
  25.        }
  26.      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  27.       if (requestCode == CAMERA_REQUEST) {
  28.        Bitmap photo = (Bitmap) data.getExtras().get(“data”);
  29.        imageView.setImageBitmap(photo);
  30.       }
  31.    }
  32.     @Override
  33.     public boolean onCreateOptionsMenu(Menu menu) {
  34.         // Inflate the menu; this adds items to the action bar if it is present.
  35.         getMenuInflater().inflate(R.menu.activity_main, menu);
  36.         return true;
  37.     }
  38. }

Output:

android simple camera example output 1