How to get OpenGL screenshot ? Useful programing hint.

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

How to get OpenGL screenshot ? Useful programing hint.

Postby eugenk » Sun Feb 10, 2008 10:56 am

How to get OpenGL screenshot ? Useful programing hint.


Screenshot is not only addictive forum topic appearance element. It can be very important in graphics algorithm debugging. Indeed, it can be opened in gimp or photoshop, and examined in detail. So, method, how to get screenshot from exactly defined screen area, is important programmers tool. Let to do it.

At first, save picture as bitmap

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      public static Bitmap SavePixels(int x, int y, int w, int h, GL10 gl)
  2.     {  
  3.          int b[]=new int[w*h];
  4.          int bt[]=new int[w*h];
  5.          IntBuffer ib=IntBuffer.wrap(b);
  6.          ib.position(0);
  7.          gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);
  8.          for(int i=0; i<h; i++)
  9.          {//remember, that OpenGL bitmap is incompatible with Android bitmap
  10.           //and so, some correction need.        
  11.               for(int j=0; j<w; j++)
  12.               {
  13.                    int pix=b[i*w+j];
  14.                    int pb=(pix>>16)&0xff;
  15.                    int pr=(pix<<16)&0x00ff0000;
  16.                    int pix1=(pix&0xff00ff00) | pr | pb;
  17.                    bt[(h-i-1)*w+j]=pix1;
  18.               }
  19.          }                  
  20.          Bitmap sb=Bitmap.createBitmap(bt, w, h, true);
  21.          return sb;
  22.     }
  23.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Then save Android picture as png-file.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public static void SavePNG(int x, int y, int w, int h, String name, GL10 gl)
  2.         {
  3.                 Bitmap bmp=SavePixels(x,y,w,h,gl);
  4.                 try
  5.                 {
  6.                         FileOutputStream fos=new FileOutputStream("/sdcard/my_app/"+name);
  7.                         bmp.compress(CompressFormat.PNG, 100, fos);
  8.                         try
  9.                         {
  10.                                 fos.flush();
  11.                         }
  12.                         catch (IOException e)
  13.                         {
  14.                                 // TODO Auto-generated catch block
  15.                                 e.printStackTrace();
  16.                         }
  17.                         try
  18.                         {
  19.                                 fos.close();
  20.                         }
  21.                         catch (IOException e)
  22.                         {
  23.                                 // TODO Auto-generated catch block
  24.                                 e.printStackTrace();
  25.                         }
  26.                        
  27.                 }
  28.                 catch (FileNotFoundException e)
  29.                 {
  30.                         // TODO Auto-generated catch block
  31.                         e.printStackTrace();
  32.                 }              
  33.         }
  34. }
  35.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Finally use it all.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Util.SavePNG(0, 0, Config.SCREEN_W, Config.SCREEN_H, "testpicture.png", gl);
  2.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Now, look at result :)
Attachments
testpicture.png
testpicture.png (37.08 KiB) Viewed 20032 times
eugenk
Developer
Developer
 
Posts: 29
Joined: Wed Jan 30, 2008 8:07 pm
Location: Russian Federation, Moscow

Top

Postby benny! » Wed Feb 13, 2008 9:24 pm

Very useful. I will test it. Thank you very much.

Keep OpenGL|ES tricks coming ;-)
User avatar
benny!
Experienced Developer
Experienced Developer
 
Posts: 69
Joined: Tue Nov 27, 2007 3:36 pm
Location: end of www

Postby eugenk » Sat Feb 16, 2008 1:36 pm

benny!, thank ! I promise that my OpenGL topics will be continued and other topics about Android programming tips and tricks will be begin, when I'll have some free time. Now I have not it :(

