4

I am new to OpenGL ES in Android. But right now, I need to use OpenGL ES 2.0 to do some photo editing. I found the sample code "Hello-effect" in Android 4.1.2, it complete the work of editing the photo and render it to a window. But right now, I also need to save the edited photo to a local bitmap. I think there maybe some way to get data directly from texture, but the only method I found is glReadPixels(...). So I do some try to use it:

The first test I do:

  1. I use GLSurfaceView to show the photo that has been edited by android.media.effect API.
  2. I add a button in the same layout containing the GLSurfaceView.
  3. When click the button, I call glReadPixels(...) to get the photo data. But as the step 3 I only got the botton's data.
I guess that when I click the button the windows' framebuffer has been replaced by the button's content.

The second test I do:

  1. I create a FBO to save the edited photo and use glReadPixels(...) to get the photo data, but it is a black pic.

The code looks like this:

public void renderTextureOffscreen (int texId) {
    // Bind default FBO
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, offScreenFrameBuffer[0]); 
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER,GLES20.GL_COLOR_ATTACHMENT0,GLES20.GL_TEXTURE_2D, texId , 0);

    int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
    if(status == GLES20.GL_FRAMEBUFFER_COMPLETE)
    {
        // Set viewport
        GLES20.glViewport(0, 0, mViewWidth, mViewHeight);
        GLToolbox.checkGlError("glViewport");

        // Disable blending
        GLES20.glDisable(GLES20.GL_BLEND);

        // Set the vertex attributes
        GLES20.glVertexAttribPointer(mTexCoordHandle, 2, GLES20.GL_FLOAT, false,
            0, mTexVertices);
        GLES20.glEnableVertexAttribArray(mTexCoordHandle);
        GLES20.glVertexAttribPointer(mPosCoordHandle, 2, GLES20.GL_FLOAT, false,
            0, mPosVertices);
        GLES20.glEnableVertexAttribArray(mPosCoordHandle);
        GLToolbox.checkGlError("vertex attribute setup");

        // Set the input texture
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLToolbox.checkGlError("glActiveTexture");
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
        GLToolbox.checkGlError("glBindTexture");
        GLES20.glUniform1i(mTexSamplerHandle, 0);

        // Draw
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
    }

My question is that whether my guess is right? And as the FBO method, do I lose some critical step to finish my job.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117

1 Answers1

0

Try reading pixels right after

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

Also make sure that glReadPixels is called from the right thread, i.e. in your renderer subclass.

And another thing, i see that you are binding the texture used for the FBO as input to the shader, you cannot do that.

I suppose that after RTT, you are rendering the result to a quad or something on screen is that right ?

Can you show the rest of the code that you are using to read the pixels ?