Monday 23 February 2015

Android Camera Application

In this blog, I will show how to use default camera application in our application

Java Code:

Sample.java

package com.example.eventorganize;

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

public class Sample extends Activity {
    ImageView imgphoto;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample);
        imgphoto = (ImageView) findViewById(R.id.imgphoto);
        imgphoto.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                capture();
            }
        });
    }
    protected void capture() {
        // TODO Auto-generated method stub
        Intent intent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 0);
    }
   
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        System.err.println("code:" + data);
        if (data != null) {
            Bitmap bp = (Bitmap) data.getExtras().get("data");
            imgphoto.setImageBitmap(bp);
        }
    }
}


activity_sample.xml

 <ImageView
        android:id="@+id/imgphoto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:background="@drawable/capture"
        android:contentDescription="@string/app_name"
        android:scaleType="fitXY" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="@string/tap"
        android:textAppearance="?android:attr/textAppearanceLarge" />


strings.xml

<string name="tap">Tap here to capture Photo</string>


Screen Shots:






No comments:

Post a Comment