What is camera

Android camera provides the features by which you can take the pic or record the video.

In my prgram run the app it will show you the buttn open camera when you click it will open the camera and you will take the pic.


Camera:.

In this section i am going to show the camera with practical example.

First open the android studio and create a project and give the name of your project like CameraDemo.

The screenshots of the program is as follows:

camera image camera image

You need to drag and drop the one ImageView and one Button from pallet. Open the activity_main.xml file and code look like this


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<ImageView
    android:id="@+id/ImageView"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    />
<Button
    android:id="@+id/btncamera"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_red_dark"
    android:textColor="@android:color/white"
    android:layout_marginBottom="1dp"
    android:text="Open Camera"
        />

</RelativeLayout>
       

Open the MainActivity.java file and write code look like this.


package com.example.niitpc.myapplication;
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity   {

    ImageView imageView;
    Button btncamera;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
     super.onCreate( savedInstanceState );
     setContentView(R.layout.activity_main );
     
     imageView=(ImageView)findViewById( R.id.ImageView );
     btncamera=(Button)findViewById( R.id.btncamera );
     btncamera.setOnClickListener(
     new View.OnClickListener() {
     @Override
     public void onClick(View v) {
     Intent intent=new Intent( MediaStore.ACTION_IMAGE_CAPTURE );
     startActivityForResult( intent,0 );
     }
   }

 );

}
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  
    super.onActivityResult( requestCode, resultCode, data );
    Bitmap bitmap=(Bitmap)data.getExtras().get("data");
    imageView.setImageBitmap( bitmap );
    }
}


Explanation:

ImageView imageView:Here we have declared the object imageView of ImageView

Button btncamera:Here i have declared the object btncamera of Button.

imageView=(ImageView)findViewById( R.id.ImageView ):Here i have initialized the imageview object.

btncamera=(Button)findViewById( R.id.btncamera ):Here i have initialized the btncamera object.

btncamera.setOnClickListener:Here i have created the listener for btncamera object of Button.

Intent intent=new Intent( MediaStore.ACTION_IMAGE_CAPTURE ):Here i have created the object intent of Intent and passing the argument mediaStore.ACTION_IMAGE_CAPTURE to the constructor of Intent.

startActivityForResult( intent,0 ):Here i have called the startActivityForResult() and passing two argument first is intent object and second is request code as 0.The main functionality is Starting another activity do not have to be one-way. You can also start another activity and receive a result back. To receive a result, call startActivityForResult() (instead of startActivity()).Here is like camera.

``

protected void onActivityResult(int requestCode, int resultCode, Intent data) :Here i have override the onActivityResult().It will take three argument first is requestCode seconde is resultCode and last is Intent object.

Bitmap bitmap=(Bitmap)data.getExtras().get("data"):Here i have take the pic from camera and assign into bitmap object of Bitmap.

imageView.setImageBitmap( bitmap ):The setImageBitmap() take the bitmap object as argument and set into imageView object.


Camera

what is camera for video click here

back                     next