Camera
These are the two ways in which you can use the camera in your application
-
Using existing android camera application in our application
-
Directly using Camera API provided by android in our application
Using existing android camera application in our application
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 available 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 camera 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 is defined in the activity class. We are calling it from main activity in the activity class, the methods defined in the same work that do the same thing, but when you are not calling from the activity, but use from somewhere else. They are listed below
Sr.No | Activity function description |
---|---|
1 | startActivityForResult(Intent intent, int requestCode, Bundle options)
It starts an activity , but can take extra bundle of options with it |
2 | startActivityFromChild(Activity child, Intent intent, int requestCode)
It launch the activity when your activity is child of any other activity |
3 | startActivityFromChild(Activity child, Intent intent, int requestCode, Bundle options)
It work same as above , but it can take extra values in the shape of bundle with it |
4 | startActivityFromFragment(Fragment fragment, Intent intent, int requestCode)
It launches activity from the fragment you are currently inside |
5 | startActivityFromFragment(Fragment fragment, Intent intent, int requestCode, Bundle options)
It not only launches the activity from the fragment , but can take extra values with it |
There is nothing that you use to launch the activity, they return all the results that can be achieved by overriding the result function.
onActivityResult.
Example
Here's an example that shows how to launch an existing camera app to capture an image and display results as a bitmap
To experiment with this example, you need to run it on the actual device on which the camera is supported.
Steps | Description |
---|---|
1 | You will use Android studio IDE to create an Android application and name it as Camera under a com.example.sairamkrishna.myapplication. 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 | 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/MainActivity.java.
package com.example.sairamkrishna.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);
}
}
Following will be the content of res/layout/activity_main.xml file−
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">
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" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:id="@+id/textView"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24"
android:textSize="35dp" />
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" />
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" />
Following will be the content of res/values/strings.xml to define one new constants
name="app_name">My Application
name="hello_world">Hello world!
name="action_settings">Settings
Following is the default content of AndroidManifest.xml −
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.camera"
android:versionCode="1"
android:versionName="1.0" >
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.example.sairamkrishna.myapplication.MainActivity"
android:label="@string/app_name" >
android:name="android.intent.action.MAIN" />
android:name="android.intent.category.LAUNCHER" />
Try to run your application. I think you are connected to your real Android mobile device with your computer. To run an app from Android Studio, open an activity file from your project and click the Run icon from the toolbar. Before starting your application, Android Studio will display a window to choose an option, where you can run your Android app.