5

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

Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37
Stella
  • 1,817
  • 6
  • 30
  • 55

2 Answers2

2

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));
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
  • While testing on front camera, the video does not seem centred to the face even when aligned in that manner. – prex Apr 06 '20 at 07:12
  • @prex I would suggest you go through - https://developer.android.com/reference/android/hardware/camera2/CameraCharacteristics. CamAPI2 up to date samples available in the android official repo - https://github.com/android/camera-samples. You might find this link also helpful- https://androidwave.com/video-recording-with-camera2-api-android. Please read through it and feel free to get in touch if you have any concerns. Bit busy at the moment, may take awhile to come up with the precise solution in respective to ur problem. – Anoop M Maddasseri Apr 06 '20 at 10:23
  • I am using the latest repo and Kotlin version. Thanks for the links. – prex Apr 06 '20 at 20:08
  • This is the link to my question. https://stackoverflow.com/questions/60824076/full-screen-video-recording-in-front-camera-using-camera2-api?noredirect=1&lq=1 – prex Apr 06 '20 at 20:09
1

Maybe you could implement a transform method that scales and rotates the TextureView to the correct orientation.

private void transformImage(int width, int height) {
    if (mPreviewSize == null || mTextureView == null) {
        return;
    }
    Matrix matrix = new Matrix();
    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    RectF textureRectF = new RectF(0, 0, width, height);
    RectF previewRectF = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
    float centerX = textureRectF.centerX();
    float centerY = textureRectF.centerY();
    if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
        previewRectF.offset(centerX - previewRectF.centerX(),
                centerY - previewRectF.centerY());
        matrix.setRectToRect(textureRectF, previewRectF, Matrix.ScaleToFit.FILL);
        float scale = Math.max((float) width / mPreviewSize.getWidth(),
                (float) height / mPreviewSize.getHeight());
        matrix.postScale(scale, scale, centerX, centerY);
        matrix.postRotate(90 * (rotation - 2), centerX, centerY);
    }
    mTextureView.setTransform(matrix);
}

After that you should call transform method from onResume:

public void onResume() {
    super.onResume();
    if (mTextureView.isAvailable()) {
        transformImage(mTextureView.getWidth(), mTextureView.getHeight());
    } else {
        mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
    }
}

And on SurfaceTextureListener.Callback:

private TextureView.SurfaceTextureListener mSurfaceTextureListener =
        new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                transformImage(width, height);
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {
            }
        };

Source.

Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37