Skip to content

Commit

Permalink
Add scaling support to surface view renderer.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 575134648
  • Loading branch information
MediaPipe Team authored and copybara-github committed Oct 20, 2023
1 parent 305d7ab commit 8fc3a04
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -92,6 +93,9 @@ public enum Scale {
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}.
*/
Expand Down Expand Up @@ -294,6 +298,15 @@ public float[] calculateTextureBoundary() {
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};
}

Expand Down Expand Up @@ -380,6 +393,22 @@ public void setScale(Scale scale) {
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;
}
Expand Down

0 comments on commit 8fc3a04

Please sign in to comment.