Alex Bush
Alex Bush

Reputation: 735

Android OpenGL Screenshot

I've searched a lot about taking screenshot of my OpenGL object on Android and come up with this solution. It worked great but in my case I have camera view and opengl view(with transparent background) on top of camera view. So what I want to do is to get opengl screenshot with transparent background instead of black. As I said I have tried link above and it worked but I'm stuck with black background. It's little bit complicated to figure out how to get rid of the black background in this particular case. Hope somebody could help me and asap if it's possible(also I think the solution is easy, I'm just missing something simple). Thank you.

Upvotes: 9

Views: 14620

Answers (2)

Will Kru
Will Kru

Reputation: 5212

The solution you mentioned is using a Bitmap.Config.RGB_565 which doesn't support alpha channels.

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

Instead you should use Bitmap.Config.ARGB_8888 or Bitmap.Config.ARGB_4444.

Upvotes: 6

Midhere
Midhere

Reputation: 664

I used following method and worked like a champ.

public static Bitmap SavePixels(int x, int y, int w, int h, GL10 gl)
{  
     int b[]=new int[w*(y+h)];
     int bt[]=new int[w*h];
     IntBuffer ib=IntBuffer.wrap(b);
     ib.position(0);
     gl.glReadPixels(x, 0, w, y+h, GL10.GL_RGBA, GL10.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;
}

You just need to call

YourClass.SavePixels(0,0,width,height,gl);

I hope this will work for you...

Thanks, Midhun

Upvotes: 17

Related Questions