Add scaling support to surface view renderer.

PiperOrigin-RevId: 575134648
This commit is contained in:
MediaPipe Team 2023-10-20 01:06:10 -07:00 committed by Copybara-Service
parent 305d7abec4
commit 8fc3a0473f

View File

@ -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<Float, Float> 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<Float, Float> 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;
}