The Android framework includes support for various cameras and camera features available on devices, allowing you to capture pictures and videos in your applications. This document discusses a quick, simple approach to image and video capture and outlines an advanced approach for creating custom camera experiences for your users.
Code:
1. MainActivity.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.view.SurfaceView;
import android.widget.TextView;
public class MainActivity extends Activity{
private Camera camera; // camera object
private TextView TimeLeft; // time left field
File mediaFile;
private static final String IMAGE_DIRECTORY_NAME = "Captured_Images";
int numberOfCameras;
private TextView cameras;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TimeLeft=(TextView)findViewById(R.id.TimeLeft); // make time left object
camera = Camera.open();
numberOfCameras = Camera.getNumberOfCameras();
cameras=(TextView)findViewById(R.id.camera);
if(numberOfCameras==0)
cameras.setText("Camera Not Supported in this Mobile");
else if(numberOfCameras==2)
cameras.setText("No Of Cameras:"+numberOfCameras+"\n Both Front & Back Camera Available");
else
cameras.setText("No Of Cameras:"+numberOfCameras+"\n Back Camera only Available");
SurfaceView view = new SurfaceView(this);
try {
camera.setPreviewDisplay(view.getHolder()); // feed dummy surface to surface
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new CountDownTimer(10000,1000){
@Override
public void onFinish() {
// count finished
TimeLeft.setText("Picture Taken");
camera.takePicture(null, null, null, jpegCallBack);
}
@Override
public void onTick(long millisUntilFinished) {
// every time 1 second passes
TimeLeft.setText("Seconds Left: "+millisUntilFinished/1000);
}
}.start();
camera.startPreview();
}
Camera.PictureCallback jpegCallBack=new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(),IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.getDefault()).format(new Date());
mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "IMG_" + timeStamp + ".png");
try {
Bitmap userImage = BitmapFactory.decodeByteArray(data, 0, data.length);
// set file out stream
FileOutputStream out = new FileOutputStream(mediaFile);
// set compress format quality and stream
userImage.compress(Bitmap.CompressFormat.JPEG, 90, out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
2. Update Android Manifest File:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3. activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:id="@+id/camera"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#7ba400"/>
<TextView
android:id="@+id/TimeLeft"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#29166f"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
Output:
Photo has been taken automatically and stored in Captured_Images folder in SD Card.
Code:
1. MainActivity.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.view.SurfaceView;
import android.widget.TextView;
public class MainActivity extends Activity{
private Camera camera; // camera object
private TextView TimeLeft; // time left field
File mediaFile;
private static final String IMAGE_DIRECTORY_NAME = "Captured_Images";
int numberOfCameras;
private TextView cameras;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TimeLeft=(TextView)findViewById(R.id.TimeLeft); // make time left object
camera = Camera.open();
numberOfCameras = Camera.getNumberOfCameras();
cameras=(TextView)findViewById(R.id.camera);
if(numberOfCameras==0)
cameras.setText("Camera Not Supported in this Mobile");
else if(numberOfCameras==2)
cameras.setText("No Of Cameras:"+numberOfCameras+"\n Both Front & Back Camera Available");
else
cameras.setText("No Of Cameras:"+numberOfCameras+"\n Back Camera only Available");
SurfaceView view = new SurfaceView(this);
try {
camera.setPreviewDisplay(view.getHolder()); // feed dummy surface to surface
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new CountDownTimer(10000,1000){
@Override
public void onFinish() {
// count finished
TimeLeft.setText("Picture Taken");
camera.takePicture(null, null, null, jpegCallBack);
}
@Override
public void onTick(long millisUntilFinished) {
// every time 1 second passes
TimeLeft.setText("Seconds Left: "+millisUntilFinished/1000);
}
}.start();
camera.startPreview();
}
Camera.PictureCallback jpegCallBack=new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(),IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.getDefault()).format(new Date());
mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "IMG_" + timeStamp + ".png");
try {
Bitmap userImage = BitmapFactory.decodeByteArray(data, 0, data.length);
// set file out stream
FileOutputStream out = new FileOutputStream(mediaFile);
// set compress format quality and stream
userImage.compress(Bitmap.CompressFormat.JPEG, 90, out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
2. Update Android Manifest File:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3. activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:id="@+id/camera"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#7ba400"/>
<TextView
android:id="@+id/TimeLeft"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#29166f"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
Output:
Photo has been taken automatically and stored in Captured_Images folder in SD Card.
thank so much....it's run :) merci
ReplyDeletethank you... if you have any doubt send mail to krsaravanan22@gmail.com
DeleteHello, how i can use front cam?
ReplyDeleteHello, how i can use front cam?
ReplyDeletecamera not showing .what i do ?
ReplyDeleteCan you explain how to take min 10 photos automatically
ReplyDeleteIt is not working...
ReplyDeleteError -- takePicture failed
Plz help me sir
need ur device info like android version & you need to access front camera or back camera?
ReplyDeletegetting error in takepicture,please help me out
ReplyDelete