Hi friends let us learn how to create a camera based application in android.
Basics for capturing a image using existing camera in our mobile
You will use MediaStore.ACTION_IMAGE_CAPTURE to launch an existing
camera application installed on your phone. Its syntax is given below
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Apart from the above , there are other availaible Intents provided by MediaStore. They are listed as follows
| Sr.No | Intent type and description |
| 1 | ACTION_IMAGE_CAPTURE_SECURE It returns the image captured from the camera , when the device is secured |
| 2 | ACTION_VIDEO_CAPTURE It calls the existing video application in android to capture video |
| 3 | EXTRA_SCREEN_ORIENTATION It is used to set the orientation of the screen to vertical or landscape |
| 4 | EXTRA_FULL_SCREEN It is used to control the user interface of the ViewImage |
| 5 | INTENT_ACTION_VIDEO_CAMERA This intent is used to launch the camea in the video mode |
| 6 | EXTRA_SIZE_LIMIT It is used to specify the size limit of video or image capture size |
Now you will use the function
startActivityForResult to launch this activity and wait for its result. Its syntax is given below
startActivityForResult(intent,0)
This method has been defined in the
activity class. We are
calling it from main activity. There are methods defined in the activity
class that does the same job , but used when you are not calling from
the activity but from somewhere else. They are listed below
| Sr.No | Activity function description |
| 1 | startActivityForResultIntentintent,intrequestCode,Bundleoptions It starts an activity , but can take extra bundle of options with it |
| 2 | startActivityFromChildActivitychild,Intentintent,intrequestCode It launch the activity when your activity is child of any other activity |
| 3 | startActivityFromChildActivitychild,Intentintent,intrequestCode,Bundleoptions It work same as above , but it can take extra values in the shape of bundle with it |
| 4 | startActivityFromFragmentFragmentfragment,Intentintent,intrequestCode It launches activity from the fragment you are currently inside |
| 5 | startActivityFromFragmentFragmentfragment,Intentintent,intrequestCode,Bundleoptions It not only launches the activity from the fragment , but can take extra values with it |
No matter which function you used to launch the activity , they all
return the result. The result can be obtained by overriding the function
onActivityResult.
Example
Here is an example that shows how to launch the exisitng camera
application to capture an image and display the result in the form of
bitmap
To experiment with this example , you need to run this on an actual device on which camera is supported.
| Steps | Description |
| 1 | You will use Eclipse IDE to create an Android
application and name it as Camera under a package com.example.camera.
While creating this project, make sure you Target SDK and Compile With
at the latest version of Android SDK to use higher levels of APIs. |
| 2 | Modify src/MainActivity.java file to add intent code to launch the activity and result method to recieve the output. |
| 3 | Modify layout XML file res/layout/activity_main.xml
add any GUI component if required. Here we add only imageView and a
textView. |
| 4 | Modify res/values/strings.xml to define required constant values |
| 5 | Run the application and choose a running android device and install the application on it and verify the results. |
Following is the content of the modified main activity file
src/com.example.camera/MainActivity.java.
package com.example.camera;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView imgFavorite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgFavorite = (ImageView)findViewById(R.id.imageView1);
imgFavorite.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
open();
}
});
}
public void open(){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
@Override
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");
imgFavorite.setImageBitmap(bp);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Following will be 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="34dp"
android:layout_marginTop="36dp"
android:contentDescription="@string/hello_world"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/imageView1"
android:text="@string/tap"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Following will be the content of
res/values/strings.xml to define one new constants
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Camera</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="tap">Tap the image to open the camera!!</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" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.camera.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 try to run your Camera application. I assume you have connected
your actual Android Mobile device with your computer. To run the app
from Eclipse, open one of your project's activity files and click Run

icon from the toolbar.