【问题标题】:How to draw Buffer[] to a TextureView on Android?如何在 Android 上将 Buffer[] 绘制到 TextureView?
【发布时间】:2016-05-25 17:01:12
【问题描述】:

我正在使用 JavaCV 的 FFmpegFrameGrabber 从视频文件中检索帧。这个FFmpegFrameGrabber 返回一个Frame,它基本上包含一个Buffer[] 来保存视频帧的图像像素。

因为性能是我的重中之重,所以我想使用 OpenGL ES 直接显示这个Buffer[],而不是把它转换成Bitmap

要显示的视图只占不到一半的屏幕并遵循 OpenGL ES document:

想要将 OpenGL ES 图形合并到一小部分布局中的开发人员应该看看 TextureView。

所以我猜TextureView是这个任务的正确选择。但是我没有找到很多关于这个的资源(其中大部分是相机预览示例)。

我想问一下如何将Buffer[] 绘制到TextureView 上?如果这不是最有效的方法,我愿意尝试你的替代方案。


更新:所以目前我有这样的设置:

在我的VideoActivity 中,我反复提取包含ByteBuffer 的视频Frame,然后将其发送到我的MyGLRenderer2 以转换为OpenGLES 的纹理:

...
mGLSurfaceView = (GLSurfaceView)findViewById(R.id.gl_surface_view);
mGLSurfaceView.setEGLContextClientVersion(2);
mRenderer = new MyGLRenderer2(this);
mGLSurfaceView.setRenderer(mRenderer);
mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
...

private void grabCurrentFrame(final long currentPosition){
    if(mCanSeek){
        new AsyncTask(){

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                mCanSeek = false;
            }

            @Override
            protected Object doInBackground(Object[] params) {
                try {
                    Frame frame = mGrabber.grabImage();
                    setCurrentFrame((ByteBuffer)frame.image[0]);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Object o) {
                super.onPostExecute(o);
                mCanSeek = true;
                }
            }
        }.execute();
    }
}

private void setCurrentFrame(ByteBuffer buffer){
    mRenderer.setTexture(buffer);
}

MyGLRenderer2 看起来像这样:

public class MyGLRenderer2 implements GLSurfaceView.Renderer {
private static final String TAG = "MyGLRenderer2";
private FullFrameTexture mFullFrameTexture;

public MyGLRenderer2(Context context){
    super();
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    GLES20.glViewport(0,0,width, height);
    GLES20.glClearColor(0,0,0,1);
    mFullFrameTexture = new FullFrameTexture();
}

@Override
public void onDrawFrame(GL10 gl) {
    createFrameTexture(mCurrentBuffer, 1280, 720, GLES20.GL_RGB); //should not need to be a power of 2 since I use GL_CLAMP_TO_EDGE
    mFullFrameTexture.draw(textureHandle);
    if(mCurrentBuffer != null){
        mCurrentBuffer.clear();
    }
}

//test
private ByteBuffer mCurrentBuffer;

public void setTexture(ByteBuffer buffer){
    mCurrentBuffer = buffer.duplicate();
    mCurrentBuffer.position(0);
}

private int[] textureHandles = new int[1];
private int textureHandle;

public void createFrameTexture(ByteBuffer data, int width, int height, int format) {
    GLES20.glGenTextures(1, textureHandles, 0);
    textureHandle = textureHandles[0];
    GlUtil.checkGlError("glGenTextures");

    // Bind the texture handle to the 2D texture target.
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle);

    // Configure min/mag filtering, i.e. what scaling method do we use if what we're rendering
    // is smaller or larger than the source image.
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GlUtil.checkGlError("loadImageTexture");

    // Load the data from the buffer into the texture handle.
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, /*level*/ 0, format,
            width, height, /*border*/ 0, format, GLES20.GL_UNSIGNED_BYTE, data);
    GlUtil.checkGlError("loadImageTexture");
}

}

FullFrameTexture 看起来像这样:

