Friday, February 17, 2012

How to Capture image through camera in android application?

In Manifest mention the following line...
<uses-feature android:name="android.hardware.camera"></uses-feature>
<uses-permission name="android.permission.CAMERA"></uses-permission>

Create the Activity with button and Imageview.........





import java.io.File;


import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;


public class OpenCamera extends Activity {
private static final int CAMERA_PIC_REQUEST = 2500;
Uri imageUri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.btn_end);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {


// define the file-name to save photo taken by Camera activity
String fileName = String.valueOf(System.currentTimeMillis() + ".jpg");
String directoryPath="sdcard/a/b/";
File file = new File(directoryPath,fileName);
imageUri = Uri.fromFile(file);

// create parameters for Intent with filename
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");

// imageUri is the current activity attribute, define and save
// it for later usage (also in onSaveInstanceState)
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
});
}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
if (resultCode == RESULT_OK) {
// use imageUri here to access the image because now the image
// is saved in //MediaStore table
File filePath=convertImageUriToFile(imageUri, OpenCamera.this);


// OR else you can get through extra's key data


Bitmap image = (Bitmap) data.getExtras().get("data");


ImageView imageview = (ImageView) findViewById(R.id.ImageView01);


imageview.setImageBitmap(image);


} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken",
Toast.LENGTH_SHORT);
} else {
Toast.makeText(this, "Picture was not taken",
Toast.LENGTH_SHORT);
}
}
}


public static File convertImageUriToFile(Uri imageUri, Activity activity) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID,
MediaStore.Images.ImageColumns.ORIENTATION };
cursor = activity.managedQuery(imageUri, proj, // Which columns to
// return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int file_ColumnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
int orientation_ColumnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
if (cursor.moveToFirst()) {
String orientation = cursor.getString(orientation_ColumnIndex);
String sdcardPath = cursor.getString(file_ColumnIndex);
return new File(cursor.getString(file_ColumnIndex));
}
return null;
} finally {
if (cursor != null) {
cursor.close();
}
}
}


}

1 comment: