将位图加载到opengl纹理时,文本周围出现黑色瑕疵

时间:2017-10-20 22:26:18

标签: android opengl-es mediacodec android-mediacodec

我在图像上叠加文本,然后将图像加载到OpenGL纹理中。从那里使用MediaCodec将opengl表面编码为mp4。

这是我加载到纹理然后编码为mp4的图像的示例。 Bitmap being uploaded to texture

但是在使用MediaCodec将OpenGl表面编码为mp4后,文本周围会出现黑色文物。像这样:

Encoded mp4 screenshot

这基本上就是我如何将bmp加载到纹理中并显示它。

// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bmp, 0);
//Also have tried this
//GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, mWidth, mHeight, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ByteBuffer.wrap(frameData));
onDrawFrame(mInputSurface.textureHandle);

public void onDrawFrame(int textureHandle)
{
    GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);


    // Tell OpenGL to use this program when rendering.
    GLES20.glUseProgram(mProgramHandle);

    // Set program handles
    mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "position");
    mTextureCoordHandle = GLES20.glGetAttribLocation(mProgramHandle, "inputTextureCoordinate");
    mTextureUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "videoFrame");

    // Set the active texture unit to texture unit 0.
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

    // Bind the texture to this unit.
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle);

    // Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 0.
    GLES20.glUniform1i(mTextureUniformHandle, 0);

    drawTriangle(mRectVertices, mRectTextureCoordinates);

}   
    /**
 * Draws a triangle from the given vertex data.
 *
 * @param aTriangleBuffer The buffer containing the vertex data.
 * @param aRectTextureCoordinates
 */
private void drawTriangle(final FloatBuffer aTriangleBuffer, FloatBuffer aRectTextureCoordinates)
{       
    // Pass in the position information
    aTriangleBuffer.position(mPositionOffset);
    GLES20.glVertexAttribPointer(mPositionHandle, 2, GLES20.GL_FLOAT, false,
            0, aTriangleBuffer);
    GLES20.glEnableVertexAttribArray(mPositionHandle);        

    // Pass in the color information
    aRectTextureCoordinates.position(mPositionOffset);
    GLES20.glVertexAttribPointer(mTextureCoordHandle, 2, GLES20.GL_FLOAT, false,
            0,aRectTextureCoordinates);
    GLES20.glEnableVertexAttribArray(mTextureCoordHandle);

    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
}

知道可能导致这种情况的原因吗?

编辑: @BDL "你的纹理是否包含alpha通道?"

位图有一个alpha通道。它编码为ARGB_8888。 但我确保它完全不透明,所有像素的alpha值为255.

"您是否启用了混合?"

我已尝试启用

GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA);

没有任何改变

编辑2:

我将视口设置为与用于编码视频的表面大小相同。

 // Set the OpenGL viewport to the same size as the surface.
 GLES20.glViewport(0, 0, width, height);

但后来我尝试将宽度和高度加倍

 // Set the OpenGL viewport to the same size as the surface.
 GLES20.glViewport(0, 0, width*2, height*2);

虽然只有四分之一的图像显示......但没有更多的文物。

larger viewport example

0 个答案:

没有答案