Android

Open Camera Instance Asynchronously

Is it possible for opening camera asynchronously? i would say “yes”. By Asynchronous task we can open camera in a separate thread.

Opening camera is an expensive task for every android application. The speed of camera opening depends on the hardware capacity and present memory resource availability. Device with good hardware opens camera quickly.

While opening the camera in main thread will lead UI idle. In the meantime main thread will be busy waiting the camera opening task to finish. It is wasting time and valuable resources from the angle of an efficient programmer. In order to make use of the time we need to perform other important tasks in parallel.

So easiest solution is to perform open camera task in background or in a separate thread other than main thread. We call this task an asynchronous task. It will not disturb main thread which will perform UI related operations.

The newly created asynchronous task will start opening camera in parallel background task after completion it will notify UI.

Code Sample For Opening Camera Asynchronously

import android.app.Activity;
import android.hardware.Camera;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;

public class MyActivity extends Activity {
    
    private Camera mCamera;     
    private Handler customHandler = new Handler(); 
    private ProgressBar mProgressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        mProgressBar = (ProgressBar) findViewById(R.id.progressBarView);
    }      

    //Task to open camera in background
    private class CameraOpenTask extends AsyncTask<Void, Void, Camera> {

        @Override
        protected void onPreExecute() {
        //Show progressbar    
        mProgressBar.setVisibility(View.VISIBLE);      
        }

        @Override
        protected Camera doInBackground(Void... params) {
            return safeCameraOpen(0); //This function will open camera; '0' => back camera
        }

        @Override
        protected void onPostExecute(Camera camera) {
        //hide progress bar
        mProgressBar.setVisibility(View.GONE);
        mCamera = camera;

           
        //Set this camera instance in camera preview 
           
        }
    }

    private Camera safeCameraOpen(int id) {
        
        Camera camera;

        try {
            releaseCameraAndPreview();
            camera = Camera.open(id);
            if (camera != null) {
               return camera;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private void releaseCameraAndPreview() {
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
    }

}
About author

Rojer is a programmer by profession, but he likes to research new things and is also interested in writing. Devdeeds is his blog, where he writes all the blog posts related to technology, gadgets, mobile apps, games, and related content.

Leave a Reply

Your email address will not be published. Required fields are marked *