Wednesday 17 April 2013

Camera Demo

Demo:




1) Activity:

package com.venky.camerademo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class CameraDemoActivity extends Activity {
      /** Called when the activity is first created. */

      ImageView iv;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

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

            Button btnTakeImage = (Button) findViewById(R.id.button1);

            btnTakeImage.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View arg0) {
                        // TODO Auto-generated method stub

                        Intent cameraIntent = new Intent(
                                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                        startActivityForResult(cameraIntent, 1);
                  }
            });
      }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == 1) {

                  if (requestCode == RESULT_OK) {
                        Bitmap image = (Bitmap) data.getExtras().get("data");
                        iv.setImageBitmap(image);

                        Log.e("Camera", "OK");

                  }
                  if (resultCode == RESULT_CANCELED) {

                        Toast.makeText(getApplicationContext(),
                                    "Error while opening Camera", 5000).show();
                  }

            }
      }
}


2)main.xml:
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#5F04B4"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="Take Image"
        android:textSize="20dp" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp" />

</LinearLayout>


3)Download this Project Click Here
 

No comments:

Post a Comment