1) Create a following xml file in res - layout
camera_image.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- Give the path name when your Preview.java File current location available-->
<com.vl.mobile.components.camera.Preview
android:id="@+id/camera_panel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<RelativeLayout
android:id="@+id/button_panel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/camback_button"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
/>
<ImageView
android:id="@+id/capture_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:background="@drawable/camerabutton"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/image_panel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:visibility="gone"
>
<ImageView
android:id="@+id/image_captured"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<ImageView
android:id="@+id/image_back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/camback_button"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
/>
<ImageView
android:id="@+id/image_save_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/save_button"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</RelativeLayout>
2) Create following file in drawable folder
camerabutton.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="@drawable/capture_button" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/capture_button" />
<item
android:state_enabled="true"
android:drawable="@drawable/takephoto_button" />
</selector>
3)Create following .java file in your Package
Preview.java
package com.vl.mobile.components.camera;// Your Package Name
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class Preview extends SurfaceView implements SurfaceHolder.Callback
{
public SurfaceHolder mHolder;
public Camera mCamera;
private int cameraid;
Context objContext;
static int rotation = 90;
public Preview(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
objContext = context;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
checkCamera();
}
public Preview(Context context, AttributeSet attrs)
{
super(context, attrs);
objContext = context;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
checkCamera();
}
public Preview(Context context)
{
super(context);
objContext = context;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
checkCamera();
}
public void surfaceCreated(SurfaceHolder holder)
{
if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.FROYO){
mCamera = Camera.open();
Camera_Image.presentBackCamera = true;
Camera_Image.presentFrontCamera = false;
}
else if (Camera_Image.presentBackCamera){
mCamera = Camera.open(cameraid);
Camera_Image.presentBackCamera = true;
Camera_Image.presentFrontCamera = false;
}
else if (Camera_Image.presentFrontCamera){
mCamera = Camera.open(cameraid);
Camera_Image.presentBackCamera = false;
Camera_Image.presentFrontCamera = true;
}
try
{
mCamera.setPreviewDisplay(holder);
}
catch (IOException exception)
{
mCamera.release();
mCamera = null;
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
if(mCamera != null)
{
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
if(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.FROYO)
{
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraid, info);
rotation = info.orientation; // rotet the sarface view
}
else
rotation = 0;
Camera.Parameters parameters1 = mCamera.getParameters();
parameters1.setRotation(90);
mCamera.setParameters(parameters1);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
}
// Check No of camera availabel in device
public void checkCamera()
{
if(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.FROYO)
{ // for check api version is grater then 2.2
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < Camera.getNumberOfCameras(); i++)
{
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK)
{ // For Back Camera
Camera_Image.presentBackCamera = true;
cameraid=i;
break;
}
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT)
{ // For Front Camera
Camera_Image.presentFrontCamera = true;
cameraid=i;
}
}
}
else
{
Camera_Image.presentBackCamera = true;
Camera_Image.presentFrontCamera = false;
}
}
}
Camera_Image.java
package com.vl.mobile.components.camera;// Your Package Name
import java.io.File;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.vl.mobile.gsp.Constants;
import com.vl.mobile.gsp.R;
public class Camera_Image extends Activity{
// Private Variable Declaration
public Preview mPreview;
public ImageView backButton;
public ImageView captureButton;
public ImageView imageBackButton;
public ImageView imageSaveButton;
public ImageView imageCaptured;
public static boolean presentBackCamera;
public static boolean presentFrontCamera;
public RelativeLayout imagePanel, buttonPanel;;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Remove a TitleBar
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.camera_image);
mPreview = (Preview)findViewById(R.id.camera_panel);
buttonPanel = (RelativeLayout)findViewById(R.id.button_panel);
imagePanel = (RelativeLayout)findViewById(R.id.image_panel);
backButton = (ImageView)findViewById(R.id.back_button);
captureButton = (ImageView)findViewById(R.id.capture_button);
imageBackButton = (ImageView)findViewById(R.id.image_back_button);
imageSaveButton = (ImageView)findViewById(R.id.image_save_button);
imageCaptured = (ImageView)findViewById(R.id.image_captured);
captureButton.setClickable(true);
captureButton.setEnabled(true);
backButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
setResult(RESULT_CANCELED);
finish();
}
});
// Capture a Picture
captureButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(Camera_Image.this, "please wait...", Toast.LENGTH_SHORT).show();
captureButton.setClickable(false);
captureButton.setEnabled(false);
mPreview.mCamera.autoFocus(new CustomAutoFoucsCallback(Camera_Image.this, handler));
}
});
// Save a Picture
imageSaveButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
setResult(RESULT_OK);
finish();
}
});
imageBackButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
imagePanel.setVisibility(View.GONE);
buttonPanel.setVisibility(View.VISIBLE);
captureButton.setBackgroundResource(R.drawable.capture_button);
mPreview.mCamera.startPreview();
}
});
}
private Handler handler = new Handler()
{
@Override
public void handleMessage(android.os.Message msg)
{
Bitmap myBitmap = null;
Drawable dr = null;
File imgFile = null;
try
{
if (msg.arg1 == 0)// if response is true
{
imgFile = new File(Constants.CAPTURED_IMAGE);
if(imgFile.exists()){
Toast.makeText(Camera_Image.this, "done", Toast.LENGTH_SHORT).show();
myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
dr = new BitmapDrawable(myBitmap);
imagePanel.setBackgroundDrawable(dr);
imagePanel.setVisibility(View.VISIBLE);
buttonPanel.setVisibility(View.GONE);
}
else
{
Toast.makeText(Camera_Image.this, "done but image not found", Toast.LENGTH_LONG).show();
}
}
else if (msg.arg1 == 1)
{
Toast.makeText(Camera_Image.this, "camera bitmap not found", Toast.LENGTH_LONG).show();
}
else if (msg.arg1 == 2)
{
Toast.makeText(Camera_Image.this, "exception while capturing", Toast.LENGTH_LONG).show();
}
}
catch (OutOfMemoryError e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
myBitmap = null;
dr = null;
imgFile = null;
}
};
}
CustomAutoFoucsCallback.java
package com.vl.mobile.components.camera;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.os.Handler;
public class CustomAutoFoucsCallback implements AutoFocusCallback
{
public Activity activity;
public Handler handler;
public CustomAutoFoucsCallback(Activity activty, Handler handler)
{
this.activity = activty;
this.handler = handler;
}
public void onAutoFocus(boolean success, Camera camera)
{
try
{
this.takePicture(camera);
}catch (Exception e) {
e.printStackTrace();
}
}
public void takePicture(Camera camera)
{
try
{
camera.takePicture(
null,
null,
new CustomPictureCallback(handler));
}
catch (Exception e)
{
e.printStackTrace();
}
catch(OutOfMemoryError e)
{
e.printStackTrace();
}
}
}
CustomPictureCallback.java
package com.vl.mobile.components.camera;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import com.vl.mobile.gsp.Constants;
import com.vl.mobile.gsp.util.Storage;
public class CustomPictureCallback implements PictureCallback {
public Handler handler;
public CustomPictureCallback(Handler handler) {
this.handler = handler;
}
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
Bitmap cameraBitmap = null;
Message msg = null;
Matrix matrix = null;
try {
msg = new Message();
//successful completion
msg.arg1 = 0;
if (data != null)
{
// Capture Camera Image and Store it to File
cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
int rotation = 90 - Preview.rotation;
matrix = new Matrix();
if(rotation < 0)
matrix.postScale(-1F, 1F);
else
matrix.postScale(1F, 1F);
matrix.postRotate(rotation);
try
{
cameraBitmap = Bitmap.createBitmap(cameraBitmap, 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), matrix, true);
}
catch (OutOfMemoryError e)
{
e.printStackTrace();
msg.arg1 = 2;
}
// Chcek your folder is exist or not
//create a globel variable CAPTURE_IMAGE
//public static String CAPTURED_IMAGE;
//imagePath=Environment.getExternalStorageDirectory().getPath() + "/PhotoList "
File dir = new File((Ex.imagePath)Your Folder name When you save your Image);
if (!dir.exists())
{
dir.mkdirs();
}
dir = null;
Constants.CAPTURED_IMAGE = imagePath+ "/"
+ System.currentTimeMillis() + ".jpg"; // Give the Path When u save Your Image File
outStream = new FileOutputStream(Constants.CAPTURED_IMAGE );
cameraBitmap.compress(CompressFormat.JPEG, 100, outStream);
cameraBitmap=null;
}else{
//Camera bitmap not found
msg.arg1 = 1;
}
}
catch (OutOfMemoryError e)
{
//exception while capturing
e.printStackTrace();
msg.arg1 = 2;
}
catch (Exception e)
{
//exception while capturing
e.printStackTrace();
msg.arg1 = 2;
}
finally
{
if (outStream != null)
{
try
{
outStream.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
// Clean-up process
outStream = null;
cameraBitmap = null;
data=null;
matrix = null;
this.handler.sendMessage(msg);
}
}