diff --git a/cameraview/src/main/gles/com/otaliastudios/cameraview/TextureMediaEncoder.java b/cameraview/src/main/gles/com/otaliastudios/cameraview/TextureMediaEncoder.java index 8ac32875..7f0f3418 100644 --- a/cameraview/src/main/gles/com/otaliastudios/cameraview/TextureMediaEncoder.java +++ b/cameraview/src/main/gles/com/otaliastudios/cameraview/TextureMediaEncoder.java @@ -116,12 +116,17 @@ class TextureMediaEncoder extends VideoMediaEncoder // We must scale this matrix like GlCameraPreview does, because it might have some cropping. // Scaling takes place with respect to the (0, 0, 0) point, so we must apply a Translation to compensate. float[] transform = frame.transform; + float[] overlayTransform = frame.overlayTransform; float scaleX = mConfig.scaleX; float scaleY = mConfig.scaleY; float scaleTranslX = (1F - scaleX) / 2F; float scaleTranslY = (1F - scaleY) / 2F; Matrix.translateM(transform, 0, scaleTranslX, scaleTranslY, 0); Matrix.scaleM(transform, 0, scaleX, scaleY, 1); + if (overlayTransform != null) { + Matrix.translateM(overlayTransform, 0, scaleTranslX, scaleTranslY, 0); + Matrix.scaleM(overlayTransform, 0, scaleX, scaleY, 1); + } // We also must rotate this matrix. In GlCameraPreview it is not needed because it is a live // stream, but the output video, must be correctly rotated based on the device rotation at the moment. @@ -131,11 +136,16 @@ class TextureMediaEncoder extends VideoMediaEncoder Matrix.translateM(transform, 0, 0.5F, 0.5F, 0); Matrix.rotateM(transform, 0, mConfig.transformRotation, 0, 0, 1); Matrix.translateM(transform, 0, -0.5F, -0.5F, 0); + if (overlayTransform != null) { + Matrix.translateM(overlayTransform, 0, 0.5F, 0.5F, 0); + Matrix.rotateM(overlayTransform, 0, mConfig.transformRotation, 0, 0, 1); + Matrix.translateM(overlayTransform, 0, -0.5F, -0.5F, 0); + } drainOutput(false); // Future note: passing scale values to the viewport? They are scaleX and scaleY, // but flipped based on the mConfig.scaleFlipped boolean. - mViewport.drawFrame(mConfig.textureId, mConfig.overlayTextureId, transform, frame.overlayTransform); + mViewport.drawFrame(mConfig.textureId, mConfig.overlayTextureId, transform, overlayTransform); mWindow.setPresentationTime(frame.timestamp); mWindow.swapBuffers(); mFramePool.recycle(frame); diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/SnapshotPictureRecorder.java b/cameraview/src/main/java/com/otaliastudios/cameraview/SnapshotPictureRecorder.java index 7a442198..e8d174ee 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/SnapshotPictureRecorder.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/SnapshotPictureRecorder.java @@ -136,26 +136,48 @@ class SnapshotPictureRecorder extends PictureRecorder { float scaleTranslY = (1F - realScaleY) / 2F; Matrix.translateM(mTransform, 0, scaleTranslX, scaleTranslY, 0); Matrix.scaleM(mTransform, 0, realScaleX, realScaleY, 1); + if (mOverlayTransform != null) { + Matrix.translateM(mOverlayTransform, 0, scaleTranslX, scaleTranslY, 0); + Matrix.scaleM(mOverlayTransform, 0, realScaleX, realScaleY, 1); + } // Fix rotation: // TODO Not sure why we need the minus here... It makes no sense to me. LOG.w("Recording frame. Rotation:", mResult.rotation, "Actual:", -mResult.rotation); int rotation = -mResult.rotation; + int overlayRotation = -mController.offset(CameraController.REF_VIEW, CameraController.REF_OUTPUT); + // apparently with front facing camera with don't need the minus sign + if (mResult.facing == Facing.FRONT) { + overlayRotation = -overlayRotation; + } mResult.rotation = 0; // Go back to 0,0 so that rotate and flip work well. Matrix.translateM(mTransform, 0, 0.5F, 0.5F, 0); + if (mOverlayTransform != null) { + Matrix.translateM(mOverlayTransform, 0, 0.5F, 0.5F, 0); + } // Apply rotation: Matrix.rotateM(mTransform, 0, rotation, 0, 0, 1); + if (mOverlayTransform != null) { + Matrix.rotateM(mOverlayTransform, 0, overlayRotation, 0, 0, 1); + } // Flip horizontally for front camera: if (mResult.facing == Facing.FRONT) { Matrix.scaleM(mTransform, 0, -1, 1, 1); + if (mOverlayTransform != null) { + // not sure why we have to flip the mirror the y axis + Matrix.scaleM(mOverlayTransform, 0, -1, -1, 1); + } } // Go back to old position. Matrix.translateM(mTransform, 0, -0.5F, -0.5F, 0); + if (mOverlayTransform != null) { + Matrix.translateM(mOverlayTransform, 0, -0.5F, -0.5F, 0); + } // Future note: passing scale values to the viewport? // They are simply realScaleX and realScaleY.