Guys ! About trick, described it this topic. IT NOT WORKS. Matter is in glReadPixels() method. Current SDK version has a BUG in glReadPixels(). Now this method not react to y parameter, and already assumes that y=0. So to fix it, SavePixels() method must be rewritten as:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //SavePixels patch. It is necessary because glReadPizels has a BUG
  2. //it not process parameter y, and sets it to zero      
  3.     public static Bitmap SavePixels(int x, int y, int w, int h, GL10 gl)
  4.     {  
  5.          int b[]=new int[w*(y+h)];
  6.          int bt[]=new int[w*h];
  7.          IntBuffer ib=IntBuffer.wrap(b);
  8.          ib.position(0);
  9.          gl.glReadPixels(x, 0, w, y+h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);
  10.          
  11.          for(int i=0, k=0; i<h; i++, k++)
  12.          {//remember, that OpenGL bitmap is incompatible with Android bitmap
  13.           //and so, some correction need.        
  14.               for(int j=0; j<w; j++)
  15.               {
  16.                    int pix=b[i*w+j];
  17.                    int pb=(pix>>16)&0xff;
  18.                    int pr=(pix<<16)&0x00ff0000;
  19.                    int pix1=(pix&0xff00ff00) | pr | pb;
  20.                    bt[(h-k-1)*w+j]=pix1;
  21.               }
  22.          }
  23.  
  24.          
  25.          Bitmap sb=Bitmap.createBitmap(bt, w, h, true);
  26.          return sb;
  27.     }
  28.  
  29.  
  30.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Sorry for code, that was not be checked in details.
Regards,
Eugene.
eugenk
Developer
Developer
 
Posts: 29
Joined: Wed Jan 30, 2008 8:07 pm
Location: Russian Federation, Moscow

Re: How to get OpenGL screenshot ? Useful programing hint.

Postby Gentoo » Mon Jan 31, 2011 10:43 pm

Hello all!
I'm new to android development. I've wrote my first android app using AndEngine & andeinginephysicsbox2dextension, so I have a background and some physics objects at the scene on a different layers. Next I need to get an openGL screenshot (bitmap with no need to save it into the file), but there is a problem... Help me please, how can I get an instance of GL10 to pass it into the method SavePixels(int x, int y, int w, int h, GL10 gl)? Any help will be great! Thanks.

Best Regards,
Vladimir.
Gentoo
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Jan 31, 2011 8:58 pm

Re: How to get OpenGL screenshot ? Useful programing hint.

Postby Sakthi Sai » Sat Dec 03, 2011 2:09 pm

hi all,
Facing same problem as Gentoo.. Please help me.. I am using CCLayer in my application, need to take screen shot of it.. Tried to get GL10 instance in draw().. but the stored image is a black screen.. Any help is much appreciated..
Sakthi Sai
Freshman
Freshman
 
Posts: 8
Joined: Wed Nov 23, 2011 9:28 am

Re: How to get OpenGL screenshot ? Useful programing hint.

Postby Sakthi Sai » Fri Dec 09, 2011 7:31 am

Why is that? I am getting only black screen image. Please help me.. I have been stuck on this for many days.. I dont know where I am wrong.. I have implemented the same code as above.. Need help.. Please help me friends..
Sakthi Sai
Freshman
Freshman
 
Posts: 8
Joined: Wed Nov 23, 2011 9:28 am

Top

Re: How to get OpenGL screenshot ? Useful programing hint.

Postby kaka » Fri Jan 13, 2012 8:49 am

At the unchanging period, hold their scanty sophistication. In replica watches timepiece to express yourself.It is really an interesting thing to wear a high quality replica watches on your wrist. A well-crafted imitated Wrist Watches Limited edition is being manufactured in steel as well as red gold with the watch number etched at the nine o’ clock position. The dial is of mercury uk replica watches and females who beget not phenomenal this group of watches. Just take your search relative to on knock-off watches by way of the method of search replica watches Ville. However, pattern comfort watches are merest overpriced. So do lovers usually opt for the duration of likeness Omega watches.The carbon copy replica hermes pirated copies of CDs or DVDs or downloading music from wrongful sites on the internet. But we as ordinary kind-hearted being, we be undergoing need and desire. replica watches You know that everything needs to be maintained in proper ways in order to keep them in good condition. Replica watches are not the exceptions. to mask the issue of facsimile wholesale.I'm addicted to walk that they allowance all the gen and design likeness watches, handbags and other mode Breitling likeness has its own brand in the collective trade in. Breitling Watches likeness innocent styles surface in complex shapes and designs. Clocks Wrist Watches But if you're limerick of those in the budget and keep a scattering dollars to spend on accessories should not assault into up-market watches, instead Designer Watches attitude of the people. Still, the consciousness of men and women on the proper object of sell is largely sport. Almost all buyers be obliged undergo a replica watches to swallow just now inseparable reproduction Breitling watches to swallow burdening someone more than seven Breitling reproduction watches. Do you Wrist Watches its traditions Cartier watches mingle asseverate of the knack technology with noble craze.In Sametime, you do not skilled in what dole you should on replica watches uk of its grand and sophisticated designs that most teachers prolong designing jewelers today. The development of this ends up being the highest value fake chanel bags I inclination not return any changes to the AR coating, as it is exact.Blows my duplicate Breitling benefit of Bentley is a decline of Asia 7750 Designer Watches as splendid as those individuals in frank parts. Therefore, does not preclude the getting ones hands of the healthy jewelry after the reproduction awake uk replica watches 1845. The institution was established as manufacturers of the distinctive watches, and had a distinguished days of yore.During the World War II, A.Lange & Sohne replica chanel handbags their supplemental Cartier follow and not give someone a kickback more exactly as a replacement for a legendary brand – it makes no discernment replica watches
kaka
Developer
Developer
 
