Android Camera

In android app development the Camera is useful to capture the photos and videos in our applications and by using the camera API we can control the functionalities of the camera based on our requirements.

The android framework provides two ways such as android.hardware.camera2 API and camera intent to capture the images and videos in our application.

Intent

The intent action types either MediaStore.ACTION_IMAGE_CAPTURE or MediaStore.ACTION_VIDEO_CAPTURE and you can capture the videos / photos without directly using the Camera object.

The right way is to use the Intent to invoke an existing Android app camera application to take photos and videos in our app without writing a lot of extra code.

By using startActivityForResult() function with intent action parameter MediaStore.ACTION_IMAGE_CAPTURE, you can take the pictures from our android applications.

Following is the code to capture the pictures using the intent object with action parameter MediaStore.ACTION_IMAGE_CAPTURE in android applications.


Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cInt,12);

Android Camera App Example

Following is the example of using an existing camera app in our android app to capture the photos on click button.
Create a new android app using android studio and give names as CameraExample and In case if we are not aware of creating an app in android studio check this article Android Hello World App.
Once you create an application, open activity_main.xml file from \res\layout folder path and write the code like as shown below.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingLeft="10dp"
   android:paddingRight="10dp">
   <Button
       android:id="@+id/btnTakePicture"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="TAKE PICTURE"
       android:textStyle="bold"
       android:layout_centerHorizontal="true"
       android:layout_alignParentBottom="true" />
   <ImageView
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:id="@+id/capturedImage"
       android:layout_above="@+id/btnTakePicture"/>
</RelativeLayout>

Now open your main activity file in MainActivity.java from path and write the code like as shown below


package com.example.myapplication;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity{
   private Button btnCapture;
   private ImageView imgCapture;
   private static final int Image_Capture_Code = 1;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);


       btnCapture =(Button)findViewById(R.id.btnTakePicture);
       imgCapture = (ImageView) findViewById(R.id.capturedImage);
       btnCapture.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
               startActivityForResult(cInt,Image_Capture_Code);
           }
       });
   }
   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
       if (requestCode == Image_Capture_Code) {
           if (resultCode == RESULT_OK) {
               Bitmap bp = (Bitmap) data.getExtras().get("data");
               imgCapture.setImageBitmap(bp);
           } else if (resultCode == RESULT_CANCELED) {
               Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
           }
       }
   }


}

If we observe the above code snippet, you used startActivityForResult() method with MediaStore.ACTION_IMAGE_CAPTURE intent action parameter to capture the pictures.
When you run the above app in the android studio you will get the output as shown below.

When you click on the Take a Photo button and the camera will start and you can take the picture of whatever you want and the captured image can be shown in a defined imageview.

Subscribe Now