Android flip front camera mirror flipped video
Asked Answered
P

5

23

I'm trying to record a video from Android's front camera while displaying on surface view as well.

What I found out with the front camera is that it mirror flips the video during recording, even though the surface view shows a normal view.

Is there any way I can prevent this or fix it?

I read upon other stackoverflow articles like How to keep android from inverting the image from the front facing camera?

But it seems to be only talking about taking photos with the front camera and reversing the image, which I already fixed using matrix myself. However, it seems like using a matrix for a video does not work.

Phenoxide answered 27/8, 2013 at 1:46 Comment(9)
Weird. If you are able to apply a matrix to the video there must be a way to flip it.Aleph
Hi, the preview looks fine, but the actually recording for some reason mirror flips it.Phenoxide
#7925778Strophic
That answer does not seem relevant. There seems to be no way to apply matrix transformations to video MediaRecorders, only to alter the view shown on recording surfaces.Homoeroticism
Hi @DannyKim, Have u found the solution of above problem?Havildar
Have a look at androidtrainningcenter.blogspot.inErupt
Did anyone find a way to this issue ?Allan
@DannyKim i am facing same issue. did you find solution?Playoff
@Piyush, see my illustrated answerBursar
O
4

In my case, I just need to horizontal-flip when playing it. When recording it's already flipped for mirror effect and as I see it's not possible to modify it. However below code solved my problem when playing.

videoPlayer.setScaleX(-1);
Onitaonlooker answered 10/7, 2017 at 15:58 Comment(0)
V
2

I don't have a solution, but it seems to me the key is:

MediaRecorder.setOrientationHint

Sets the orientation hint for output video playback. This method should be called before prepare(). This method will not trigger the source video frame to rotate during video recording, but to add a composition matrix containing the rotation angle in the output video if the output format is OutputFormat.THREE_GPP or OutputFormat.MPEG_4 so that a video player can choose the proper orientation for playback. Note that some video players may choose to ignore the compostion matrix in a video during playback.

I'm recording video in H264 and it doen't work for me :( But it might help you. did to try it?

Varus answered 16/9, 2013 at 16:5 Comment(2)
it only flips the orientation of the video (hence the method name). but the recorded video is still mirror flipped from preview :/Phenoxide
The above answer is unrelated to the question.Homoeroticism
S
2

In the documentation of the setDisplayOrientation method (http://developer.android.com/reference/android/hardware/Camera.html), we can find:

Note that preview display of front-facing cameras is flipped horizontally before the rotation, that is, the image is reflected along the central vertical axis of the camera sensor. So the users can see themselves as looking into a mirror.

So your preview display should be flipped but not your recorded video. With your preview display, do you see yourself as looking into a mirror? If so, everything is working fine.

Sophist answered 17/9, 2013 at 12:15 Comment(5)
No, I can't see myself in the front camera preview as if looking in a mirror. The problem I see is the exact opposite of what the documentation describes. The preview is correct after Camera.setDisplayOrientation(), but the H.264 video is flipped horizontally after MediaRecorder.setOrientationHint().Homoeroticism
If you can't see yourself in the front camera preview as if looking in a mirror, your device probably has a problem. I would suggest that you first try some other device with front facing camera.Bursar
Both the original poster and I see the same "problem" and it occurs on every Nexus device I own with a front-facing camera and a recent OS. Perhaps the documentation is outdated?Homoeroticism
I have yet to find any device running KitKat or Lollipop that does not reproduce this behavior when recording videos from the front facing camera. I have tried Samsung Galaxy S3, S4, S5, S6, Moto G, Nexus 4, and Nexus 6. I believe the docs are outdated or wrong.Homoeroticism
@Homoeroticism I finally wrote an illustrated answer, please check.Bursar
G
0

You have to apply a matrix transformation, like such:

if(isFrontCamera) {
    m.preScale(-ratio, ratio);
} else {
    m.preScale(ratio, ratio);
}

Assuming you don't resize anything the ration would be 1, making the non-front camera preScale obsolete.

Goraud answered 16/9, 2013 at 10:57 Comment(2)
There is no way to apply a matrix transformation to anything except views i.e. preview and playback. If the video recording itself is flipped, MediaRecorder does not seem to offer any functions to apply a transformation to fix the video it saves.Homoeroticism
@Homoeroticism yes, only possible if we use surface as input for mediarecorder and opengl for image processing, in this case we can do anything, flip and so onSelves
K
0

If you need to flip the video recording and save it into internal storage then you can use the Mp4Composer dependency in android studio.

First you need to add the dependency,

Step 1. Add the JitPack repository to your build file

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

dependencies {
        implementation 'com.github.MasayukiSuda:Mp4Composer-android:v0.4.1'
}

The following is a sample code for the same with vertical flip. You can flip it by making flipVertical(true). Also you can flip it horizontally by flipHorizontal(true).

private void flipVideo(String path, String destinationPath) {
        new Mp4Composer(sourcePath, destinationPath)
        .size(1280, 720)
        .fillMode(FillMode.PRESERVE_ASPECT_FIT)
        //.flipHorizontal(true)
        .flipVertical(true)
        // .rotation(Rotation.ROTATION_270)
        //.filter(glFilter)
        //.mute(muteCheckBox.isChecked())
        //.timeScale(2f)
        //.changePitch(false)
        //.trim(2000, 5000)
        .listener(new Mp4Composer.Listener() {
            @Override
            public void onProgress(double progress) {
                Log.d("app_log", "onProgress = " + progress);
                runOnUiThread(() -> progressBar.setProgress((int) (progress * 100)));
            }

            @Override
            public void onCompleted() {
                Log.d("app_log", "onCompleted(): video conversion completed");
               runOnUiThread(() -> {
                   Toast.makeText(CameraActivity.this, "video saved under " + destinationPath, Toast.LENGTH_SHORT).show();
               });
            }

            @Override
            public void onCanceled() {
            }

            @Override
            public void onCurrentWrittenVideoTime(long timeUs) {
            }

            @Override
            public void onFailed(Exception exception) {
                Log.e("app_log", "onFailed() " + exception.getMessage());
            }
        })
        .start();
    }

Call the method like this,

flipVideo(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/video_input.mp4", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/video_output.mp4");

For more: Mp4Composer-android

Kearns answered 24/9, 2021 at 17:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.