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

2

11

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

Sisera answered 9/11, 2017 at 9:4 Comment(14)
This may help you github.com/google/grafikaGewirtz
We are all so accustomed to the front camera mirroring effect that it becomes hard to distinguish 'correct' from 'inverse'. The front-facing camera captures video just same as the rear-facing camera. Try to capture short clip of the same scene with both cameras, and compare the results. It is your preview that is flipped (but it's not wrong, this is how we expect it to be).Stercoricolous
@AlexCohn I taken the short clip..the camera preview is not flipped,but the video that recorded is flipped...Sisera
can you upload the two video clips, e.g. to tinypic.com ?Stercoricolous
ok sure..later I show u..Sisera
@AlexCohn this is the video clip for camera preview(when recording) ufile.io/76rf2Sisera
@AlexCohn this is the video clip for the final video(after recording) ufile.io/2l3ufSisera
@AlexCohn u can see the book is inverse(book in left hand side go to right hand side)Sisera
I asked to take the same video with rear and front cameras. I believe they will look the same.Stercoricolous
Take with Rear and front camera?What is this mean?Sisera
Take video by the system camera app of the same scene using rear camera. Does it look like uploadfiles.io/76rf2 or like uploadfiles.io/2l3uf?Stercoricolous
@AlexCohn I take video with system camera..the video is look like the file I upload.Is this a expected behavior?Sisera
the video is look like the file I upload. You uploaded two files, does the 'system' video look like uploadfiles.io/76rf2 or like uploadfiles.io/2l3uf?Stercoricolous
Before recording is like uploadfiles.io/76rf2,the final video is like uploadfiles.io/2l3ufSisera
S
3

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.

Stercoricolous answered 12/11, 2017 at 12:55 Comment(10)
so in this case,is there any way to make the final video flip?Cause as you said,this is the natural way,then I want to flip it,therefore the the video will appear same as the preview right??Sisera
You cannot do this with MediaRecorder. You can use the lower-level API of MediaCodec to record anything. You still 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.Stercoricolous
wow..this may very painful for MediaCodec..anyways thank you very much..you just let me know,I am not doing wrong..tqSisera
The big question is: why should you present it wrongly flipped?Stercoricolous
cause I look at the snapchat camera in my phone,the camera preview and the final video is totally the same..so tot to build 1 for myself..But I just cant figure it out,why my final video is flipped,but snapchat is not flippedSisera
See forums.androidcentral.com/general-help-how/… and reddit.com/r/nexus6/comments/3846ay/…. Snapchat went an extra mile to make mirrored front-facing camera video. Some people like it, others don't.Stercoricolous
Actually, flipping the image as needed and cropping to an aspect ratio is not that expensive if you remap the SurfaceView's texture using OpenGL operations in the GPU. We did this in my startup's old app. It's somewhat easier to show the preview flipped when using a TextureView, which natively supports a rotation matrix. developer.android.com/reference/android/view/…Teodoor
@Teodoor Thanks for this input. While flipping the texture for output is easy, I don't have enough experience to judge if reamp of texture for MediaCodec works reliably on all devices.Stercoricolous
From experience, MediaCodec works better than MediaRecorder to get broad support back to 4.3/4.4. There just isn't a lot of sample code to learn from. Also, certain older devices have very specific issues to workaround because CTS tests weren't used much prior to Lollipop. Everything got easier in Lollipop.Teodoor
I won't argue about MediaCodec vs. MediaRecorder. My doubts were not about MediaCodec per se, but rather specifically about the picture flip for recording via MediaCodec by texture scaling.Stercoricolous
C
0

You can achieve this by recording video manually from surface view. In such case preview and recording will match exactly.

I've been using this library for this purpose:

https://github.com/spaceLenny/recordablesurfaceview

Here is the guide how to use it (not with camera but with OpenGL drawing): https://withintent.uncorkedstudios.com/recording-screen-video-on-android-with-recordablesurfaceview-451c9daa213e

Clipboard answered 18/3, 2022 at 10:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.