4

I started from Grafika example and I want to render the camera preview with GlRenderView. My question is how I can modify the transform matrix obtained from surfacetexture in order to get video preview mirrored like with device front camera:

mDisplaySurface.makeCurrent();
mCameraTexture.updateTexImage();
mCameraTexture.getTransformMatrix(mTmpMatrix);

mFullFrameBlit.drawFrame(mTextureId, mTmpMatrix);

I tried with the below line but my video gets a weird effect: // Apply horizontal flip.

// Apply horizontal flip.
android.opengl.Matrix.scaleM(mTmpMatrix, 0, -1, 1, 1);

Thank you all.

fadden
  • 51,356
  • 5
  • 116
  • 166
Siv
  • 193
  • 2
  • 16
  • Are you scaling the mesh that the texture is being drawn into, or scaling the entire scene (via the modelview matrix)? The TextureFromCameraActivity shows how to do scaling with the Sprite2d class. – fadden Jan 24 '17 at 03:49
  • Thanks you @fadden for your response that helped me to solve my problem. – Siv Jan 24 '17 at 16:13
  • how did you solve the problem? I also was trying the next line and getting weird results: android.opengl.Matrix.scaleM(matrix, 0, -1, 1, 1); – PerracoLabs Nov 20 '17 at 23:33

1 Answers1

2

I have also encounter this issue, and it`s weird that every time I call the drawFrame function twice it is correct for the first time and upside down for the second time, like this:

  mSurfaceTexture.updateTexImage();
  mSurfaceTexture.getTransformMatrix(mTmpMatrix);


  mDisplaySurface.makeCurrent();
  GLES20.glViewport(0, 0, mSurfaceView.getWidth(), mSurfaceView.getHeight());
  mFullFrameBlit.drawFrame(mTextureId, mTmpMatrix);//draws in correct direction
  mDisplaySurface.swapBuffers();

  mOffscreenSurface.makeCurrent();
  GLES20.glViewport(0, 0, desiredSize.getWidth(), desiredSize.getHeight());
  mFullFrameBlit.drawFrame(mTextureId, mTmpMatrix);//draws upside down
  mOffscreenSurface.getPixels();

Quite curious about why would this happen.....

Anyway, the solution is simple, just add a drawFrameFilpped function in FullFrameRect class and call that to draw a flipped image:

    public void drawFrameFlipped(int textureId, float[] texMatrix) {
    float[] mMatrix=GlUtil.IDENTITY_MATRIX.clone();//must clone a new one...
    Matrix m=new Matrix();
    m.setValues(mMatrix);
    m.postScale(1,-1);
    m.getValues(mMatrix);
    //note: the mMatrix is how gl will transform the scene, and 
    //texMatrix is how the texture to be drawn onto transforms, as @fadden has mentioned
    mProgram.draw(mMatrix, mRectDrawable.getVertexArray(), 0,
            mRectDrawable.getVertexCount(), mRectDrawable.getCoordsPerVertex(),
            mRectDrawable.getVertexStride(),
            texMatrix, mRectDrawable.getTexCoordArray(), textureId,
            mRectDrawable.getTexCoordStride());
    }

and call

    mFullFrameBlit.drawFrameFlipped(mTextureId, mTmpMatrix);
Michael.Z
  • 41
  • 7