我设法用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();
但我可以使用以下代码成功翻转视频帧:
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);
我用这个demo来做我的测试,对于这个测试来说是一个很好的demo。 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