Android Camera

Android Camera

Android Camera  and Android Video Camera are includes in Android Platform, letting you take videos and pictures in your applications. This tutorial discusses an instant, simple approach to video and image capture and outlines an advanced approach for creating custom camera experience for users

These are the next two ways, where you may use camera in the application

  • By Using existing android camera app inside our application
  • By Using Camera API provided by android inside our app ( We Explained in  Camera API)

In This Tutorial We Will Explain Following

By Using Existing Android Camera App Inside Our Application

You shall use MediaStore.ACTION_IMAGE_Take to launch a preexisting camera application installed on your phone. Its syntax and code is listed below

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

 

From the above apart, there are other available Intents provided by MediaStore. They can be listed the following

Intent Types and Descriptions

ACTION_IMAGE_CAPTURE_SECURE

It results the PIC captured from the android camera , when these devices is secured

ACTION_Video_CAPTURE

It calls the prevailing video request in android to fully capture video

EXTRA_SCREEN_ORIENTATION

It can be used to create the orientation of the display to vertical or landscape

EXTRA_FULL_SCREEN

It can be used to control an individual user interface of the ViewImage

INTENT_ACTION_VIDEO_CAMERA

This intent can be used to release the camera in the training video mode

EXTRA_SIZE_LIMIT

It is employed to specify the size limit of video or image capture size

Now you use the function startActivityForResult() to unveiling this activity and await its end result. Its CODE  is listed below

startActivityForResult(intent,0)

 

This technique has been identified in the activity class. Were contacting it from main activity. A couple of methods defined in the experience class that does the same job , but used if you are not calling from the experience but from someplace else. They are the following

Activity Function &  Descriptions

startActivityForResult

An activity is started out because of it , but may take extra package of options with it

startActivityFromChild

It launch the experience whenever your activity is child of another activity

startActivityFromChild

It work identical to above , but normally it takes extra ideals in the condition of bundle with it

startActivityFromFragment

It launches activity from the fragment you are inside presently

startActivityFromFragment

It not only launches the experience from the fragment , but may take extra beliefs with it

Whichever function you used to establish the activity , each of them give back the effect. The result can be acquired by overriding the function onActivityResult.

Android Camera Example

Here is a good android camera example that presents how to release the prevailing android camera application to fully capture a graphic and screen the bring about the proper execution of bitmap

To test out this android example , you will need to perform this on a genuine device which camera is recognized.

Steps     Description

  1. You use Android studio IDE to set-up an Android program and name it as Camera under a com.example.sappal.myapplication
  2. Modify src/MainActivity.java to include objective code to establish the experience and consequence solution to recieve the productivity.
  3. Modify design XML res/design/activity_main.xml add any GUI aspect if required. Here we add only imageView and a textView.
  4. Run the application form and choose a jogging android os device and set up the application onto it and validate the results.

Following is this content of the changed main activity data file src/MainActivity.java.

package com.example.sappal.myapplication;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;

import android.graphics.Bitmap;
import android.os.BatteryManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Set;

public class MainActivity extends ActionBarActivity {
   Button b1,b2;
   ImageView iv;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      b1=(Button)findViewById(R.id.button);
      iv=(ImageView)findViewById(R.id.imageView);
      
      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 0);
         }
      });
   }
   
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      // TODO Auto-generated method stub
      super.onActivityResult(requestCode, resultCode, data);
      
      Bitmap bp = (Bitmap) data.getExtras().get("data");
      iv.setImageBitmap(bp);
   }
   
   @Override
   protected void onDestroy() {
      super.onDestroy();
   }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
   }
   
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      
      int id = item.getItemId();
      
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
}

 

Here is the content of res/layout/activity_main.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   
   <TextView android:text="Camera Example" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Andro Development"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="camera"
      android:id="@+id/button"
      android:layout_below="@+id/imageView"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="86dp" />
      
</RelativeLayout>

 

Following will be the content of res/values/strings.xml to define one new constants

<resources>
   <string name="app_name">My Application</string>
   <string name="hello_world">Hello world!</string>
   <string name="action_settings">Settings</string>
</resources>

 

Following is the default content of AndroidManifest.xml 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.camera"
   android:versionCode="1"
   android:versionName="1.0" >

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.sappal.myapplication.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>

Let’s make an effort to run the application. I suppose you have linked your actual Google android Mobile device with your personal computer. To perform the app from Android studio room, available one of your project’s activity documents and click Run Eclipse Run Icon from the tool. Before starting the application, Android studio room will display pursuing window to choose a choice where you want to perform your Android software.

android camera

Choose your mobile device as a choice and check your mobile device that may screen pursuing display then

android camera

Now just touch on the button on and the camera will be opened up. Capture a picture just. After capturing it , two buttons can look requesting to discard it or keep it

android camera

Just press the tic button and you’ll be cut back to the application with the captured image instead of android icon

android camera