android – 使用Matrix的rotateM()从SurfaceTexture旋转矩阵但破坏视频输出

我设法用opengl es播放视频,我使用了grafika的ContinuousCaptureActivity方式,我的数据源是MediaPlayer而不是Camera,这没什么区别.
MediaPlayer连续生成视频帧,我在onFrameAvailable回调中绘制每个帧到屏幕.代码如下,效果很好:

    mVideoTexture.updateTexImage();
    mVideoTexture.getTransformMatrix(mTmpMatrix);
    mDisplaySurface.makeCurrent();
    int viewWidth = getWidth();
    int viewHeight = getHeight();
    GLES20.glViewport(0, 0, viewWidth, viewHeight);
    mFullFrameBlit.drawFrame(mTextureId, mTmpMatrix);
    mDisplaySurface.swapBuffers();

现在我想旋转270度的视频帧,所以我改变了代码:

        GLES20.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    mVideoTexture.updateTexImage();
    mVideoTexture.getTransformMatrix(mTmpMatrix);
    mDisplaySurface.makeCurrent();
    int viewWidth = getWidth();
    int viewHeight = getHeight();
    GLES20.glViewport(0, 0, viewWidth, viewHeight);
    Matrix.rotateM(mTmpMatrix, 0, 270, 1f, 0, 0);
    mFullFrameBlit.drawFrame(mTextureId, mTmpMatrix);
    mDisplaySurface.swapBuffers();

但结果很奇怪,请看下面的图片:
enter image description here

但我可以使用以下代码成功翻转视频帧:

        mVideoTexture.updateTexImage();
    mVideoTexture.getTransformMatrix(mTmpMatrix);
    mDisplaySurface.makeCurrent();
    int viewWidth = getWidth();
    int viewHeight = getHeight();
    GLES20.glViewport(0, 0, viewWidth, viewHeight);
    mTmpMatrix[5] = -1 * mTmpMatrix[5];
    mTmpMatrix[13] = 1.0f - mTmpMatrix[13];
    mFullFrameBlit.drawFrame(mTextureId, mTmpMatrix);
    mDisplaySurface.swapBuffers();

如何实现轮换,谁能给我一些帮助?

加:

首先,我想告诉我,我总是将这个代码用于每个绘制操作:

        GLES20.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

我使用这个演示来做我的测试,这是一个非常好的测试演示.
https://github.com/izacus/AndroidOpenGLVideoDemo

从surfacetexture得到的矩阵是:

1.0,0.0,0.0,0.0

0.0,-1.0,0.0,0.0

0.0,0.0,1.0,0.0

0.0,1.0,0.0,1.0

在“Matrix.setRotateM(videoTextureTransform,0,270,0,0,1);”之后

它成了:

1.1924881E-8,-1.0,0.0,0.0

1.0,1.1924881E-8,0.0,0.0

0.0,0.0,1.0,0.0

0.0,0.0,0.0,1.0

而这个视频效果是:
enter image description here

最佳答案
关于z轴的fadden旋转矩阵需要跟随正确的平移才能将其带回到屏幕上,可以这么说.我在SurfaceTexture视频上测试了以下所有3个旋转:

旋转90

Matrix.rotateM(mTmpMatrix, 0, 90, 0, 0, 1);
Matrix.translateM(mTmpMatrix, 0, 0, -1, 0);

旋转180

Matrix.rotateM(mTmpMatrix, 0, 180, 0, 0, 1);
Matrix.translateM(mTmpMatrix, 0, -1, -1, 0);

旋转270

Matrix.rotateM(mTmpMatrix, 0, 270, 0, 0, 1);
Matrix.translateM(mTmpMatrix, 0, -1, 0, 0);

转载注明原文:android – 使用Matrix的rotateM()从SurfaceTexture旋转矩阵但破坏视频输出 - 代码日志