Search code examples
androidcamera-api

Camera 2 : Unable to record video in full screen?


I'm using google official sample to work around video recording by using Camera API 2 . but the problem is I'm not able to make it full screen as I limited the screen orientation only to portrait.Here the xml I edited and the official 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">

    <AutoFitTextureView
        android:id="@+id/texture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        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_alignParentLeft="true"
        android:layout_alignParentStart="true">

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

        <ImageView
            android:id="@+id/info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|right"
            android:contentDescription="Description"
            android:padding="20dp"
            android:src="@drawable/ic_more" />

    </FrameLayout>

</RelativeLayout>

I think here is setting the video screen size by aspect ration to the TextureView but I couldn't make it to full screen. Any help is greatly appreciated.

mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), width, height, mVideoSize);
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
    mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight());
} else {
    mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth());
}

enter image description here


Solution

  • You need to deal with preview size to control over the screen size of recording . use the same chooseVideoSize method to make your preview full screen by obtaining the Output Sizes of SurfaceTexture class.

    private static Size chooseVideoSize(Size[] choices) {
            for (Size size : choices) {
                if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) {
                    return size;
                }
            }
            Log.e(TAG, "Couldn't find any suitable video size");
            return choices[choices.length - 1];
        }
    

    that is

      mPreviewSize = chooseVideoSize(map.getOutputSizes(SurfaceTexture.class));