Wednesday, 29 June 2016

Android Camera

Camera is mainly used to capture picture and video. We can control the camera by using methods of camera api.




activity_main.xml
---------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
tools:context="com.sgp.anil.camera.MainActivity">

    <
Button
       
android:id="@+id/button_camera"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_gravity="center_horizontal"
       
android:text="Camera" />

    <
ImageView
       
android:id="@+id/display_imageView"
       
android:layout_width="350dp"
       
android:layout_height="350dp"
       
android:layout_gravity="center" />
</
LinearLayout>

MainActivity.Java
----------------------
package com.sgp.anil.camera;

import android.Manifest;
import android.content.Intent;import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    Button
camerabtn;
    ImageView
imageView;
   
int REQUEST_CAMERA = 1;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
       
camerabtn = (Button) findViewById(R.id.button_camera);
       
imageView = (ImageView) findViewById(R.id.display_imageView);
       
camerabtn.setOnClickListener(new View.OnClickListener() {
            
@Override
           
public void onClick(View v) {
                

                    Intent intent =
new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent,
0);
               

            }
        });
    }

   
@Override
   
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Bitmap bp = (Bitmap) data.getExtras().get(
"data");
        
imageView.setImageBitmap(bp);
       
super.onActivityResult(requestCode, resultCode, data);
    }

   

    }
}



No comments:

Post a Comment