public class FullFrameTexture {
private static final String VERTEXT_SHADER =
    "uniform mat4 uOrientationM;\n" +
        "uniform mat4 uTransformM;\n" +
        "attribute vec2 aPosition;\n" +
        "varying vec2 vTextureCoord;\n" +
        "void main() {\n" +
        "gl_Position = vec4(aPosition, 0.0, 1.0);\n" +
        "vTextureCoord = (uTransformM * ((uOrientationM * gl_Position + 1.0) * 0.5)).xy;" +
        "}";

private static final String FRAGMENT_SHADER =
    "precision mediump float;\n" +
        "uniform sampler2D sTexture;\n" +
        "varying vec2 vTextureCoord;\n" +
        "void main() {\n" +
        "gl_FragColor = texture2D(sTexture, vTextureCoord);\n" +
        "}";

private final byte[] FULL_QUAD_COORDINATES = {-1, 1, -1, -1, 1, 1, 1, -1};

private ShaderProgram shader;

private ByteBuffer fullQuadVertices;

private final float[] orientationMatrix = new float[16];
private final float[] transformMatrix = new float[16];

public FullFrameTexture() {
    if (shader != null) {
        shader = null;
    }

    shader = new ShaderProgram(EglUtil.getInstance());

    shader.create(VERTEXT_SHADER, FRAGMENT_SHADER);

    fullQuadVertices = ByteBuffer.allocateDirect(4 * 2);

    fullQuadVertices.put(FULL_QUAD_COORDINATES).position(0);

    Matrix.setRotateM(orientationMatrix, 0, 0, 0f, 0f, 1f);
    Matrix.setIdentityM(transformMatrix, 0);
}

public void release() {
    shader = null;
    fullQuadVertices = null;
}

public void draw(int textureId) {
    shader.use();

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);

    int uOrientationM = shader.getAttributeLocation("uOrientationM");
    int uTransformM = shader.getAttributeLocation("uTransformM");

    GLES20.glUniformMatrix4fv(uOrientationM, 1, false, orientationMatrix, 0);
    GLES20.glUniformMatrix4fv(uTransformM, 1, false, transformMatrix, 0);

    // Trigger actual rendering.
    renderQuad(shader.getAttributeLocation("aPosition"));

    shader.unUse();
}

private void renderQuad(int aPosition) {
    GLES20.glVertexAttribPointer(aPosition, 2, GLES20.GL_BYTE, false, 0, fullQuadVertices);
    GLES20.glEnableVertexAttribArray(aPosition);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
}

}

现在我可以在应用程序崩溃前短暂显示一些框架(颜色也错误)。

【问题讨论】:

    标签: android opengl-es javacv textureview


    【解决方案1】:

    按照您的要求进行操作的最有效方法是将像素转换为 OpenGL ES 纹理,然后在 TextureView 上进行渲染。要使用的函数是glTexImage2D()

    您可以在 Grafika 中找到一些示例,它使用该功能上传一些生成的纹理。看看createImageTexture()。如果您的应用中还没有 GLES 代码,Grafika 的 gles 包可能会很有用。

    FWIW,将视频帧直接解码到从 TextureView 的 SurfaceTexture 创建的 Surface 会更有效,但我不知道 JavaCV 是否支持。

    编辑:如果您不介意使用 NDK,另一种方法是使用 ANativeWindowCreate a Surface 用于 TextureView 的 SurfaceTexture,将其传递给本机代码,然后调用 ANativeWindow_fromSurface() 以获取 ANativeWindow。使用ANativeWindow_setBuffersGeometry() 设置大小和颜色格式。锁定缓冲区,复制像素,解锁缓冲区以发布它。我不认为这需要在内部进行额外的数据副本,并且可能比glTexImage2D() 方法具有一些优势。

    【讨论】:

    • 非常感谢您的指导,真的很有帮助。我会先试试 OpenGL ES 并尽快返回。
    • 您是否介意详细说明“在 TextureView 上渲染 OpenGL ES 纹理”?也许我弄错了,但即使我使用演示代码GeneratedTexture.createTestTexture(GeneratedTexture.Image.FINE);,输出始终为 0。
    • 确保您是从具有活动 EGL 上下文的线程调用它。参见例如TextureViewGLActivity 是一个使用 GLES 渲染到 TextureView 的示例,使用专用的渲染器线程。
    • 是的,我确实遵循 TextureViewGLActivity 示例。我试图用简单的GeneratedTexture.createTestTexture(GeneratedTexture.Image.FINE); 替换Renderer.doAnimation() 中的所有GL_SCISSOR_TEST 位,但我仍然出现黑屏。
    • 嗨@fadden,我想我必须放弃这种方法,因为它可能不符合我的性能要求。我在玩你的ExtractMpegFramesTest。最昂贵的操作是我不需要的 png 转换。是否可以使用ExtractMpegFramesTest 实现视频擦洗?假设我在解码接下来的 30 帧时让用户浏览它时提前解码了 30 帧。另一个问题是否需要连续帧,因为我可能不需要那么多帧。每 3 帧解码一帧是完美的。
    猜你喜欢
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2013-06-24
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多