Fix video snapshot overlay rotation for Camera2 only

pull/502/head
Mattia Iavarone 6 years ago
parent ed34c67ef0
commit e83206be7a
  1. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera1Engine.java
  2. 4
      cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java
  3. 6
      cameraview/src/main/java/com/otaliastudios/cameraview/video/SnapshotVideoRecorder.java
  4. 7
      cameraview/src/main/java/com/otaliastudios/cameraview/video/encoding/TextureMediaEncoder.java
  5. 2
      demo/src/main/res/layout/activity_camera.xml

@ -372,7 +372,7 @@ public class Camera1Engine extends CameraEngine implements
LOG.i("onTakeVideoSnapshot", "rotation:", stub.rotation, "size:", stub.size); LOG.i("onTakeVideoSnapshot", "rotation:", stub.rotation, "size:", stub.size);
// Start. // Start.
mVideoRecorder = new SnapshotVideoRecorder(Camera1Engine.this, glPreview, getOverlay()); mVideoRecorder = new SnapshotVideoRecorder(Camera1Engine.this, glPreview, getOverlay(), stub.rotation);
mVideoRecorder.start(stub); mVideoRecorder.start(stub);
} }

@ -719,7 +719,9 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv
LOG.i("onTakeVideoSnapshot", "rotation:", stub.rotation, "size:", stub.size); LOG.i("onTakeVideoSnapshot", "rotation:", stub.rotation, "size:", stub.size);
// Start. // Start.
mVideoRecorder = new SnapshotVideoRecorder(this, glPreview, getOverlay()); // The overlay rotation should alway be VIEW-OUTPUT, just liek Camera1Engine.
int overlayRotation = getAngles().offset(Reference.VIEW, Reference.OUTPUT, Axis.ABSOLUTE);
mVideoRecorder = new SnapshotVideoRecorder(this, glPreview, getOverlay(), overlayRotation);
mVideoRecorder.start(stub); mVideoRecorder.start(stub);
} }

@ -62,15 +62,18 @@ public class SnapshotVideoRecorder extends VideoRecorder implements RendererFram
private Surface mOverlaySurface; private Surface mOverlaySurface;
private Overlay mOverlay; private Overlay mOverlay;
private boolean mHasOverlay; private boolean mHasOverlay;
private int mOverlayRotation;
public SnapshotVideoRecorder(@NonNull CameraEngine engine, public SnapshotVideoRecorder(@NonNull CameraEngine engine,
@NonNull GlCameraPreview preview, @NonNull GlCameraPreview preview,
@Nullable Overlay overlay) { @Nullable Overlay overlay,
int overlayRotation) {
super(engine); super(engine);
mPreview = preview; mPreview = preview;
mEngine = engine; mEngine = engine;
mOverlay = overlay; mOverlay = overlay;
mHasOverlay = overlay != null; mHasOverlay = overlay != null;
mOverlayRotation = overlayRotation;
} }
@Override @Override
@ -129,6 +132,7 @@ public class SnapshotVideoRecorder extends VideoRecorder implements RendererFram
mResult.rotation, mResult.rotation,
type, mTextureId, type, mTextureId,
mHasOverlay ? mOverlayTextureId : TextureMediaEncoder.NO_TEXTURE, mHasOverlay ? mOverlayTextureId : TextureMediaEncoder.NO_TEXTURE,
mOverlayRotation,
scaleX, scaleY, scaleX, scaleY,
mFlipped, mFlipped,
EGL14.eglGetCurrentContext() EGL14.eglGetCurrentContext()

@ -34,15 +34,18 @@ public class TextureMediaEncoder extends VideoMediaEncoder<TextureMediaEncoder.C
boolean scaleFlipped; boolean scaleFlipped;
EGLContext eglContext; EGLContext eglContext;
int transformRotation; int transformRotation;
int overlayTransformRotation;
public Config(int width, int height, int bitRate, int frameRate, int rotation, String mimeType, public Config(int width, int height, int bitRate, int frameRate, int rotation, String mimeType,
int textureId, int overlayTextureId, float scaleX, float scaleY, boolean scaleFlipped, EGLContext eglContext) { int textureId, int overlayTextureId, int overlayTransformRotation,
float scaleX, float scaleY, boolean scaleFlipped, EGLContext eglContext) {
// We rotate the texture using transformRotation. Pass rotation=0 to super so that // We rotate the texture using transformRotation. Pass rotation=0 to super so that
// no rotation metadata is written into the output file. // no rotation metadata is written into the output file.
super(width, height, bitRate, frameRate, 0, mimeType); super(width, height, bitRate, frameRate, 0, mimeType);
this.transformRotation = rotation; this.transformRotation = rotation;
this.textureId = textureId; this.textureId = textureId;
this.overlayTextureId = overlayTextureId; this.overlayTextureId = overlayTextureId;
this.overlayTransformRotation = overlayTransformRotation;
this.scaleX = scaleX; this.scaleX = scaleX;
this.scaleY = scaleY; this.scaleY = scaleY;
this.scaleFlipped = scaleFlipped; this.scaleFlipped = scaleFlipped;
@ -140,7 +143,7 @@ public class TextureMediaEncoder extends VideoMediaEncoder<TextureMediaEncoder.C
boolean hasOverlay = mConfig.overlayTextureId != NO_TEXTURE; boolean hasOverlay = mConfig.overlayTextureId != NO_TEXTURE;
if (hasOverlay) { if (hasOverlay) {
Matrix.translateM(overlayTransform, 0, 0.5F, 0.5F, 0); Matrix.translateM(overlayTransform, 0, 0.5F, 0.5F, 0);
Matrix.rotateM(overlayTransform, 0, mConfig.transformRotation, 0, 0, 1); Matrix.rotateM(overlayTransform, 0, mConfig.overlayTransformRotation, 0, 0, 1);
Matrix.translateM(overlayTransform, 0, -0.5F, -0.5F, 0); Matrix.translateM(overlayTransform, 0, -0.5F, -0.5F, 0);
} }

@ -17,7 +17,7 @@
android:layout_marginBottom="88dp" android:layout_marginBottom="88dp"
android:keepScreenOn="true" android:keepScreenOn="true"
app:cameraExperimental="true" app:cameraExperimental="true"
app:cameraEngine="camera2" app:cameraEngine="camera1"
app:cameraPreview="glSurface" app:cameraPreview="glSurface"
app:cameraPlaySounds="true" app:cameraPlaySounds="true"
app:cameraGrid="off" app:cameraGrid="off"

Loading…
Cancel
Save