JustPaste.it

Front camera rotate

have used custom video recording using Camera2Video :

 

There Front camera rotation is not available.Here we can achieve front camera rotation.

 

Just add the below code to get the expected result:

 

 

Camera2VideoFragment.java:

 

String cameraId;  // take this cameraId as global

 

public static String ROTATE = null;

 

ImageView ivRotateFront, ivRotateBack;

 

   @Override
    public void onViewCreated(final View view, Bundle savedInstanceState) {
        mTextureView = (AutoFitTextureView) view.findViewById(R.id.texture);
        mButtonVideo = (Button) view.findViewById(R.id.video);
        ivRotateFront = (ImageView)view.findViewById(R.id.iv_rotate_front);
        ivRotateBack = (ImageView)view.findViewById(R.id.iv_rotate_back);

 

        mButtonVideo.setOnClickListener(this);
        ivRotateFront.setOnClickListener(this);
        ivRotateBack.setOnClickListener(this);
        view.findViewById(R.id.info).setOnClickListener(this);
    }

 

     case R.id.iv_rotate_front: {

 

                ivRotateFront.setVisibility(View.GONE);
                ivRotateBack.setVisibility(View.VISIBLE);

 

                closeCamera();
                stopBackgroundThread();

 

                startBackgroundThread();
                if (mTextureView.isAvailable()) {

 

                    ROTATE = "fulfilled";

 

                    Log.e("Rotate", ""+ROTATE);

 

                    openCamera(mTextureView.getWidth(), mTextureView.getHeight());

 

                } else {
                    mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
                }

 

                break;
            }

 

            case R.id.iv_rotate_back: {

 

                Log.e("ClickBack", "Test");

 

                ivRotateFront.setVisibility(View.VISIBLE);
                ivRotateBack.setVisibility(View.GONE);

 

                closeCamera();
                stopBackgroundThread();

 

                startBackgroundThread();
                if (mTextureView.isAvailable()) {
                    openCamera(mTextureView.getWidth(), mTextureView.getHeight());
                } else {
                    mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
                }

 

                break;
            }

 

      case R.id.video: {
                if (mIsRecordingVideo) {

 

                    ivRotateBack.setClickable(true);
                    ivRotateFront.setClickable(true);
                    stopRecordingVideo();
                } else {

 

                    ivRotateBack.setClickable(false);
                    ivRotateFront.setClickable(false);

 

                    startRecordingVideo();
                }
                break;
            }

 

 

 private void openCamera(int width, int height) {

 

        try {
         
            if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
                throw new RuntimeException("Time out waiting to lock camera opening.");
            }

 

            if(ROTATE != null){

 

                Log.e("FrontCamera", "Test");

 

                 cameraId = manager.getCameraIdList()[1];

 

                ROTATE = null;

 

            } else {

 

                Log.e("BackCamera", "Test");

 

                 cameraId = manager.getCameraIdList()[0];

 

            }

 

        } catch (CameraAccessException e) {
            Toast.makeText(activity, "Cannot access the camera.", Toast.LENGTH_SHORT).show();
            activity.finish();
        }
       
    }
  

fragment_camera2_video.xml:

 

 

<?xml version="1.0" encoding="utf-8"?>

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 

    <com.example.android.camera2video.AutoFitTextureView
        android:id="@+id/texture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

 

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_below="@id/texture"
        android:background="#4285f4">

 

        <Button
            android:id="@+id/video"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/record" />

 

 

        <ImageView
            android:id="@+id/iv_rotate_front"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/rotate_white"
            android:layout_gravity="start|center_vertical"
            android:visibility="visible"
           />

 

 

        <ImageView
            android:id="@+id/iv_rotate_back"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/rotate_black"
            android:layout_gravity="start|center_vertical"
            android:visibility="gone"
            />

 

        <ImageButton
            android:id="@+id/info"
            android:contentDescription="@string/description_info"
            style="@android:style/Widget.Material.Light.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|right"
            android:padding="20dp"
            android:src="@drawable/ic_action_info" />

 

    </FrameLayout>
</RelativeLayout>