An OpenGL Example for Rendering a Bitmap to a MediaCodec Surface

I am looking for an example of how to render a surface bitmap provided by MediaCodec so that I can encode and then move them to mp4 video.

The closest well-known example that I see is EncodeAndMuxTest . Unfortunately, with my limited knowledge of OpenGL, I was not able to convert an example using bitmaps instead of the raw OpenGL frames that it is currently creating. Here is an example of a frame generation method.

private void generateSurfaceFrame(int frameIndex) {

    frameIndex %= 8;
    int startX, startY;
    if (frameIndex < 4) {
        // (0,0) is bottom-left in GL
        startX = frameIndex * (mWidth / 4);
        startY = mHeight / 2;
    } else {
        startX = (7 - frameIndex) * (mWidth / 4);
        startY = 0;
    }

    GLES20.glClearColor(TEST_R0 / 255.0f, TEST_G0 / 255.0f, TEST_B0 / 255.0f, 1.0f);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    GLES20.glEnable(GLES20.GL_SCISSOR_TEST);
    GLES20.glScissor(startX, startY, mWidth / 4, mHeight / 2);
    GLES20.glClearColor(TEST_R1 / 255.0f, TEST_G1 / 255.0f, TEST_B1 / 255.0f, 1.0f);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    GLES20.glDisable(GLES20.GL_SCISSOR_TEST);
}

-, , , , , ? , , , ( ).

: , generateSurfaceFrame , :

private int generatebitmapframe()
{
    final int[] textureHandle = new int[1];

    try {

        int id = _context.getResources().getIdentifier("drawable/other", null, _context.getPackageName());

        // Temporary create a bitmap
        Bitmap bmp = BitmapFactory.decodeResource(_context.getResources(), id);

        GLES20.glGenTextures(1, textureHandle, 0);

        if (textureHandle[0] != 0)
        {
            // Bind to the texture in OpenGL
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

            // Set filtering
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

            // Load the bitmap into the bound texture.
            GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bmp, 0);
        }

        if (textureHandle[0] == 0)
        {
            throw new RuntimeException("Error loading texture.");
        }

        //Utils.testSavebitmap(bmp, new File(OUTPUT_DIR, "testers.bmp").getAbsolutePath());
    }
    catch (Exception e) { e.printStackTrace(); }

    return textureHandle[0];
}
+2

All Articles