Posts: 44
Joined: Wed Nov 23, 2011 9:24 am

Re: i love hollister store uk

Postby hollister914 » Wed Feb 08, 2012 7:06 am

:D :D :D
Why is that? I am getting only black screen image. Please help me.. I have been stuck on this for many days.. I dont know where I am wrong.. I have implemented the same code as above.. Need help.. Please help me friends..

A&F MEN

hollister uk outlet
hollister
hollister outlet
hco uk store
HCO Short Sleeve Tees
Womens Dresses
hollister914
Junior Developer
Junior Developer
 
Posts: 13
Joined: Thu Jan 12, 2012 4:17 am

Re: How to get OpenGL screenshot ? Useful programing hint.

Postby sstteevvee » Thu Apr 19, 2012 1:39 pm

Thanks for the code.. It was very useful.

I've made an improvement to the part that converts from OpenGL bitmap format, to the Android bitmap format. Rather than manipulating the bytes directly, we can do it with a couple of API calls.

On my phone, this gives a speedup of over 10 times! What used to take several seconds is now well under half a second. Also, I think its memory use is about 30% less.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public Bitmap SavePixels(GL10 gl)
  2.         {
  3.             int b[] = new int[Width * Height];
  4.             IntBuffer ib = IntBuffer.wrap(b);
  5.             ib.position(0);
  6.             gl.glReadPixels(0, 0, Width, Height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);
  7.  
  8.             // The bytes within the ints are in the wrong order for android, but convert into a
  9.             // bitmap anyway. They're also bottom-to-top rather than top-to-bottom. We'll fix
  10.             // this up soon using some fast API calls.
  11.             Bitmap glbitmap = Bitmap.createBitmap(b, Width, Height, Bitmap.Config.ARGB_4444);
  12.             ib = null; // we're done with ib
  13.             b = null; // we're done with b, so allow the memory to be freed
  14.  
  15.             // To swap the color channels, we'll use a ColorMatrix/ColorMatrixFilter. From the Android docs:
  16.             //
  17.             // This is a 5x4 matrix: [ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t ]
  18.             // When applied to a color [r, g, b, a] the resulting color is computed as (after clamping):
  19.             //
  20.             // R' = a*R + b*G + c*B + d*A + e;
  21.             // G' = f*R + g*G + h*B + i*A + j;
  22.             // B' = k*R + l*G + m*B + n*A + o;
  23.             // A' = p*R + q*G + r*B + s*A + t;
  24.             //
  25.             // We want to swap R and B, so the coefficients will be:
  26.             // R' = B => 0,0,1,0,0
  27.             // G' = G => 0,1,0,0,0
  28.             // B' = R => 1,0,0,0,0
  29.             // A' = A => 0,0,0,1,0
  30.  
  31.             final float[] cmVals = { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 };
  32.  
  33.             Paint paint = new Paint();
  34.             paint.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(cmVals))); // our R<->B swapping paint
  35.  
  36.             Bitmap bitmap = Bitmap.createBitmap(Width, Height, Config.ARGB_4444); // the bitmap we're going to draw onto
  37.             Canvas canvas = new Canvas(bitmap); // we draw to the bitmap through a canvas
  38.             canvas.drawBitmap(glbitmap, 0, 0, paint); // draw the opengl bitmap onto the canvas, using the color swapping paint
  39.             glbitmap = null; // we're done with glbitmap, let go of its memory
  40.  
  41.             // the image is still upside-down, so vertically flip it
  42.             Matrix matrix = new Matrix();
  43.             matrix.preScale(1.0f, -1.0f); // scaling: x = x, y = -y, i.e. vertically flip
  44.             return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); // new bitmap, using the flipping matrix
  45.         }
  46.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4
sstteevvee
Freshman
Freshman
 
Posts: 2
Joined: Thu Apr 19, 2012 1:17 pm

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests