Reputation: 400
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.
Upvotes: 23
Views: 26915
Reputation: 15638
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
Upvotes: 0
Reputation: 1390
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);
Upvotes: 4
Reputation: 2297
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.
Upvotes: 2
Reputation: 971
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?
Upvotes: 2
Reputation: 172
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.
Upvotes: 0