diff --git a/mediapipe/java/com/google/mediapipe/components/GlSurfaceViewRenderer.java b/mediapipe/java/com/google/mediapipe/components/GlSurfaceViewRenderer.java index 4376aeb58..2ecb9d1e6 100644 --- a/mediapipe/java/com/google/mediapipe/components/GlSurfaceViewRenderer.java +++ b/mediapipe/java/com/google/mediapipe/components/GlSurfaceViewRenderer.java @@ -25,6 +25,7 @@ import android.opengl.GLES31; import android.opengl.GLSurfaceView; import android.opengl.Matrix; import android.util.Log; +import android.util.Pair; import com.google.mediapipe.framework.TextureFrame; import com.google.mediapipe.glutil.CommonShaders; import com.google.mediapipe.glutil.ShaderUtil; @@ -92,6 +93,9 @@ public class GlSurfaceViewRenderer implements GLSurfaceView.Renderer { private boolean shouldClampToBorder = false; private Scale scale = Scale.FILL; + private float zoomFactor = 1.0f; + private Pair zoomLocation = new Pair<>(0.5f, 0.5f); + /** * Sets the {@link BitmapCaptureListener}. */ @@ -294,6 +298,15 @@ public class GlSurfaceViewRenderer implements GLSurfaceView.Renderer { float textureBottom = (1.0f - scaleHeight) * alignmentVertical; float textureTop = textureBottom + scaleHeight; + Pair currentZoomLocation = this.zoomLocation; + float zoomLocationX = currentZoomLocation.first; + float zoomLocationY = currentZoomLocation.second; + + textureLeft = (textureLeft - 0.5f) / zoomFactor + zoomLocationX; + textureRight = (textureRight - 0.5f) / zoomFactor + zoomLocationX; + textureBottom = (textureBottom - 0.5f) / zoomFactor + zoomLocationY; + textureTop = (textureTop - 0.5f) / zoomFactor + zoomLocationY; + return new float[] {textureLeft, textureRight, textureBottom, textureTop}; } @@ -380,6 +393,22 @@ public class GlSurfaceViewRenderer implements GLSurfaceView.Renderer { this.scale = scale; } + /** Zoom factor applied to the frame, must not be 0. */ + public void setZoomFactor(float zoomFactor) { + if (zoomFactor == 0.f) { + return; + } + this.zoomFactor = zoomFactor; + } + + /** + * Location where to apply the zooming of the frame to. Default is 0.5, 0.5 (scaling is applied to + * the center). + */ + public void setZoomLocation(float zoomLocationX, float zoomLocationY) { + this.zoomLocation = new Pair<>(zoomLocationX, zoomLocationY); + } + private boolean isExternalTexture() { return textureTarget == GLES11Ext.GL_TEXTURE_EXTERNAL_OES; }