Android camera2 API CameraDevice

android camer2 api open cameradevice

Describes how to open the android camera2 api CameraDevice

Part 3 of the camera2 API tutorial series which concentrates opening the android camera2 api cameradevice. This is needed before CaptureRequests & CameraCaptureSessions can be started.

Code available on github

You can download the code by cloning this url from github https://github.com/mobapptuts/recyclerview_image_gallery.git and the code changes are under the Tag “cameradevice”

Or else you can run this command

git clone --branch cameradevice https://github.com/mobapptuts/recyclerview_image_gallery.git

Create CameraDevice & CameraDevice.StateCallback methods

CamaraIntentActivity

private CameraDevice mCameraDevice;
private CameraDevice.StateCallback mCameraDeviceStateCallback
        = new CameraDevice.StateCallback() {
    @Override
    public void onOpened(CameraDevice camera) {
        mCameraDevice = camera;
        Toast.makeText(getApplicationContext(), "Camera Opened!", Toast.LENGTH_SHORT).show();
    }

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

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

Create openCamera method

private void openCamera() {
        CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        try {
            cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

Call the openCameraMethod when the surface texture is available

@Override
                public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                    setupCamera(width, height);
                    openCamera();
                }

Run & check the CameraDevice is opened successfully

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>