1. Switch camera rear to front & front-rear in Camera2 API

In previous 2 blogs, I have explained Video Recoding using Camera2 API. Furtermore In this tutorials I’m explaing about swich camera front-rear via-varsa. As you we have build a CameraVideoFragment.java utility class.

2. Declare below variable in CameraVideoFragment.java.

    /* 0 forback camera 
     * 1 for front camera 
     * Initlity default camera is front camera    
    public static final String CAMERA_FRONT = "1";
    public static final String CAMERA_BACK = "0";
    private String cameraId = CAMERA_FRONT;

3. Check available cameras

We can fetch all available cameras from Camera Manager instase by calling getCameraIdList .It’s return all available via getCameraIdList() (front/rear), then you can find the best camera that you want to use or suits your need.

You can get CameraCharacteristics by using camera ID. You can manage facing, resolution and filter also. In variable declear block we define front and back camera id.

In this tutorial, the front camera is the default camera.

   CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

As above private String cameraId = CAMERA_FRONT; we set default camera is front camera, So cameraId have instance of front camera.

4. Furthermore we can Switch Camera usign below code

 public void switchCamera() {
        if (cameraId.equals(CAMERA_FRONT)) {
            cameraId = CAMERA_BACK;

        } else if (cameraId.equals(CAMERA_BACK)) {
            cameraId = CAMERA_FRONT;


After changing cameraId we just close the previous camera and reopen it.

 public void reopenCamera() {
        if (mTextureView.isAvailable()) {
            openCamera(mTextureView.getWidth(), mTextureView.getHeight());
        } else {

Finally just call switchCamera() method in child Fragment

Download Sample Project- Switch Camera in Camera2 API Android

If you have any query, feel free to connect us.