简体   繁体   English

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

[英]Use rotateM() of Matrix to rotate matrix from SurfaceTexture but corrupt the video output

I managed to play video with opengl es, I used the way of grafika's ContinuousCaptureActivity, my data source is MediaPlayer rather than Camera which makes no difference. 我设法用opengl es播放视频,我使用了grafika的ContinuousCaptureActivity方式,我的数据源是MediaPlayer而不是Camera,这没什么区别。 MediaPlayer produces video frames continuously and I draw each frame to screen in onFrameAvailable callback. MediaPlayer连续生成视频帧,我在onFrameAvailable回调中绘制每个帧到屏幕。 The code is as follows which works well: 代码如下,效果很好:

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

Now I want to rotate video frames with 270 degrees, so I changed the code: 现在我想旋转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();

But the result is weird, take a look at the picture below: 但结果很奇怪,请看下面的图片: 在此输入图像描述

But I can flip video frame successfully with the code below: 但我可以使用以下代码成功翻转视频帧:

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

How to achieve the rotation, Could anyone give me some help? 如何实现轮换,谁能给我一些帮助?

ADD: 加:

At first, I want to tell that I always used this code for each draw action: 首先,我想告诉我,我总是将这个代码用于每个绘制操作:

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

I use this demo to do my test, it is a very good demo for this test. 我使用这个演示来做我的测试,这是一个非常好的测试演示。 https://github.com/izacus/AndroidOpenGLVideoDemo https://github.com/izacus/AndroidOpenGLVideoDemo

The matrix got from the surfacetexture is: 从surfacetexture得到的矩阵是:

1.0, 0.0, 0.0, 0.0 1.0,0.0,0.0,0.0

0.0, -1.0, 0.0, 0.0 0.0,-1.0,0.0,0.0

0.0, 0.0, 1.0, 0.0 0.0,0.0,1.0,0.0

0.0, 1.0, 0.0, 1.0 0.0,1.0,0.0,1.0

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

it became: 它成了:

1.1924881E-8, -1.0, 0.0, 0.0 1.1924881E-8,-1.0,0.0,0.0

1.0, 1.1924881E-8, 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,1.0,0.0

0.0, 0.0, 0.0, 1.0 0.0,0.0,0.0,1.0

And this video effect is: 而这个视频效果是: 在此输入图像描述

fadden's rotation matrix about the z-axis needs to be followed by the correct translation to bring it back on-screen, so to speak. 关于z轴的fadden旋转矩阵需要跟随正确的平移才能将其带回到屏幕上,可以这么说。 I've tested all 3 rotations below on SurfaceTexture video: 我在SurfaceTexture视频上测试了以下所有3个旋转:

ROTATE 90 旋转90

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

ROTATE 180 旋转180

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

ROTATE 270 旋转270

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

You're rotating about the X axis: 你绕着X轴旋转:

Matrix.rotateM(mTmpMatrix, 0, 270, 1f, 0, 0);

By convention that runs from left to right. 按惯例,从左到右。 The axis acts like an axle; 轴就像一个轴; by flipping it 270 degrees, you're rotating the plane so you're viewing it edge-on, and it's effectively vanishing. 通过将它翻转270度,你正在旋转飞机,这样你就可以正常地观察它,并且它正在逐渐消失。 I think what you're seeing is essentially uninitialized data, and if you call glClear() you'll see the background color instead. 我认为你所看到的基本上是未初始化的数据,如果你调用glClear()你会看到背景颜色。

Try rotating about the Z axis, which is a line pointing out of the screen: 尝试围绕Z轴旋转,这是一条指向屏幕外的线:

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

(It might also be interesting to experiment with a rotation of about 15 degrees about the X axis just to see how that looks. When fiddling with matrices it's often useful to start with small values.) (尝试围绕X轴旋转大约15度也可能是有趣的,只是为了看看它的外观。当摆弄矩阵时,从小值开始通常很有用。)

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

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