Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using GLES20.glReadPixels on android, the data returned by it is not exactly the same with the living preview

I am trying to use Android GPUImage on this page https://github.com/CyberAgent/android-gpuimage to do some real time filtering work on video, and I use glReadPixels() to get the data which has been processed by the GPU and the filter has been added, then I created a bitmap using the data returned and compress it to a jpeg picture at last. The process works fine but the jpeg is not the same as the preview, it just displays the bottom part of the preview, however the size of it is right, I have been searching this for a long time but still get no clue,so can you guys give me some advices,any suggestion will be greatly appreciated.

And my code that using glReadPixels is like this:

public static Bitmap SavePixels(int x, int y, int w, int h){ 
    int b[]=new int[w*(y+h)];
    int bt[]=new int[w*h];
    IntBuffer ib = IntBuffer.wrap(b);
    ib.position(0);
    GLES20.glReadPixels(0, 0, w, h, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ib);

        for(int i=0, k=0; i<h; i++, k++)
        {//remember, that OpenGL bitmap is incompatible with Android bitmap
         //and so, some correction need.        
             for(int j=0; j<w; j++)
             {
                  int pix=b[i*w+j];
                  int pb=(pix>>16)&0xff;
                  int pr=(pix<<16)&0x00ff0000;
                  int pix1=(pix&0xff00ff00) | pr | pb;
                  bt[(h-k-1)*w+j]=pix1;
             }
        }

        Bitmap sb=Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
        return sb;
 }

I called this function just after the GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4) which is in the onDraw function.

like image 907
geekShaw Avatar asked Dec 16 '13 08:12

geekShaw


1 Answers

This issue has been solved, it happened because the glReadPixels reads the buffer which is connected with the surfaceView that the preview is displayed on, and the size of this surfaceView is bigger than the width and height params I setted in glReadPixels,so it just displayed part of the preview(begins from the left bottom corner of the preview).

like image 125
geekShaw Avatar answered Oct 02 '22 04:10

geekShaw