Android video app camera device setup

The android video app camera device setup tutorial describes how to create and initialise the android  camera2 api CameraDevice which is a representation of the actual device’s camera.

This is one of the steps that has to be done before a connection to the camera can be made.

Get Code

The code to start this tutorial is on github here

https://github.com/mobapptuts/android_camera2_api_video_app.git Tag camera2-video-cameradevice

or you can run this command

git clone https://github.com/mobapptuts/android_camera2_api_video_app.git  –branch camera2-video-cameradevice

Steps

Create members for the CameraDevice & CameraDevice.StateCallback

private CameraDevice mCameraDevice;
    private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback() {
        @Override
        public void onOpened(CameraDevice camera) {

        }

        @Override
        public void onDisconnected(CameraDevice camera) {

        }

        @Override
        public void onError(CameraDevice camera, int error) {

        }
    };

 Initialise the mCameraDevice in the onOpened method and clean up the camera resources in the onDisconnected & onError methods

private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback() {
       @Override
       public void onOpened(CameraDevice camera) {
           mCameraDevice = camera;
       }

       @Override
       public void onDisconnected(CameraDevice camera) {
           camera.close();
           mCameraDevice = null;
       }

       @Override
       public void onError(CameraDevice camera, int error) {
           camera.close();
           mCameraDevice = null;
       }
   };

 Create a closeCamera method to clean up the CameraDevice resources

private void closeCamera() {
      if(mCameraDevice != null) {
          mCameraDevice.close();
          mCameraDevice = null;
      }
  }

 Now call the closeCamera method from the activity’s onPause method

@Override
    protected void onPause() {
        closeCamera();

        super.onPause();
    }

 

Android video app camera device setup summary

In the android video app camera device setup summary tutorial we were introduce to the the CameraDevice member and the CameraDevice StateCallback object which provides a functional CameraDevice.

About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>