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.
Asked
Active
Viewed 1.4k times
2 Answers
17
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

Midhere
- 664
- 1
- 4
- 20
-
Looks promising! Sorry been super busy... will test it soon. – Alex Bush Feb 25 '11 at 05:13
-
4@Midhere... black screen shot. – Noman Dec 16 '11 at 06:06
-
@Midhere How do you initialize gl ? – XXX Mar 09 '13 at 21:43
-
1You should initialize gl using GLSurfaceView and by implementing render callback. GLSurfaceview will render/draw your 3d graphics and you need to write graphics draw code under onDrawFrame(GL10 gl) method. So savepixels should be called after drawing all graphics components. If you don't, you will see a black screen shot. – Midhere Mar 19 '13 at 06:35
-
Can you please provide me the complete code for this, i dont have knowledge about open GL.... :( – Anuj Sharma Jun 12 '13 at 07:40
-
For a version that doesn't require the byte copy, see `saveFrame()` in Grafika's EglSurfaceBase class (https://github.com/google/grafika/blob/master/src/com/android/grafika/gles/EglSurfaceBase.java). – fadden Jan 07 '15 at 16:34
-
8how to get gl in this? – Rohit Goyal Feb 22 '16 at 14:14
-
The function needs to run on an Open GL thread - please check here https://stackoverflow.com/questions/5234867/using-opengl-from-the-main-thread-on-android/5236218 – CanonicalBear Nov 27 '20 at 08:02
6
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
.
-
Thank you. I have tried to use ARGB_4444 and I got image with messed colors(I think something has shifted and it has funny look of many different colors). I think I need to change the algoritm of converting pixels to rgb. When I tried 8888 I got error about the size of bitmap. How should I change the size? – Alex Bush Jan 26 '11 at 02:48
-
ARGB_4444 'messes up' your colors because it's using half the amount of space for saving the same info. Whereas RGB_8888 uses 8 bits for every channel (R, G, B and the alpha channel) RGB_4444 uses 4 bits for each channel, therefore truncating the info and giving a very bad perceived quality. The most prominent effects are banding and color distortion. Some dithering techniques attempt to solve it, but I don't know if they're available natively in Android. If 8888 gives you a memory error, try decreasing the size of the bitmap. Maybe your device doesn't have enough RAM available. – RedOrav Jan 03 '13 at 20:47
-
3
-
Function should run on an Open GL thread: https://stackoverflow.com/questions/5234867/using-opengl-from-the-main-thread-on-android/5236218 – CanonicalBear Nov 27 '20 at 08:03