Friday, 11 March 2016

Android Camera

Camera is mainly used to capture picture and video. We can control the camera by using methods of camera api.
Android provides the facility to work on camera by 2 ways:
  1. By Camera Intent
  2. By Camera API



activity_main.xml

Drag one imageview and one button from the pallete, now the xml file will look like this:
File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.    <Button  
  8.         android:id="@+id/button1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentBottom="true"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:text="Take a Photo" >  
  14.     </Button>  
  15.   
  16.     <ImageView  
  17.         android:id="@+id/imageView1"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="fill_parent"  
  20.         android:layout_above="@+id/button1"  
  21.         android:layout_alignParentTop="true"  
  22.         android:src="@drawable/ic_launcher" >  
  23.     </ImageView>  
  24. </RelativeLayout>  

File: MainActivity.java
  1. package com.example.simplecamera;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.graphics.Bitmap;  
  6. import android.os.Bundle;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.ImageView;  
  11.   
  12. public class MainActivity extends Activity {  
  13.      private static final int CAMERA_REQUEST = 1888;  
  14.      ImageView imageView;  
  15.      public void onCreate(Bundle savedInstanceState) {  
  16.   
  17.          super.onCreate(savedInstanceState);  
  18.          setContentView(R.layout.activity_main);  
  19.   
  20.          imageView = (ImageView) this.findViewById(R.id.imageView1);  
  21.          Button photoButton = (Button) this.findViewById(R.id.button1);  
  22.   
  23.          photoButton.setOnClickListener(new View.OnClickListener() {  
  24.   
  25.          @Override  
  26.          public void onClick(View v) {  
  27.               Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
  28.               startActivityForResult(cameraIntent, CAMERA_REQUEST);  
  29.          }  
  30.         });  
  31.        }  
  32.   
  33.      protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  34.       if (requestCode == CAMERA_REQUEST) {  
  35.        Bitmap photo = (Bitmap) data.getExtras().get("data");  
  36.        imageView.setImageBitmap(photo);  
  37.       }  
  38.    }  
  39.   
  40.     @Override  
  41.     public boolean onCreateOptionsMenu(Menu menu) {  
  42.         // Inflate the menu; this adds items to the action bar if it is present.  
  43.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  44.         return true;  
  45.     }  
  46.   
  47. }  






No comments:

Post a Comment