Android flip front camera mirror flipped video

2020-01-31 03:10发布

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.

4条回答
够拽才男人
2楼-- · 2020-01-31 03:21

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?

查看更多
男人必须洒脱
3楼-- · 2020-01-31 03:25

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);
查看更多
\"骚年 ilove
4楼-- · 2020-01-31 03:28

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.

查看更多
何必那么认真
5楼-- · 2020-01-31 03:43

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.

查看更多
登录 后发表回答