How to make video captured by front camera not being inverse Android?

ken picture ken · Nov 9, 2017 · Viewed 25.7k times · Source

I recording video using MediaRecorder.When using back-camera,it working fine,but when using front camera,the video captured is being flipped/inverse.Means that the item in right,will appear on the left.The camera preview is working fine,just final captured video flipped.

Here is the camera preview looks like

enter image description here

But the final video appear like this(all the item in left hand side,appear on right hand side)

enter image description here

What I tried so far:

I tried to apply the matrix when prepare recorder,but it seems does change anything.

private boolean prepareRecorder(int cameraId){

    //# Create a new instance of MediaRecorder
    mRecorder = new MediaRecorder();

    setCameraDisplayOrientation(this,cameraId,mCamera);
    int angle = getVideoOrientationAngle(this,cameraId);
    mRecorder.setOrientationHint(angle);

    if(cameraId == Camera.CameraInfo.CAMERA_FACING_FRONT){
        Matrix matrix = new Matrix();
        matrix.preScale(1.0f,-1.0f);
    }

    //all other code to prepare recorder here
  }

I already read for all this question below,but all this seems didnt solve my problem.For information,I using SurfaceView for the camera preview,so this question here doesn't help.

1) Android flip front camera mirror flipped video

2) How to keep android from inverting the image from the front facing camera?

3) Prevent flipping of the front facing camera

So my question is :

1) How to capture a video by front camera which the video not being inverse(exactly the same with camera preview)?

2) How to achieve this when the Camera preview is using SurfaceView but not TextureView ? (cause all the question I mention above,tell about using TextureView)

All possible solution is mostly welcome..Tq

EDIT

I made 2 short video clip to clarify the problem,please download and take a look

1) The video during camera preview of recording

2) The video of the final product of recording

Answer

Alex Cohn picture Alex Cohn · Nov 12, 2017

So, if the system camera app produces video similar to your app, you didn't do something wrong. Now it's time to understand what happens to front-facing camera video recording.

The front facing camera is not different from the rear facing camera in the way it captures still pictures or video. There is a difference how the phone displays camera preview on the screen. To make it look more natural to the user, Android (and all other systems) mirrors the preview, so that you can see yourself as if in a mirror.

It is important to understand that this only applies to the way the preview is presented to you. If you pick up any video conferencing app, connect two devices that you hold in two hands, and look at yourself, you will see to your surprise that the two instances of yourself are flipped.

This is not a bug, this is the natural way to present the video to the other party.

See the sketch:

This is how you see the scene:

how you see the scene

This is how your peer sees the same scene

how your peer sees the same scene

Normally, recording of a video is done from the point if view of your peer, as in the second picture. This is the natural setup for, e.g., video conferencing.

But Snapchat and some other social apps choose to store the front-facing video clip as if you record it from the mirror (as if the recorder is in your hand on the first picture). Some people like this feature, others hate it (see https://forums.androidcentral.com/general-help-how/664539-front-camera-pics-mirrored-reversed-only-snapchat.html and https://www.reddit.com/r/nexus6/comments/3846ay/has_anyone_found_a_fix_for_snapchat_flipping)

You cannot use MediaRecorder for that. You can use the lower-level API of MediaCodec to record processed frames. You need to flip each frame 'manually', and this may be a significant performance hit, because normally the MediaRecorder 'connects' the camera to hardware encoder in a very efficient way, without need even to copy the pixels to user memory. This answer shows how you can manipulate the way camera is rendered to texture.