Android Camera API

Android Camera API

Android Camera API We Will Discuss About This. As You Know Well Android Camera  and Android Video Camera are includes in Android Platform, letting you take videos and pictures in your applications. This tutorial discusses an instant, simple approach to video and image capture and outlines an advanced approach for creating custom camera experience for users

These are the next two ways, where you may use camera in the application

  • By Using existing android camera app inside our application ( We Explained in Android Camera )
  • By Using Camera API provided by android inside our app

In This Tutorial We Will Explain Following

By Using Camera API provided by android inside our app

We are using the android camera API to combine the camera inside our android app

First you’ll need to initialize the camera object using the static method provide by the api called Camera.open. Its code is

Camera object = null;
object = Camera.open(); 

 

From the above function aside , there are other functions provided by the Camera class whatever are the following

Methods & Descriptions

getCameraInfo

It comes back the given information in regards to a particular camera

getNumberOfCameras()

It comes back an integer amount defining of cameras on device

lock()

It is employed to lock the camera , so no other program can get access to it

release()

It can be used release a the lock on camera , so other applications can get access to it

open(int cameraId)

It can be used to open up particular camera when multiple video cameras are supported

enableShutterSound

It can be used to permit/disable default shutter audio of image capture

Now you will need make an distinct class and expand it with SurfaceView and implements SurfaceHolder software.

Both classes that contain been used have the next purpose

Class      Descriptions

  • Camera                It can be used to regulate the camera and take images or catch video recording from the camera
  • SurfaceView      This class can be used to provide a live camera preview to an individual.

You must call the preview approach to the camera category to get started on the preview of the camera to an individual

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {
   private Camera theCamera;
   
   public void surfaceCreated(SurfaceHolder holder) {
      theCamera.setPreviewDisplay(holder);
      theCamera.startPreview();
   }
   
   public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3){
   }
   
   public void surfaceDestroyed(SurfaceHolder arg0) {
   }
}	

In addition to the preview there are other options of the camera that may be arranged using the other functions provided by the Android Camera API

Methods & Descriptions

startFaceDetection()

This function begins the true face recognition in the camera

stopFaceDetection()

It is employed to stop the face detection which is enabled by the above function

startSmoothZoom(int value)

It requires an integer value and focus the camera very compared to that value effortlessly

stopSmoothZoom()

It can be used to avoid the focus of the camera

stopPreview()

It can be used to avoid the preview of the camera to an individual

takePicture

It can be used to permit/disable default shutter audio of image capture

Android Camera Api Example

Following android camera api example shows the use of the android camera API in the application form

To test out this android camera api example, you shall need genuine Mobile device outfitted with latest Google android Operating-system, because camera is not recognized by the emulator

Steps     Description

  1. You use Android studio room IDE to generate an Android software and name it as Camera under a package deal com.example.sappal.myapplication
  2. Modify src/MainActivity.java to include the respected code of android camera api.
  3. Modify design XML res/design/activity_main.xml add any GUI aspect if required. Here we add only FrameView and a button and a ImageView.
  4. Modify AndroidManifest.xml as shown below to include the required permissions for android camera api
  5. Run the application form and choose a operating android os device and mount the application onto it and validate the results.

Following is this content of the improved main activity data file src/MainActivity.java.

package com.example.sappal.myapplication;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;

import android.graphics.Bitmap;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;

import android.os.BatteryManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Set;

public class MainActivity extends Activity implements SurfaceHolder.Callback {
   Camera camera;
   SurfaceView surfaceView;
   SurfaceHolder surfaceHolder;
   
   Camera.PictureCallback rawCallback;
   Camera.ShutterCallback shutterCallback;
   Camera.PictureCallback jpegCallback;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
      surfaceHolder = surfaceView.getHolder();
      
      surfaceHolder.addCallback(this);
      surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      
      jpegCallback = new PictureCallback() {
         
         @Override
         public void onPictureTaken(byte[] data, Camera camera) {
            FileOutputStream outStream = null;
               try {
                  outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
                  
                  outStream.write(data);
                  outStream.close();
               }
               
               catch (FileNotFoundException e) {
                  e.printStackTrace();
               }
               
               catch (IOException e) {
                  e.printStackTrace();
               }
               
               finally {
               }
               
               Toast.makeText(getApplicationContext(), "Picture Saved", Toast.LENGTH_LONG).show();
               refreshCamera();
            }
         };
      }
      
      public void captureImage(View v) throws IOException {
         camera.takePicture(null, null, jpegCallback);
      }
      
      public void refreshCamera() {
         if (surfaceHolder.getSurface() == null) {
            return;
         }
         
         try {
            camera.stopPreview();
         }
         
         catch (Exception e) {
         }
         
         try {
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
         }
         catch (Exception e) {
      }
   }
   
   @Override
   protected void onDestroy() {
      super.onDestroy();
   }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
   }
   
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar 
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      
      int id = item.getItemId();
      
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
   
   @Override
   public void surfaceCreated(SurfaceHolder holder) {
      try {
         camera = Camera.open();
      }
      
      catch (RuntimeException e) {
         System.err.println(e);
         return;
      }
      
      Camera.Parameters param;
      param = camera.getParameters();
      param.setPreviewSize(352, 288);
      camera.setParameters(param);
      
      try {
         camera.setPreviewDisplay(surfaceHolder);
         camera.startPreview();
      }
      
      catch (Exception e) {
         System.err.println(e);
         return;
      }
   }
   
   @Override
   public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
      refreshCamera();
   }
   
   @Override
   public void surfaceDestroyed(SurfaceHolder holder) {
      camera.stopPreview();
      camera.release();
      camera = null;
   }
}

Modify the content of the res/layout/activity_main.xml

<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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   
   <TextView android:text="Camera Example" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Andro Development"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />
      
   <SurfaceView
      android:id="@+id/surfaceView"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"/>
      
</RelativeLayout>

Modify the content of the res/values/string.xml

<resources>
   <string name="app_name">My Application</string>
   <string name="hello_world">Hello world!</string>
   <string name="action_settings">Settings</string>
</resources>

Modify the content of the AndroidManifest.xml and add the necessary permissions as given below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.camera1"
   android:versionCode="1"
   android:versionName="1.0" >
   
   <uses-permission android:name="android.permission.CAMERA" />
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
   <uses-feature android:name="android.hardware.camera" />
   <uses-feature android:name="android.hardware.camera.autofocus" />

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.sappal.myapplication.MainActivity"
         android:label="@string/app_name" >
      
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>

Let’s make an effort to run the android camera api application. I suppose you have linked your actual Google android Mobile device with your personal computer. To perform the software from Android studio room, open up one of your project’s activity data and click Run Eclipse Run icon from the toolbar. Prior to starting your application, Google android studio will screen following window to choose a choice where you want to perform your Android request.

android camera api

Choose your mobile device as a choice and then check your mobile device that will display following display:

android camera api

[fbcomments]