Search code examples
androidopengl-esandroid-mediacodecmediamuxer

Android image to video


How I can load an image as a texture and rendering it through GLES to use the MediaCodec Surface input approach?

I was started from EncodeAndMuxTest example.

Thank you in advance.


Solution

  • Look at sample from grafika ,it will give you insight about how you should do it

    Here is code to load bitmap into texture

    int mTextureId = -1;
    
    public void loadTexture(Bitmap bitmap)
    {
        if (mTextureId != -1) {
            int[] textureHandle = new int[1];
    
            GLES20.glGenTextures(1, textureHandle, 0);
    
    
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureId);
    
            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, 
                GLES20.GL_TEXTURE_MIN_FILTER,
                GLES20.GL_NEAREST);
            GLES20.glTexParameterf(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);
        } else {
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureId);
        }
    
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
     }
    

    and here is how you can render it to inputsurface

    // Create Fullframe rectangle (a class from grafika),
    mInputSurface.makeCurrent();
    mFullFrameRect = new FullFrameRect(new Texture2dProgram(Texture2dProgram.ProgramType.TEXTURE_2D));
    
    ....
    
    
    // And when you want to draw it
    mInputSurface.makeCurrent(); // if its not already current
    loadTexture(bitmap);
    GLES20.glViewport(0, 0, viewWidth, viewHeight);
    mFullFrameRect.drawFrame(mTextureId, GlUtil.IDENTITY_MATRIX);
    mInputSurface.setPresentationTime(pts);
    mInputSurface.swapBuffers();
    

    FullFrameRect, Texture2dProgram, GlUtil are classes from Grafika, so you should copy it or implement similar functionality by yourself