How to capture image from camera in android | Camera Capture code in android

1-Android manifest file

<?xml version=“1.0” encoding=“utf-8”?>

<manifest xmlns:android=http://schemas.android.com/apk/res/android&#8221;

package=“com.camera.demo”

android:versionCode=“1”

android:versionName=“1.0” >

<uses-sdk android:minSdkVersion=“8” />

<application

android:icon=“@drawable/ic_launcher”

android:label=“@string/app_name” >

<activity

android:label=“@string/app_name”

android:name=“.CameraDemoActivity” >

<intent-filter >

<action android:name=“android.intent.action.MAIN” />

<category android:name=“android.intent.category.LAUNCHER” />

</intent-filter>

</activity>

</application>

</manifest>
2- CameraDemoActivity.java


package com.camera.demo;

import android.app.Activity;

import android.content.ActivityNotFoundException;

import android.content.Intent;

import android.graphics.Bitmap;

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;

public class CameraDemoActivity extends Activity {

protected static final int CAMERA_REQUEST = 1;

ImageView imgView;

Button btnCamera;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

imgView = (ImageView) findViewById(R.id.imageView1);

btnCamera = (Button) findViewById(R.id.btn_camera);

btnCamera.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

// ******** code for take image

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT,

MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());

try {

intent.putExtra(“return-data”, true);

startActivityForResult(intent, CAMERA_REQUEST);

} catch (ActivityNotFoundException e) {

// Do nothing for now

}

}

});

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == CAMERA_REQUEST) {

Bundle extras = data.getExtras();

if (extras != null) {

Bitmap photo = extras.getParcelable(“data”);

// display image in ImageView.

imgView.setImageBitmap(photo);

// saveBitmapToFile(“/sdcard/crop/cropped_img.jpg”, photo);

}

}

}

}

3-main.xml

<?xml version=“1.0” encoding=“utf-8”?>

<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android&#8221;

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:orientation=“vertical” >

<Button

android:id=“@+id/btn_camera”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center”

android:text=“Take image” />

<ImageView

android:id=“@+id/imageView1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center” />

</LinearLayout>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s