简体   繁体   English

Android 使用 SurfaceTexture TransformMatrix 镜像相机预览

[英]Android mirror the camera Preview using SurfaceTexture TransformMatrix

I started from Grafika example and I want to render the camera preview with GlRenderView.我从 Grafika 示例开始,我想用 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.谢谢你们。

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:我也遇到过这个问题,奇怪的是每次调用drawFrame函数两次都是第一次正确,第二次颠倒,像这样:

  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:无论如何,解决方案很简单,只需在 FullFrameRect 类中添加一个 drawFrameFillpped 函数并调用它来绘制翻转图像:

    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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM