Home Android Capturing Image and Video from Camera-Android

Capturing Image and Video from Camera-Android

65
0
SHARE

The Android framework includes support for various cameras and camera features available on devices, allowing you capturing pictures and videos in your applications.

This example helps you make your own application to capture images and videos and save it to your gallery.

NOTE :  This project is developed in Android Studio, and tested in Android Marshmallow 6.0 .

Suppose you are implementing a crowd-sourced acclimate account that makes a all-around acclimate map by aggregate calm pictures of the sky taken by accessories active your applicant app. Integrating photos is alone a baby allotment of your application. You wish to yield photos with basal fuss, not reinvent the camera. Happily, a lot of Android-powered accessories already accept at atomic one camera appliance installed. In this lesson, you apprentice how to accomplish it yield a account for you. If your appliance uses, but does not crave a camera in adjustment to function, instead set android:required to false. In accomplishing so, Google Play will acquiesce accessories after a camera to download your application. It’s again your albatross to analysis for the availability of the camera at runtime by calling hasSystemFeature(PackageManager.FEATURE_CAMERA). If a camera is not available, you should again attenuate your camera features.

The Android way of delegating accomplishments to added applications is to adjure an Intent that describes what you wish done. This action involves three pieces: The Intent itself, a alarm to alpha the alien Activity, and some cipher to handle the angel abstracts if focus allotment to your activity.

1. Android Layout Files

In your .xml file whose path is ” res/layout/activity_main.xml ” , edit the following UI components or you can choose yourself.

File : activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.cloudinfinity.camera.MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/imageButton"
        android:layout_alignParentEnd="true" />

    <ImageButton
        android:layout_width="140dp"
        android:layout_height="100dp"
        android:id="@+id/imageButton"
        android:src="@drawable/camera"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true" />

    <ImageButton
        android:layout_width="140dp"
        android:layout_height="100dp"
        android:id="@+id/imageButton2"
        android:src="@drawable/video"
        android:layout_alignBottom="@+id/imageButton"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/imageView" />
</RelativeLayout>

 

2. Activity Code (Camera Activity)

File : MainActivity.java

package com.example.cloudinfinity.camera;

import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ImageButton b1,b2;
    ImageView iv;
    private static final int VIDEO_REQUEST = 200;
    private static final int CAMERA_REQUEST = 123;

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

        b1=(ImageButton)findViewById(R.id.imageButton);
        b2=(ImageButton)findViewById(R.id.imageButton2);
        iv=(ImageView)findViewById(R.id.imageView);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(i,CAMERA_REQUEST);
            }
        });

        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                startActivityForResult(i,VIDEO_REQUEST);
            }
        });
    }

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

        if(requestCode==CAMERA_REQUEST && resultCode==RESULT_OK)
        {
            Bitmap bitmap=(Bitmap)data.getExtras().get("data");
            iv.setImageBitmap(bitmap);
            Toast.makeText(this,"Image Saved in Gallery",Toast.LENGTH_SHORT).show();
        }

        if(requestCode==VIDEO_REQUEST && resultCode==RESULT_OK)
        {
            Toast.makeText(this,"Video Saved in Gallery",Toast.LENGTH_SHORT).show();
        }
    }
}

 

3. Android Manifest

Application required Camera hardware permission, add below into AndroidManifest.xml.

<uses-feature android:name="android.permission.CAMERA"></uses-feature>

 

4. Run the application

Here, this will look like;

capturing

LEAVE A REPLY

Please enter your comment!
Please enter your name here

17 + two =